【发布时间】: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