config.action_mailer.asset_host not serving images from cloud front CDN - ruby-on-rails

I have a CloudFront CDN serving my rails assets. When viewing source of assets served through a web browser it points to my CDN properly. However, Action Mailer is not using this path when serving its assets.
Why might this be?
Here is my environments/production.rb file:
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { host: 'localhost' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.smtp_settings = {
:address => "email-smtp.us-east-1.amazonaws.com",
:port => 587,
:user_name => Figaro.env.smtp_username, #Your SMTP user
:password => Figaro.env.smtp_password, #Your SMTP password
:authentication => :login,
:enable_starttls_auto => true
}
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.action_controller.asset_host = Figaro.env.cloudfront_endpoint
config.action_mailer.asset_host = Figaro.env.cloudfront_endpoint

It seems you are not setting the correct value for config.action_mailer.default_url_options.
This is what the docs says:
Unlike controllers, the mailer instance doesn't have any context about the incoming request so you'll need to provide the :host parameter yourself.
As the :host usually is consistent across the application you can configure it globally in config/application.rb
it should be:
config.action_mailer.default_url_options = { host: Figaro.env.cloudfront_endpoint }
Note: make sure you are using image_url instead of image_path in your mail views/templates

Related

Error sending emails with Rails 4 & Devise (connection refused, port 25)

I'be been having some massive issues getting emails to send. I've tried multiple ways of doing things; using gmail, mailgun, sendgrid, the mailgun gem etc, no luck at all. For some reason it's trying to send using port 25 although, as you can see below, I'm specifying port 587. The error I get is:
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
My development.rb file is as follows:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => 'smtp.mailgun.org',
:port => 587,
:api_key => 'key-5d321b99f722e71f759993ee41512b99',
:user_name => 'postmaster#sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:password => '(mailgun password)',
:domain => 'sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:authentication => :plain,
:enable_starttls_auto => true
}
I have looked around for solutions however none seem to solve the issue. I'm totally out of ideas.
entire Development.rb document:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = false
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
user_name: "chris.edhogben#gmail.com",
password: "<password>",
authentication: 'plain',
enable_starttls_auto: true
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
Try This configuration
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'localhost',
port: 25,
domain: 'exmaple.com',
enable_starttls_auto: false
}
Use the below settigns in production.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => 'smtp.mailgun.org',
:port => 587,
:user_name => 'postmaster#sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:password => '(mailgun password)',
:domain => 'sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:authentication => :plain
}
Use the below settigns for gmail:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
user_name: "username",
password: "password",
authentication: 'plain',
enable_starttls_auto: true
}
If you get SMTP authentication error , then click on http://www.google.com/accounts/DisplayUnlockCaptcha & enable it. It will enable google for registering new apps within 10 minutes
add same mail configuration in production.rb also.
Restart the server and check

Devise Mail is not being catched in development environment

I have tried doing authentication using devise in my rails app, I have made use of the 'confirmable' module in devise. However I am able to sign up but i am not seeing any email captured on my mailcatcher user interface. How do I get this problem sorted???
My development.rb is configured as follows
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost: 3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => 'localhost', :port => 1025}
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
You can use Google Mail as your smtp server.
Replace
config.action_mailer.smtp_settings = {:address => 'localhost', :port => 1025}
With
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:domain => 'mail.google.com',
:port => 587,
:user_name => 'your#gmail.com',
:password => 'password',
:authentication => :plain,
:enable_starttls_auto => true
}
For me I used the following configuration
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "your_gmail",
:password => "your_password",
:authentication => "plain",
:enable_starttls_auto => true
}
Then I opened my email and opened this https://www.google.com/settings/security/lesssecureapps then set it to on.
Remember also to restart your server after editing your development.rb

I don't get confirm mail

I have a problem with devise mail confirm.
I adding mailcatcher, that work correct:
Thu, 24 Sep 2015 05:49:50 +0300
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: ...#gmail.com
Message-ID: <560364ce3f3d_34372357331e9383d#gerdon-MS-7346.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome ...#gmail.com!</p>
<p>You can confirm your account email through the link below:</p>
<p>Confirm my account</p>
but i not getting this at my gmail.
Here is my code on rails:
/development.rb
Rails.application.configure do
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}
end
/production.rb
Rails.application.configure do
config.log_level = :debug
config.i18n.fallbacks = true
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
config.action_mailer.default_url_options = {:host => 'localhost:3000'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "127.0.0.1"
:port => 25,
:domain => 'localhost:3000'
}
end
i use this comment like a documentation in my try to set mail confirm:
How do I set up email confirmation with Devise?
To enable email sending, you need to do two things.
Configure your email for in config/environments/development.rb (not sure if you've already done that.
Configure your email in Devise by editing config/initializers/devise.rb. You've definitely not done that, given the "from" address visible in your log.
Step 1. Here are settings if you are sending from a gmail account. Pay attention to all the email settings, including allowing email sending in development mode and default url option. You have to change the smtp settings to reflect your account.
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Send emails in test mode
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "domain.of.sender.net",
authentication: "plain",
user_name: "your_user_name",
password: "your_password",
enable_starttls_auto: true
}
Step 2. Configure config/initializers/devise.rb to include the email that you just configured for sending.
# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|
# The secret key used by Devise. Devise uses this key to generate
# random tokens. Changing this key will render invalid all existing
# confirmation, reset password and unlock tokens in the database.
# Devise will use the `secret_key_base` on Rails 4+ applications as its `secret_key`
# by default. You can change it below and use your own secret key.
# config.secret_key = 'ewe44lwemwle66wmew4lewwew'
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'YOUR_EMAIL_HERE'
This may sound like a silly question, but are you running an smtp server on localhost??

