【问题标题】:Sendgrid not sending email on Heroku production Rails AppSendgrid 未在 Heroku 生产 Rails 应用程序上发送电子邮件
【发布时间】:2019-02-16 07:03:33
【问题描述】:

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

问题是 SendGrid 应该在用户注册时发送确认电子邮件,并在用户确认后发送欢迎电子邮件,但是 SendGrid 没有发送这些电子邮件,尽管我可以看到 Heroku 正在通过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、博客文章、视频和指南,但没有任何成功。有人可以帮帮我吗?

【问题讨论】:

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


    【解决方案1】:

    您想立即发送电子邮件吗?试试deliver.now 而不是deliver

    【讨论】:

    • 不,我正在尝试先发送确认电子邮件,然后在用户确认后发送欢迎电子邮件。问题是电子邮件不会使用 SendGrid 发送出去。这在我的本地机器(localhost:3000)上使用 mailcatcher 工作正常。
    • @Richard 你找到解决方案了吗?
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多