诗歌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  
C:\ruby\> rails emails


Action Mailer 配置

smtp邮件发送,首先需要配置,在config的environment.rb最后
添加
Java代码
  1. ActionMailer::Base.delivery_method = :smtp  
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. }  
ActionMailer::Base.server_settings = {
   :address => "smtp.tutorialspoint.com",
   :port => 25,
   :domain => "tutorialspoint.com",
   :authentication => :login,
   :user_name => "username",
   :password => "password",
}


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

Java代码
  1. ActionMailer::Base.default_content_type = "text/html"#其中type可以是"text/plain""text/html", 和 "text/enriched"."text/plain"是默认  
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  
C:\ruby\> cd emails
C:\ruby\emails> ruby script/generate mailer Emailer

生成文件:
Java代码
  1. class Emailer < ActionMailer::Base  
  2. end  
class Emailer < ActionMailer::Base
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  
class Emailer < ActionMailer::Base
   def contact(recipient, subject, message, sent_at = Time.now)
      @subject = subject
      @recipients = recipient
      @from = 'no-reply@yourdomain.com'
      @sent_on = sent_at
      @body["title"] = 'This is title'
        @body["email"] = 'sender@yourdomain.com'
         @body["message"] = message
      @headers = {}
   end
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  
Hi!

You are having one email message from <%= @email %> with a tilte 

<%= @title %>
and following is the message:
<%= @message %>
Thanks



创建对应controller处理逻辑
Java代码
  1. C:\ruby\emails> ruby script/generate controller Emailer  
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  
class EmailerController < ApplicationController
   def sendmail
      email = @params["email"]
      recipient = email["recipient"]
      subject = email["subject"]
      message = email["message"]
      Emailer.deliver_contact(recipient, subject, message)
      return if request.xhr?
      render :text => 'Message sent successfully'
   end
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  
   def index
      render :file => 'app\views\emailer\index.rhtml'
   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 %>  
<h1>Send Email</h1>
<%= start_form_tag :action => 'sendmail' %>
<p><label for="email_subject">Subject</label>:
<%= text_field 'email', 'subject' %></p>
<p><label for="email_recipient">Recipient</label>:
<%= text_field 'email', 'recipient' %></p>
<p><label for="email_message">Message</label><br/>
<%= text_area 'email', 'message' %></p>
<%= submit_tag "Send" %>
<%= end_form_tag %>


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



点击发送,就应该看到成功提示

转载于:https://www.cnblogs.com/orez88/articles/1543648.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值