Changed the domain name of heroku ruby on rails app, user mailer emails sending links to wrong domain? [duplicate]

I am trying to push my app on heroku. I am still in dev.
I use devise with the confirmable module.
When I try to add a user with the heroku console I got this error:
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
in test and dev environment i have the following line:
environments/development.rb and environments/test.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
I don't have set up something in the production environment.
I've tried to push with
config.action_mailer.default_url_options = { :host => 'mywebsitename.com' }
config.action_mailer.default_url_options = { :host => 'heroku.mywebsitename.com' }
but it doesn't work too..
I see on the web that it could be related to ActionMailer but I don't know what I have to configure.
Many thanks for your idea!
EDITED:
Hi,
In order to not make my app crashes when I push on heroku I put this in my env/test.rb and my env/dev.rb (not in env.rb I think it is because it's a rails 3 app)
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
But when I tried to create a user in the heroku console:
User.create(:username => "test", :email => "test#test.com", :password => "test1234", :password_confirmation => "test1234", :confirmed_at => "2010-11-03 14:11:15.520128")
here are errors I got:
ActionView::Template::Error: Missing host to link to! Please provide :host parameter or set default_url_options[:host]
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:473:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:195:in `user_confirmation_url'
EDITED (2)
When I type heroku logs on console I got this ==> production.log <== So I think when one deploys on heroku it's already in production.
I configure the env/prod.rb like this:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
and now I have this as error when I try to create a User:
Errno::EAFNOSUPPORT: Address family not supported by protocol - socket(2)
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `initialize'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `open'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/usr/ruby1.8.7/lib/ruby/1.8/timeout.rb:62:in `timeout'
You need to add this to your environment.rb
config.action_mailer.default_url_options = { :host => 'localhost' }
Make sure you change host to your production url and keep it localhost for development. This is for the mailer, it needs a default email to send out notices such as confirmations etc...
You should check the logs on the heroku server heroku logs run that from the console and it will tell you the exact error.
When you push to heroku you need to configure the environment.rb file with the heroku subdomain:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Depending upon version, this should go in production.rb, not environment.rb.
Ok,
First you have to install the sendgrid gem with this command line:
heroku addons:add sendgrid:free
Then you just have to configure your env/dev.rb and env/prod.rb like this:
env/dev.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
env/prod.rb
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Push on git and heroku. It should work..
Codeglot's anwser above does the job, but we wanted something a bit more flexible, so we did this:
On Heroku, we run multiple Production environments for staging and testing, so we need a flexible solution for the production.rb environment file.
In production.rb
config.action_mailer.default_url_options = { :host => ENV['MAILER_URL'] }
Then set the MAILER_URL environment variable for your app like so
heroku config:set MAILER_URL=my-awesome-app.herokuapp.com --app my-awesome-app
If you're running on Cedar:
run heroku addons:add sendgrid:free from your console.
Add the following lines to config/environments/production.rb in your app.
.
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'YOUR-DOMAIN-HERE.COM' }
I had to do a number of things to get it to work in the production environment:
Inside of my production.rb file (/config/environments/production.rb) I added the following:
Rails.application.routes.default_url_options[:host] = 'myappsname.herokuapp.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
This is with Rails 4 and Devise 3
The working one after so many research,
Don't forget to add default from: mail address in your ApplicationMailer (application_mailer.rb) as,
class ApplicationMailer < ActionMailer::Base
default from: 'yourmail#gmail.com'
layout 'mailer'
end
Add the below configuration in your production.rb.
config.action_mailer.default_url_options =
{ :host => 'yourapp.herokuapp.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'heroku.com',
user_name: 'yourmail#gmail.com',
password: 'yourgmailpassword',
authentication: 'login',
enable_starttls_auto: true
}
Enable IMAP from your Gmail settings in Forwarding IMAP/POP tab.
Allow less secure apps: ON from https://myaccount.google.com/lesssecureapps
You're now good to go. :)

PDF attachment broken on rails 3.1.6 production environment

I store a pdf document in the database and send it by email with this lines of code:+
def invoice(payment)
#payment = payment
#user = payment.subscriber or raise(ActiveRecord::RecordNotFound)
#subscription = payment.subscription or raise(ActiveRecord::RecordNotFound)
attachments[#payment.invoice_doc_name] = {
:mime_type => 'application/pdf',
:encoding => 'base64',
:content => #payment.invoice_pdf
}
mail to: #user.email
end
This works perfectly fine on my dev, test and staging environment which is on Heroku Cedar like production. The PDF is correct, I can download it in the user interface. Production configuration is:
Railsapp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Specify the default JavaScript compressor
config.assets.js_compressor = :uglifier
# Specifies the header that your server uses for sending files
# (comment out if your front-end server doesn't support this)
config.action_dispatch.x_sendfile_header = nil
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Use a different logger for distributed setups
config.logger = Logger.new($stdout)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.default_url_options = {
:host => 'www.****.com',
:protocol => 'https'
}
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:enable_startls_auto => true,
:port => 587,
:authentication => :plain,
:user_name => '***',
:password => '***'
}
end

Resources