$ rails g mailer welcome Running via Spring preloader in process 59807 create app/mailers/welcome_mailer.rb invoke erb create app/views/welcome_mailer invoke test_unit create test/mailers/welcome_mailer_test.rb create test/mailers/previews/welcome_mailer_preview.rb
打开 app/mailers/application_mailer.rb 文件,替换 default from 后面的参数为发件人邮箱,如:
1 2 3 4
classApplicationMailer < ActionMailer::Base default from:'xxxxxxxx@qq.com' layout 'mailer' end
打开 app/mailers/welcome_mailer.rb 文件,编辑收件人和邮件主题,如:
1 2 3 4 5 6
classWelcomeMailer < ApplicationMailer defwelcome_mail(member) @member = member mail to:@member.email, subject:'Welcome to My Awesome Site' end end
在 app/views/welcome_mailer 文件夹中创建 welcome_mailer.html.erb 文件,使用 html 来编写邮件的视图。如下:
1 2 3
<h1>Welcome to example.com, <%=@member.username %></h1> <h3>You have successfully signed up to example.com</h3> <h3>Thanks for joining and have a great day!</h3>