Actionmailer不会在开发中发送电子邮件,即使他们在控制台中说“已发送”。
gemlist:
gem 'send_grid'
development.rb:
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => "localhost:3000" }
为setup_mail.rb:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => '587',
:domain => "heroku.com",
:user_name => "app123456789@heroku.com",
:password => "asdfasdfasdf",
:authentication => :plain,
:enable_starttls_auto => true
}
应用程序/邮寄者/ user_mailer.rb:
class UserMailer < ActionMailer::Base
include SendGrid
def registration_confirmation(user)
@user = user
mail(:to => user.email, :subject => "Registered", :from => "media@myemail.com")
end
end
应用程序/视图/ user_mailer文件/ registration_confirmation.html.haml:
Confirm your email address please!
= accept_invitation_users_url(email_token: @user.email_token)
控制台:
Sent mail to alain@myemail.com (656ms)
Date: Tue, 11 Feb 2014 13:35:11 -0500
From: media@myemail.com
To: alain@myemail.com
Message-ID: <52fa6d5f667ea_27ce5564820279f3@Ice-box.mail>
Subject: Registered
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
X-SMTPAPI: {}
Confirm your email address please!
http://localhost:3000/users/accept_invitation?email_token=_JL8DQ2GItxl
Redirected to http://localhost:3000/
Completed 302 Found in 1396ms (ActiveRecord: 192.2ms)
答案 0 :(得分:1)
您可以尝试设置:
config.action_mailer.raise_delivery_errors = true
看看它是否会在交货时出现任何错误。否则,请尝试使用其他电子邮件或在垃圾文件夹中查看
或者在开发模式下尝试检查是否存在任何问题(看起来很好,因为您可以在控制台中看到确认)
答案 1 :(得分:1)
将config.action_mailer.raise_delivery_errors = true
添加到production.rb或development.rb并重新启动您的应用以使更改正常工作
答案 2 :(得分:0)
将它放在smtp设置之前:
config.action_mailer.raise_delivery_errors = true
在您调用电子邮件方法的末尾添加“.deliver”,即:
UserMailer.welcome_email(@user).deliver
它会引发异常,帮助您找到失败原因。
如果您使用后台作业发送电子邮件,您可以将此代码段放在例外区块中并使用ExceptionNotification gem发送电子邮件,其中包含有助于您轻松捕获并找到失败原因的详细信息。