Rails action_mailer config not working in production - ruby-on-rails

I am trying to setup action_mailer with Rails 4.1.0 to send emails.
In my config/environments/{env}.rb, I have:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
domain: 'domain.com',
authentication: 'login',
user_name: 'email#domain.com',
password: 'pass',
enable_starttls_auto: true }
It's working fine in development, but in prod it's trying to connect to localhost.
m = Mail.new
m.delivery_method
=> #<Mail::SMTP:0xbd79fdac #settings={:address=>"localhost", :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}>
But when I do "puts Rails.application.config.action_mailer" Im getting the correct config
{:raise_delivery_errors=>false, :default_url_options=>{:host=>"domain.com"}, :delivery_method=>:smtp, :smtp_settings=>{:address=>"smtp.mandrillapp.com", :port=>587, :domain=>"domain.com", :authentication=>"login", :user_name=>"email#domain.com", :password=>"password", :enable_starttls_auto=>true}, :assets_dir=>"xxx", :javascripts_dir=>"xxx", :stylesheets_dir=>"xxx", :asset_host=>nil, :relative_url_root=>nil}
Any idea?

The Mail gem won't use your ActionMailer config by default. ActionMailer uses Mail but those configuration settings will only apply if you send mail via ActionMailer.
If it's working in your dev mode, then you certainly have configured the Mail gem separately in your development.rb environment file, or in an environment specific initializer.

I forgot I had this config in my development.rb
Mail.defaults do
delivery_method Rails.configuration.action_mailer.delivery_method, Rails.configuration.action_mailer.smtp_settings
end

You need to add the host option for the default_url_options hash on the production.rb file, like so:
config.action_mailer.default_url_options = { :host => ENV['HOST_DEFAULT_URL'],
only_path: false }
That should do the trick.

Related

how to send mail through the localhost?

please help solve the problem.
i use webrick server on localhost. in development.rb i registered:
config/enviroments/development.rb:
Rails.application.configure do
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
i use gem devise. and i user 'forgot youre password'-function (devise-recoverable module). the result did not get to the email message. but where can I find a mark(log) that was an attempt to send a letter? whether there is a directory where they are stored?
You will need to edit your config/environments/development.rb file.
Here's something to get you started, add this to that file:
# port 3000 or whatever port you are using for your localhost
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
# this line is what you want to be true, else you won't get messages!
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'gmail.com' # or to whatever domain your email is
authentication: "plain",
enable_starttls_auto: true,
user_name: 'my_gmail_or_other_username',
password: 'my_gmail_or_other_password'
}
Now, when you are moving towards production or even just pushing it to a public repository you obviously don't want to hardcode the username and password (for security purposes). For this I suggest you look up how to use environmental variables. The Figaro gem might also come in handy here and is another option.
This is the default 'smtp' way, there are other methods, such as the use of a gem as specified by the earlier answer.
try gem mailcatcher for development purpose.
http://mailcatcher.me/
github: https://github.com/sj26/mailcatcher
This gem will provide you a tab in browser where all your emails will be there unless you delete them.

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. :)

Using Amazon SES & Devise

So I want devise to utilize amazon ses to send the "I forgot my password" emails to a user. I have followed all of the steps for setting up devise with Gmail and then tried to configure SES as the mailer in production.rb I am new with ruby and I am sure there is something I have done incorrectly with either my syntax or configuration.
Production.rb
config.action_mailer.delivery_method = :smtp,
config.action_mailer.smtp_settings = {
:address => "email-smtp.us-west-2.amazonaws.com",
:user_name => ENV['AWS_SMTP_USER'], # Your SMTP user here.
:password => ENV['AWS_SMTP_PASSWORD'], # Your SMTP password here.
:authentication => :login,
:enable_starttls_auto => true
}
I stored both AWS_SMTP_USER & AWS_SMTP_PASSWORD to the config using "$ Heroku Config"
Devise.rb
config.mailer_sender = 'Admin#synapticecho.com'
I don't think I am missing anything else, but I haven't found anyone trying to use Devise & SES as the mailer. I'm just not sure what other pieces I might be missing and I didn't find the AWS documentation all that helpful for configuring SES to work with Devise.
I can post the github project if it helps.
In my case.
I got the following error.
Net::SMTPFatalError (554 Message rejected: Email address is not verified
but, just like you. I didn't know missing anything else.
try below setting.
config.action_mailer.default_url_options = { host: Rails.application.secrets.HOST }
config.after_initialize do
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: Rails.application.secrets.ACTION_MAILER_ADDRESS,
port: Rails.application.secrets.ACTION_MAILER_PORT,
domain: Rails.application.secrets.ACTION_MAILER_DOMAIN,
user_name: Rails.application.secrets.ACTION_MAILER_USER_NAME,
password: Rails.application.secrets.ACTION_MAILER_PASSWORD,
authentication: :login,
ssl: true,
tls: true,
enable_starttls_auto: true
}
end
It works, my env.
That point is config.after_initialize.
but, I don't know why need it.
Rails 4.1.4
Devise 3.2.4
AWS SES
In AWS EC2
Verified email's domain is 'gmail.com' ( I'll change it)
I fix the problem by adding the following line in production.rb.
ActionMailer::Base.default :from => 'my.verified.email.address'

Rails action mailer didn't work on production

I am trying to send emails via action mailer. I have the same config for production and for development:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'ee.ex.my_domain.net',
port: 465,
domain: 'my_domain',
user_name: 'email#my_mail.com',
password: 'my_pass',
authentication: :ntlm,
enable_starttls_auto: true
}
config.action_mailer.default_url_options = { :host => "localhost:3000" }
The only difference between development and production config is the host. For production I use an existing url, on my local machine it works ok but when I try it on the server I got this error:
Net::SMTPSyntaxError (504 5.7.4 Unrecognized authentication type)
I have had this error before on my local machine but I changed the authentication to :ntml and it works fine.
UPDATE
tried in server console (code from ntlm gem github page)
require 'ntlm/smtp'
smtp = Net::SMTP.new('smtp.example.com')
smtp.start('localhost.localdomain', 'Domain\\User', 'Password', :ntlm) do |smtp|
smtp.send_mail(mail_body, from_addr, to_addr)
end
but have the same error

Sending email through Rails 3 with gmail problems

I am getting this error whilst trying to send emails through gmail -
Net::SMTPAuthenticationError (530 5.7.0 Must issue a STARTTLS command first. pw17sm4922458lab.5
):
app/controllers/contact_controller.rb:11:in `create'
I have tried loads of different things but to no avail, below is my settings from production.rb
# Change mail delvery to either :smtp, :sendmail, :file, :test
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "bizmodev.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: '******.*****#gmail.com',
password: '********'
}
# Specify what domain to use for mailer URLs
config.action_mailer.default_url_options = {host: "bizmo.co.uk"}
Any help on this matter would really be appreciated.
OK fixed it, basically I installed sendmail on my VPS and then restarted Apache and it now works -
Ubuntu Sendmail command line install
apt-get install sendmail
Hope this may help someone in the future...
you can enable starttls like this:
config.action_mailer.smtp_settings = {
....
:enable_starttls_auto => true}

Resources