简体   繁体   中英

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:

  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:

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.

I have also restarted my rails server.

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

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. 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_errors turned on. 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 .

So, delete that line, ActionMailer::Base.delivery_method = :smtp from config/environment.rb and place it in 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 . 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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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