使用rails收发邮件

第一步
    在environment.rb 加上

require  ' smtp_tls '      // 引用libsmtp_tls.rb
ActionMailer::Base.delivery_method  =  :smtp      // 使用smtp发送邮件

ActionMailer::Base.default_charset 
=   " UTF-8 "         // 指定发送邮件时使用的字符集

ActionMailer::Base.server_settings 
=  {
:address 
=>   " smtp.gmail.com " ,            // 使用的邮件服务器
:port  =>   587 ,                                             // 邮件服务器的端口号
:domain  =>   " xxx.com " ,                            // 暂时忽略
:authentication  =>  :login,                        // 不是很清楚,照着写
:user_name  =>   " long0428 " ,                   // 使用邮件服务器的帐号(这里是google,所以时goole邮箱的帐号)
:password  =>   " 63345133 " ,                      // 使用邮件服务器的密码(这里是google,所以时goole邮箱的密码)
// 注意:  这里我只是指定了邮件服务器,不是说我指定了google的邮件服务器,就非要使用google的邮箱发送邮件,也可以使用别的邮箱通过google的邮件服务器发送
}

到这一步我们的邮件就差不多配好了!开始第二步,创建一个OrderMailser的模型

 
class  OrderMailer  <  ActionMailer::Base      // 一定要继承ActionMailer::Base

  def confirm(sent_at 
=  Time.now)                       
    @subject    
=   ' helloWorld '                             // 主题
    @body        =  { ' name '   =>   ' dahsjkdh ' }          // 'name'是变量名,在confirm.rhtml里面声明
                                                                                如果
confirm.rhtml里面没有变量可以不需要@body这个参数
    @recipients  =   ' long0428@gmail.com '      // 接受人地址
    @from        =   ' liverpool_long@tom.com '     // 发送人地址
    @sent_on     =  sent_at                                  // 按时间发送sent_at = Time.now指明现在发送
    @headers     =  {}                                            // 用于指定头信息
  end

end

先别着急,我们现在来完成action   

  def send_mail
    OrderMailer.deliver_confirm()     
// deliver_confirm(这里的confirm是模型中的confirm方法,deliver是固                                                                                    定的写法)
    render :text =>   " scuuess! "
  end

现在来建一个rhtml,记住rhtml的名字也需要时confirm(action,模型,rhtml都是同步的)

HelloWorld
            这是第一次发送那个邮件!

最后别忘了,我们在lib文件夹上加上个libsmtp_tls.rb文件

require  " openssl "
require 
" net/smtp "

Net::SMTP.class_eval 
do
  
private
  def do_start(helodomain, user, secret, authtype)
    raise IOError, 
' SMTP session already started '   if  @started
    check_auth_args user, secret, authtype 
if  user or secret

    sock 
=  timeout(@open_timeout) { TCPSocket.open(@address, @port) }
    @socket 
=  Net::InternetMessageIO. new (sock)
    @socket.read_timeout 
=   60  #@read_timeout
    @socket.debug_output 
=  STDERR #@debug_output

    check_response(critical { recv_response() })
    do_helo(helodomain)

    raise 
' openssl library not installed '  unless defined ? (OpenSSL)
    starttls
    ssl 
=  OpenSSL::SSL::SSLSocket. new (sock)
    ssl.sync_close 
=   true
    ssl.connect
    @socket 
=  Net::InternetMessageIO. new (ssl)
    @socket.read_timeout 
=   60  #@read_timeout
    @socket.debug_output 
=  STDERR #@debug_output
    do_helo(helodomain)

    authenticate user, secret, authtype 
if  user
    @started 
=   true
  ensure
    unless @started
      # authentication failed, cancel connection.
        @socket.close 
if  not @started and @socket and not @socket.closed ?
      @socket 
=  nil
    end
  end

  def do_helo(helodomain)
     begin
      
if  @esmtp
        ehlo helodomain
      
else
        helo helodomain
      end
    rescue Net::ProtocolError
      
if  @esmtp
        @esmtp 
=   false
        @error_occured 
=   false
        retry
      end
      raise
    end
  end

  def starttls
    getok(
' STARTTLS ' )
  end

  def quit
    begin
      getok(
' QUIT ' )
    rescue EOFError
    end
  end
end
ok,大家现在可以发送邮件了.输入地址htto://localhost:3000/controllerName/ send_mail,rails就会把confirm.rhtml的内容发送到邮箱

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值