诗歌rails 之使用Rails Action Mailer发送SMTP邮件 基础教程

链接如果,你是在找
553 You are not authorized to send mail, authentication is required 这个问题的原因,请跳这里,Rails smtp 邮件出错时

Action Mailer
Rails的一个组件用来发送接收邮件,下面将演示,如何使用它创建SMTP邮件。从命令创建Rails工程开始:

Java代码
  1. C:\ruby\> rails emails  


Action Mailer 配置

smtp邮件发送,首先需要配置,在config的environment.rb最后
添加
Java代码
  1. ActionMailer::Base.delivery_method = :smtp  


这个配置是指定使用smtp,下面具体的使用参数:
也要在environment.rb中配置
Java代码
  1. ActionMailer::Base.server_settings = {  
  2.    :address => "smtp.tutorialspoint.com",  
  3.    :port => 25,  
  4.    :domain => "tutorialspoint.com",  
  5.    :authentication => :login,  
  6.    :user_name => "username",  
  7.    :password => "password",  
  8. }  


以上配需要根据实际情况写,端口验证方式和发送类型。同样,配置也可以是

Java代码
  1. ActionMailer::Base.default_content_type = "text/html"#其中type可以是"text/plain""text/html", 和 "text/enriched"."text/plain"是默认  


接着是生成mailer命令如下:
Java代码
  1. C:\ruby\> cd emails  
  2. C:\ruby\emails> ruby script/generate mailer Emailer  

生成文件:
Java代码
  1. class Emailer < ActionMailer::Base  
  2. end  


我们需要在添加方法:

Java代码
  1. class Emailer < ActionMailer::Base  
  2.    def contact(recipient, subject, message, sent_at = Time.now)  
  3.       @subject = subject  
  4.       @recipients = recipient  
  5.       @from = 'no-reply@yourdomain.com'  
  6.       @sent_on = sent_at  
  7.       @body["title"] = 'This is title'  
  8.       @body["email"] = 'sender@yourdomain.com'  
  9.       @body["message"] = message  
  10.       @headers = {}  
  11.    end  
  12. end  

其中, recipient, subject, message 和 sent_at是关于发送的参数,同时我们还有六个标准参数
引用

    *@subject主题.
    * @body 是Ruby的hash结构。你可以创建三个值对title, email, message
    * @recipients接受邮件的地址列表
    * @from发件人地址列表.
    * @sent_on发送时间.
    * @headers是另外的hash结构标识header信息如,配置MIME typeplain text 或 HTML


然后,创建发送模板

修改如下文件app/views/contact.rhtml
Html代码
  1. Hi!  
  2.   
  3. You are having one email message from <%= @email %> with a tilte   
  4.   
  5. <%= @title %>  
  6. and following is the message:  
  7. <%= @message %>  
  8. Thanks  



创建对应controller处理逻辑
Java代码
  1. C:\ruby\emails> ruby script/generate controller Emailer  

在 emailer_controller.rb中调用上面创建的Model实现发送逻辑:
Java代码
  1. class EmailerController < ApplicationController  
  2.    def sendmail  
  3.       email = @params["email"]  
  4.       recipient = email["recipient"]  
  5.       subject = email["subject"]  
  6.       message = email["message"]  
  7.       Emailer.deliver_contact(recipient, subject, message)  
  8.       return if request.xhr?  
  9.       render :text => 'Message sent successfully'  
  10.    end  
  11. end  


发送邮件,需要添加deliver_到发送的对应方法前。
request.xhr?是用阿里处理Rails Java Script(RJS)当浏览器不支持JavaScript时可以发送text信息。


下面是发送内容的输入界面,首先在emailer_controller.rb添加相应方法
Java代码
  1. def index  
  2.    render :file => 'app\views\emailer\index.rhtml'  
  3. end  


在app\views\emails\index.rhtml文件中设计输入界面

Html代码
  1. <h1>Send Email</h1>  
  2. <%= start_form_tag :action => 'sendmail' %>  
  3. <p><label for="email_subject">Subject</label>:  
  4. <%= text_field 'email', 'subject' %></p>  
  5. <p><label for="email_recipient">Recipient</label>:  
  6. <%= text_field 'email', 'recipient' %></p>  
  7. <p><label for="email_message">Message</label><br/>  
  8. <%= text_area 'email', 'message' %></p>  
  9. <%= submit_tag "Send" %>  
  10. <%= end_form_tag %>  


然后,http://127.0.0.1:3000/Emailer/inde页面测试
如下:



点击发送,就应该看到成功提示
posted @ 2009-08-11 15:02  麦飞  阅读(874)  评论(0编辑  收藏  举报