Ruby-on-rails ActionMailer 在开发 Rails 4 中不发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20769589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:09:51  来源:igfitidea点击:

ActionMailer not sending mail in development Rails 4

ruby-on-railsruby-on-rails-4actionmailer

提问by Don P

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 $ envin my terminal, I see that both environment variables are correctly set.

当我$ env在终端中运行时,我看到两个环境变量都已正确设置。

I have also restarted my rails server.

我也重新启动了我的 rails 服务器。

回答by Danny

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

默认情况下,这是错误的,防止从您的开发环境发送邮件......

回答by Aaron Krauss

For anyone not using smtp, switching the delivery method to sendmail helped me in addition to explicitly setting deliveries to be performed:

对于不使用 smtp 的任何人,除了明确设置要执行的交付之外,将交付方式切换为 sendmail 对我也有帮助:

config.action_mailer.delivery_method = :sendmail

回答by Michael Brawn

If you're having issues sending email from console, you have to call the deliver method on your mail.

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

MyMailer.create_email.deliver

回答by Ryan Romanchuk

All of these answers are great, but there is another place where you can get burned, especially in the context of debugging.

所有这些答案都很棒,但还有另一个地方可能会让您感到厌烦,尤其是在调试的情况下。

In development.rbmake sure you set config.action_mailer.raise_delivery_errors = true

development.rb确保你设置config.action_mailer.raise_delivery_errors = true

If your .delivermethod 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. This is very true if you simply have something as simple as a misconfigured credentials, or an aws access denied API error. Save ripping your hair out and make sure you have raise_delivery_errorsturned on. It wants to tell you something but can't.

如果您的.deliver方法似乎没有问题,但您实际上从未通过网络收到电子邮件,则您的交付方法可能会引发异常,而 rails 正在吞下错误。如果您只是有一些像错误配置的凭据或 aws 访问被拒绝的 API 错误这样简单的事情,那么这是非常正确的。保存撕掉你的头发并确保你已经raise_delivery_errors打开。它想告诉你一些事情,但不能。

回答by DJSampat

So I've figured it out. Having the line ActionMailer::Base.delivery_method = :smtpin config/environment.rboverrides ActionMailer::Base.delivery_method = :testin config/environments/test.rb.

所以我想通了。具有行ActionMailer::Base.delivery_method = :smtpconfig/environment.rb覆盖ActionMailer::Base.delivery_method = :testconfig/environments/test.rb

So, delete that line, ActionMailer::Base.delivery_method = :smtpfrom config/environment.rband place it in config/environments/production.rb. That allows you to place ActionMailer::Base.delivery_method = :testin config/environments/test.rband the version you want in config/environments/development.rb. I made development.rb:testas I populated my database using Faker and changed it to :smtpso I was sure that real emails were sent as an additional check.

因此,删除该行,ActionMailer::Base.delivery_method = :smtpfromconfig/environment.rb并将其放入config/environments/production.rb. 这可以让你的地方ActionMailer::Base.delivery_method = :testconfig/environments/test.rb和你想要的版本config/environments/development.rb。我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.rbfile 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]