1.配置smtp
# emial
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'userName', #不要写成了邮件,比如userName@gamil.com 这个写userName
:password => "password",
:authentication => 'plain',
:enable_starttls_auto => true
}
end
2.建mailer模型
在App/mailer下建一个comment_mailer.rb
default from: "userName@gmail.com"
#default from的设置一定要和你smtp的帐户相对应,否则可能出错。
def comment_notify_email
mail to: 'userName@gmail.com', subject: 'There is a new comment on your test'
end
class CommentsController < ApplicationController
def create
CommentMailer.comment_notify_email().deliver
end
end
CommentMailer.comment_notify_email().deliver #一定要加上deliver
如果发送失败,重新bundler一次.