rails4.2.6配置发送邮件

时间:2023-03-10 01:05:45
rails4.2.6配置发送邮件

调试了很久,最后终于可以发送了

1 在config/environments/development.rb文件里配置邮件信息

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目录下

 class ApplicationMailer < ActionMailer::Base
default from: "邮箱地址" #这里最好和上面配置文件中你的邮箱账户一样,否则可能有奇怪问题
layout 'mailer'
end

3 创建一个实际业务逻辑类

class OrderNotifier < ApplicationMailer

  def received

    mail to: "收件人地址", subject: 'Pragmatic Store Order Confirmation'
end end

4 view/order_notifier目录下创建对应方法模板,例如 received.text.rb

 <h1>OrderNotifier#received</h1>

 <p>
test received email
</p>

5 action中调用邮件类发送邮件

OrderNotifier.received.deliver_now

具体可以看 agile web development with rails4 第五版,177页详细介绍,这里只做摘记