ActionMailer发送邮件最简单实例,163邮箱

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')试一下,就可以发送邮件了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值