1.在rails环境配置相关文件中添加配置,可以是enviroment/development production test也可以是config/application.rb,应该是只要是在rails启动配置中配置都可以的
我的是在 enviroment/development.rb
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.163.com",
:port => 25,
:domain => "163.com",
:authentication => :login,
:user_name => "xxxxxx@163.com",
:password => "密码"
}
config.action_mailer.default_options = {
from: "xxxxxx@163.com"
}
创建一个mailer的class文件,只要继承ActionMailer::Base就可以了,一般的做法是和controller一样,写个application_mailer来继承ActionMailer,然后在给自己写的类继承application_mailer
3.定义发送邮件的方法
class MailerLong <ApplicationMailer
def send_mail(email)
mail(to: email,subject: '测试邮件',date: Time.now)
end
end
4.创建邮件的视图文件,在views下,创建mailer_longs/send_mail.html.erb,可以先随便写的文字在里面,用于测试
5.发送的配置已经完毕,rails c打开控制台,调用MailerLong.send_mail('xxxxxxxx@qq.com')试一下,就可以发送邮件了