Devise mailer is not sending mails in production environment - ruby-on-rails

I have set up a rails application on a company internal network. I use Devise::Mailer for registration confirmation, password recovery, etc. I also have my own mailer class derived from ActionMailer::Base. In a development environment both type of mailers work fine, but on a production environment only my custom mailer seems to work.
I both environments the mail configuration is the same:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: 'my.internal.ip.address', port: 80 }
config.action_mailer.asset_host = "http://my.internal.ip.address"
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => 'internal.mail.server',
:port => 25,
:domain => 'domain.name'
}
This SMTP server does not require authentication.
In the config/initializers/devise.rb I have set the sender address like this:
config.mailer_sender = "myaddress#domain.name"
I have tried to set the config.action_mailer.delivery_method to :file in the production environment. In this case mails sent from my custom mailer are written into files, but mails sent from the Devise mailer are not.
How can I make the Devise mailer work in production environment?
Update
I have copied the config/environments/production.rb file to config/environments/development.rb and it still works in development mode but still not in production mode.

All configurations you have set in Development mode will then work in production mode, you need to reconfigure the default mailer url - I can't imagine my.internal.ip.address is a valid host in production. You should be changing this to the domain name or the ip where the application code is hosted.

Related

ActionMailer acting different on production server

I have a action mailer method as something like this:
def mail
#receiver = User.where(status: 2).pluck(:email)
mail(bcc:#receiver, to: "username#gmail.com")
end
application.yml looks like this:
SMTP_ADDRESS: 'smtp.gmail.com'
SMTP_PORT: 587
SMTP_HOST: 'localhost:3000'
SMTP_DOMAIN: 'localhost:3000'
SMTP_USERNAME: 'user#gmail.com'
SMTP_PASSWORD: 'xxxxx'
SUPER_ADMIN_EMAIL: 'super_admin#mailinator.com'
developmet.rb looks like this:
config.action_mailer.asset_host = ENV["SMTP_HOST"]
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
#Enter the smtp provider here ex: smtp.mandrillapp.com
address: ENV["SMTP_ADDRESS"],
port: ENV['SMTP_PORT'].to_i,
#Enter the smtp domain here ex: vendaxo.com
domain: ENV["SMTP_DOMAIN"],
#Enter the user name for smtp provider here
user_name: ENV["SMTP_USERNAME"],
#Enter the password for smtp provider here
password: ENV["SMTP_PASSWORD"],
authentication: 'plain',
enable_starttls_auto: true
Where receivers are kept in BCC but "username#gmail.com" will be able to see the BCCd receivers. This is working fine when I send the mails from my local host. When receivers are sent emails, all of them are in BCC and one 'username#gmail.com' is able to see all the receivers.
But when I do the same thing on production server with similar application.yml configuration changing the host and port the username#gmail.com is NOT GETTING the emails of BCCd receivers.
I think if you are running your production server on AWS EC2 (may be other providers too), every time you do some changes to your application.yml or any other shared file, you need to restart the app server in order to apply the changed configurations . In my case I had to restart the PUMA, and the one default email started to get BCCd email ids.

Rails action_mailer config not working in production

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.

what is my heroku hostname? development is localhost:3000, what's production name?

Setting up a mailer. It works in localhost, however, it doesn't send the email, just gives me the message in the log. My development.rb is:
config.action_mailer.default_url_options = { :host => "localhost:3000" }
I tried this for my production.rb but it didn't work.
config.action_mailer.default_url_options = { :host => "http://cryptic-reef-1625.herokuapp.com/" }
I get an error when I hit the button that sends the email.
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...

How to set up mailer in Rails app for production environment on Heroku

I need to use a mailer for sending out emails to users to set their passwords to the "recoverable" function of Devise and active admin. On the development environment I have done this by adding the following to these files:
config/environments/development
#Added per active admin install instructions
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
#These settings are for the sending out email for active admin and consequently the devise mailer
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings =
{
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com', #you can also use google.com
:authentication => :plain,
:user_name => 'XXXXX#gmail.com',
:password => 'XXXXXXX'
}
How do I get the same functionality for the production environment? I want to deploy my app to Heroku. What files and code would I need to add?
All configurations you have set in Development mode will work EXCEPT you will need to reconfigure the default mailer url.
So.
Copy-paste your settings from development.rb.
Point your default mailer to your heroku app:
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
Also, be careful of any email limits your smtp may have when moving to production. It's hard to trigger gmail's smtp limits while developing, for example, but they could be more easily triggered in production.
If it works in development mode, then it will work in production mode.
Supposing everything is setup correctly, resetting a password in development will already send an actual email using your gmail account.
Devise only relies on the mailer config setup correctly (which you have done), and configuring devise to allow password reset, and possibly another setting for the From field of the email.
This should work fine!
As long as config/environments/production.rb has the same thing with an exception. The default_url_options should have a :host value of 'localhost' only in development and 'YOURAPPNAME.herokuapp.com' in heroku production.
i.e.
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
Remember to unlock captcha on gmail, otherwise it won't send email from heroku (unknown source). You can do that by going to this link: http://www.google.com/accounts/DisplayUnlockCaptcha
Just as a suggestion, I'd say move this from environments.rb
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
and place is in environments/development.rb as
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
It's not needed in production.
See Net::SMTPAuthenticationError when sending email from Rails app (on staging environment) for more information in regards to gmail seeing heroku as an unknown host.

in rails set config.action_mailer.default_url_options to whatever the current host of the app is?

How can I dynamically set the host in a config/environments/production.rb:
config.action_mailer.default_url_options = { :host => THE_HOST}
This is so the app works correctly when in staging and when in production. Our staging server is stage.app.com, and links need to go there.
In config/environments/production.rb do this:
config.action_mailer.default_url_options = { :host => 'app.com' }
and in config/environments/staging.rb do this:
config.action_mailer.default_url_options = { :host => 'stage.app.com' }
As #BrettBender commented:
You do not need to dynamically set the host. For an app in production, production.rb will be evaluated. For an app running in staging environment, rails will load the staging file automatically (same with development or any custom environments you define)
Add this to a before filter in application_controller:
ActionMailer::Base.default_url_options = {:host => request.host_with_port}

Resources