调试了很久,最后终于可以发送了
1 在config/environments/development.rb文件里配置邮件信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "smtp.163.com", :port => 25, :domain => '163.com', :user_name => '你的邮箱登陆账号',#例如 xxx@163.com :password => '你的密码', :authentication => :login, }
2 创建一个基础类 放在 app/mailers目录下
1 class ApplicationMailer < ActionMailer::Base 2 default from: "邮箱地址" #这里最好和上面配置文件中你的邮箱账户一样,否则可能有奇怪问题 3 layout 'mailer' 4 end
3 创建一个实际业务逻辑类
class OrderNotifier < ApplicationMailer def received mail to: "收件人地址", subject: 'Pragmatic Store Order Confirmation' end end
4 view/order_notifier目录下创建对应方法模板,例如 received.text.rb
1 <h1>OrderNotifier#received</h1> 2 3 <p> 4 test received email 5 </p>
5 action中调用邮件类发送邮件
OrderNotifier.received.deliver_now
具体可以看 agile web development with rails4 第五版,177页详细介绍,这里只做摘记