Sendgrid未在Heroku生产Rails应用程序上发送电子邮件

时间:2019-02-16 07:03:33

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

我正在使用Devise进行用户身份验证,使用SendGrid发送电子邮件,并使用Heroku托管我的Rails应用。

问题在于,当用户注册时,SendGrid应该发送确认电子邮件;在用户确认后,它应该发送欢迎电子邮件,但尽管我可以看到Heroku正在通过以下方式发送电子邮件,但SendGrid没有发送这些电子邮件: Heroku日志。

mailers/welcome_email.rb

class WelcomeEmail < ApplicationMailer

    def welcome_email(user)
        @user = user
        mail to: @user.email, subject: "Thanks for joining!", from: "info@app.com"
    end

end

models/user.rb

class User < ApplicationRecord
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :validatable, :confirmable

    def after_confirmation
        WelcomeEmail.welcome_email(self).deliver
    end

end

views/welcome_email.html.erb

<h1>This is the welcome email!</h1>

config/environments/development.rb

Rails.application.configure do

  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

end

config/environments/production.rb

Rails.application.configure do
  # for emails to go out
  config.action_mailer.default_url_options = { host: 'appname.herokuapp.com', protocol: 'https' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.smtp_settings = {
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :domain => 'herokuapp.com',
    :address => 'smtp.sendgrid.net',
    :port => 587,
    :authentication => :plain,
    :enable_starttls_auto => true
  }
end

Heroku日志

INFO: Rendering devise/mailer/reset_password_instructions.html.erb
INFO: Rendered devise/mailer/reset_password_instructions.html.erb
DEBUG: Devise::Mailer#reset_password_instructions: processed outbound mail
INFO: Sent mail to email@gmail.com
DEBUG: Date: 16 Feb 

From: info@app.com
Reply-To: info@app.com
To: email@gmail.com
Subject: Reset password
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
...
Hello user!
...

INFO: Redirected to https://app.herokuapp.com
Completed 302 Found in 267ms

我到处都是SO,博客文章,视频和指南,但都没有成功。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您要立即发送电子邮件吗?尝试使用deliver.now代替deliver