从Rails发送电子邮件可以在开发中使用,而不是在Heroku上进行生产

时间:2012-08-17 17:43:18

标签: ruby-on-rails heroku devise actionmailer activeadmin

我正在尝试使用gem Active Admin向我注册的用户发送电子邮件,以便他们可以创建密码。

这需要在config / environments / development.rb

上插入以下代码
 #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          => 'XXX@gmail.com',
   :password           => 'XXXX'
 }

这没有问题

用于部署到Heroku上的生产站点。我将以下代码插入config / environments / production.rb

 #Added per active admin install instructions
 config.action_mailer.default_url_options = { :host => 'http://XXXX.herokuapp.com/' }

 #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          => 'XXX@gmail.com',
   :password           => 'XXX'
 }

但现在电子邮件不会被发送。相反,我在浏览器中看到“我们很遗憾soemthing出错”消息并且日志说明以下行

2012-08-17T17:39:34+00:00 app[web.1]:     cache: [GET /admin/admin_users/new] miss

2012-08-17T17:39:34+00:00 app[web.1]: Started POST "/admin/admin_users" for   96.49.201.234 at 2012-08-17 17:39:34 +0000
2012-08-17T17:39:35+00:00 app[web.1]: Net::SMTPAuthenticationError (535-5.7.1 Please log   in with your web browser and then try again. Learn more at

2012-08-17T17:39:35+00:00 app[web.1]: ):
2012-08-17T17:39:35+00:00 app[web.1]:   app/models/admin_user.rb:35:in `block in <class:AdminUser>'

2012-08-17T17:39:35+00:00 app[web.1]: cache: [POST /admin/admin_users] invalidate, pass

我应该从哪里出发?有人可以帮我一把吗

4 个答案:

答案 0 :(得分:1)

以下是Gmail提供的有用帮助:http://support.google.com/mail/bin/answer.py?hl=en&answer=14257&p=client_login

问题是Gmail会阻止可能是机器人的可疑登录尝试。我们需要向该应用授予权限,以便其可以使用Google帐户发送电子邮件。

答案 1 :(得分:0)

Gmail要求与其邮件服务器建立SSL连接。尝试将此添加到您的SMTP设置:

:enable_starttls_auto => true

答案 2 :(得分:0)

请尝试以下代码:

 ActionMailer::Base.smtp_settings = 
 {

   :address              => 'smtp.gmail.com',
   :port                 => 587,
   :domain               => 'gmail.com', #you can also use google.com
   :authentication       => 'plain',
   :user_name            => 'XXX@gmail.com',
   :password             => 'XXX',
   :enable_starttls_auto => true
 }

所做的更改:
1. :authentication => :action:authentication => 'plain'
2.添加:enable_starttls_auto => true,由上面的janders223评论。

答案 3 :(得分:0)

http://www.google.com/accounts/DisplayUnlockCaptcha点击此处并授予其访问权限。下次它会工作