我正在开发一个Rails应用程序,当新用户注册时,该应用程序应该发送一封确认电子邮件。
我的邮件程序如下:
def notify_email(volunteer)
@volunteer= volunteer
@admin = Volunteer.first
mail(to: @admin.email, subject: 'New Application from ' + volunteer.first_name+ '!', body:
'You have new applicants, please go check on your admin dashboard!
You can contact ' + @volunteer.first_name+ ' at: ' +@volunteer.email+ '.')
end
end
在我的production.rb文件中,我的电子邮件配置设置如下(用x代替gmail用户名/密码):
config.action_mailer.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost:8080' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => 'xxxxxx',
:password => 'xxxxxxx',
:authentication => "plain",
:enable_starttls_auto => true
}
我的控制器具有以下代码:
def create
@admin= Volunteer.first
@volunteer = Volunteer.create(volunteer_params)
@volunteer.save
if @volunteer.save
flash[:success] = "Successfully Registered!"
#email notifes admin of new registration
NotifyMailer.notify_email(@volunteer).deliver_now
redirect_to '/volunteer'
end
最后,当我在本地服务器上测试该应用程序时,控制台会向我提供此信息(再次向x发送电子邮件):
NotifyMailer#notify_email: processed outbound mail in 1.5ms
Sent mail to xxxx@xxxx.org (2004.5ms)
Date: Sun, 06 Jan 2019 16:04:49 -0600
From: xxxxxxx@gmail.com
To: xxxxx@xxxxx.org
Message-ID: <5c327b817c32e_3de83ca21e89202@LAPTOP-N2MQ7EL0.mail>
Subject: New Application from Rachel!
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
任何人都可以让我深入了解我所缺少的内容吗?我检查了几个论坛和主题,似乎我在遵循正确的步骤。
先谢谢了。
答案 0 :(得分:1)
无法发表评论,但是如果您致电
NotifyMailer.notify_email(@volunteer).deliver
代替
NotifyMailer.notify_email(@volunteer).deliver_now
我相信liver_now会像sidekiq这样的后台工作者进行中继, 再试试 gem "letter_opener",这将在浏览器中打开一个新标签来检查您的电子邮件。