Sending emails on Heroku using external SMTP

*爱你&永不变心* 提交于 2019-12-06 11:30:20

问题


I want to send emails from my rails application on Heroku. As Heroku doesn't support SMTP, I use external SMTP server.

config/environments/production.rb has the following lines.

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :address => 'mydomain.com',
  :port => 587,
  :user_name => "myusername",
  :password => "password",
  :authentication => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode => 'none' 
}

When I send an email from "heroku run console", it works fine. But it doesn't send email from the website. Strangely, "heroku logs --tail" shows "Sent mail to ...". Actually the email is not delivered.

Does anyone have any idea about this issue?

Thanks.

Sam Kong


回答1:


Have you tried this in config/enviornment.rb

ActionMailer::Base.smtp_settings = {
:address => 'mydomain.com',
:port => 587,
:user_name => "myusername",
:password => "password",
:authentication => :plain,
:enable_starttls_auto => true,
:openssl_verify_mode => 'none' 
}



回答2:


Your configuration is correct. But you have to put it in the correct environment file. In case of Heroku deployment, it should be 'production'. Console works because it uses 'development' as the environment. So put the following code in config/environment/production.rb

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :address => 'mydomain.com',
  :port => 587,
  :user_name => "myusername",
  :password => "password",
  :authentication => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode => 'none' 
}

Hope it solves your problem.




回答3:


If it shows as mail as being sent that I would suggest that it's a config issue at your mail server perhaps preventing relay from the Heroku IP range - it's most odd though if it works through the heroku console and not through your application.

Have you thought of trying the SendGrid Heroku addon just to alleviate any issues with your own mailer server?



来源:https://stackoverflow.com/questions/11754979/sending-emails-on-heroku-using-external-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!