在开发环境中提交表单后,应用程序会向我的电子邮件地址发送一封电子邮件。在生产环境中,Heroku日志报告正在发送电子邮件,但是当我检查邮箱时,电子邮件不在那里。这是我的配置:
application.yml
GMAIL_USERNAME: "christopherpelnar@gmail.com"
GMAIL_PASSWORD: "sevenmississippi"
user_mailer.rb
class UserMailer < ApplicationMailer
default from: "christopherpelnar@gmail.com"
def customer_email(message)
@message = message
mail(to: "christopherpelnar@gmail.com", subject: "Message from your website" )
end
end
messages_controller.rb
def create
@message = Message.new(message_params)
if @message.save
UserMailer.customer_email(@message).deliver_now
redirect_to '/message_sent'
else
redirect_to '/'
end
end
production.rb
config.action_mailer.default_url_options = { :host => 'stormy-wildwood-29407.herokuapp.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
Heroku记录
2016-05-22: UserMailer#customer_email: processed outbound mail in 238.2ms
2016-05-22: Sent mail to christopherpelnar@gmail.com (158.2ms)
2016-05-22:
2016-05-22: Date: Sun, 22 May 2016 09:43:29 +0000
2016-05-22: From: christopherpelnar@gmail.com
2016-05-22: To: christopherpelnar@gmail.com
2016-05-22: Message-ID: <17f4181751_3.mail>
2016-05-22: Subject: Message from your website
2016-05-22: Mime-Version: 1.0
2016-05-22: Content-Type: text/html;
2016-05-22: charset=UTF-8
2016-05-22: Content-Transfer-Encoding: 7bit
问题仅出在生产中,并且没有通过localhost:3000
发送的问题答案 0 :(得分:1)
问题出在Heroku的环境变量中,因为在创建Heroku应用程序后,环境变量的设置与 application.yml 文件中的设置不完全相同。例如,要设置用户名,您应该在控制台中运行此命令:
$ heroku config:add GMAIL_USERNAME=myname@gmail.com
有关详细信息,请访问:url
快乐的编码! ;)