rails 发送电子邮件(ActionMailer)

来源:互联网 发布:淘宝阴氏埙哪里买 编辑:程序博客网 时间:2024/05/08 04:07


ActionMailer  在后台发送邮件

1.创建mailer

[ruby] view plaincopy
  1. rails g mailer UserMailer  

2.编辑app/mailer/user_mailer

[ruby] view plaincopy
  1. class UserMailer < ActionMailer::Base  
  2.   default from: "xxxxxxxx@163.com"  
  3.   
  4.   # def confirm(email)  
  5.   #   subject    "激活'我的生活'账户"  
  6.   #   recipients email  
  7.   #   from       'xxxxxxxx@163.com'  
  8.   #   sent_on    Time.now  
  9.   
  10.   #   body       "欢迎加入‘我的生活’,我们致力于更加方便的生活,请点击激活账户"  
  11.   # end  
  12.   
  13.   # def welcome_email(user)  
  14.   #   @user = user  
  15.   #   @url  = 'http://example.com/login'  
  16.   #   mail(to: 'xxxxxxxx@163.com', subject: 'Welcome to My Awesome Site')  
  17.   # end  
  18.   
  19.   def send_mail(params = {})  
  20.     @url  = 'http://example.com/login'  
  21.     mail( :subject => 'abcAAAAAAAASDFADSFADSFADSFDASFASDF',   
  22.           :to => "xxxxxxxx@163.com",   
  23.           :from => 'xxxxxxxx@163.com',   
  24.           :date => Time.now  
  25.         )   
  26.   end   
  27. end  

3.配置action mailer了,由于国内的gmail连接老出现问题,所以这里就介绍163 environments/development.rb

[ruby] view plaincopy
  1. ActionMailer::Base.delivery_method = :smtp  
  2.   config.action_mailer.perform_deliveries = true  
  3.   config.action_mailer.raise_delivery_errors = true  
  4.   config.action_mailer.default :charset => "utf-8"  
  5.   config.action_mailer.default_url_options = { :host => 'localhost:3000' }  
  6.   ActionMailer::Base.smtp_settings = {    
  7.     :address => "smtp.163.com",  
  8.     :port => 25,  
  9.     :domain => "163.com",  
  10.     :authentication => :login,  
  11.     :user_name => "mxbeijingmi@163.com",  
  12.     :password => "menxu0725"  
  13.   }  

4.发送邮件

[ruby] view plaincopy
  1. UserMailer.send_mail(nil).deliver  

5.官方文档: http://guides.rubyonrails.org/action_mailer_basics.html


http://guides.rubyonrails.org/action_mailer_basics.html
0 0