简体   繁体   English

ActionMailer不在开发Rails 4中发送邮件

[英]ActionMailer not sending mail in development Rails 4

Why is this mailer not sending any mail? 为什么这封邮件不发送任何邮件? (Or any ideas for debugging?) (或任何调试的想法?)

In my_app/config/environments/development.rb I have this code: 在my_app / config / environments / development.rb中我有这样的代码:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'my_app.com',
    user_name:            ENV['GMAIL_USERNAME'],
    password:             ENV['GMAIL_PASSWORD'],
    authentication:       'plain',
    enable_starttls_auto: true  }

Then on my local computer in ~/.bash_profile I have this code: 然后在〜/ .bash_profile的本地计算机上我有这个代码:

export GMAIL_USERNAME='blah@my_app.com'
export GMAIL_PASSWORD='***'

When I run $ env in my terminal, I see that both environment variables are correctly set. 当我在终端中运行$ env时,我发现两个环境变量都已正确设置。

I have also restarted my rails server. 我也重新启动了我的rails服务器。

You should add 你应该添加

config.action_mailer.perform_deliveries = true

as by default this is on false, preventing mails to be sent from your development environment... 默认情况下,这是假的,防止从您的开发环境发送邮件...

对于没有使用smtp的人,将交付方法切换到sendmail除了明确设置要执行的交付之外,还帮助我:

config.action_mailer.delivery_method = :sendmail

如果您在从控制台发送电子邮件时遇到问题,则必须在邮件上调用发送方法。

MyMailer.create_email.deliver

All of these answers are great, but there is another place where you can get burned, especially in the context of debugging. 所有这些答案都很棒,但还有另一个地方可以让你被烧毁,特别是在调试环境中。

In development.rb make sure you set config.action_mailer.raise_delivery_errors = true development.rb确保设置config.action_mailer.raise_delivery_errors = true

If your .deliver method seems to be working without issue, but you never actually receive the email across the wire, your delivery method may be throwing an exception and rails is swallowing the error. 如果您的.deliver方法似乎没有问题,但您实际上从未通过网络实际收到电子邮件,那么您的传递方法可能会抛出异常而且rails正在吞噬错误。 This is very true if you simply have something as simple as a misconfigured credentials, or an aws access denied API error. 如果您只是简单地使用错误配置的凭据或aws访问被拒绝的API错误,那么这是非常正确的。 Save ripping your hair out and make sure you have raise_delivery_errors turned on. 保存撕开你的头发并确保你打开了raise_delivery_errors It wants to tell you something but can't. 它想告诉你一些事情,但不能。

So I've figured it out. 所以我已经明白了。 Having the line ActionMailer::Base.delivery_method = :smtp in config/environment.rb overrides ActionMailer::Base.delivery_method = :test in config/environments/test.rb . config/environment.rb使用行ActionMailer::Base.delivery_method = :smtp会在config/environments/test.rb覆盖ActionMailer::Base.delivery_method = :test

So, delete that line, ActionMailer::Base.delivery_method = :smtp from config/environment.rb and place it in config/environments/production.rb . 因此,从config/environment.rb删除该行, ActionMailer::Base.delivery_method = :smtp ,并将其放在config/environments/production.rb That allows you to place ActionMailer::Base.delivery_method = :test in config/environments/test.rb and the version you want in config/environments/development.rb . 这允许您在config/environments/test.rb放置ActionMailer::Base.delivery_method = :test以及config/environments/development.rb I made development.rb :test as I populated my database using Faker and changed it to :smtp so I was sure that real emails were sent as an additional check. 我做了development.rb :test因为我使用Faker填充了我的数据库并将其更改为:smtp所以我确信真正的电子邮件是作为附加检查发送的。

Note: You must restart your server for these changes to take effect. 注意:必须重新启动服务器才能使这些更改生效。

Another note: Heroku's current SendGrid Instructions ( https://devcenter.heroku.com/articles/sendgrid ) put the SendGrid Heroku configuration code in a new config/initializers/mail.rb file which will likely require removing its last line and placing the desired version in each config/environments/[production.rb, development.rb, test.rb] 另一个注意事项:Heroku当前的SendGrid指令( https://devcenter.heroku.com/articles/sendgrid )将SendGrid Heroku配置代码放入新的config/initializers/mail.rb文件中,这可能需要删除其最后一行并放置每个config/environments/[production.rb, development.rb, test.rb]所需版本config/environments/[production.rb, development.rb, test.rb]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM