使用Rails Action Mailer发送SMTP邮件 基础教程

链接如果,你是在找
[color=red]553 You are not authorized to send mail, authentication is required [/color]这个问题的原因,请跳这里,[url=http://hlee.iteye.com/admin/blogs/410348]Rails smtp 邮件出错时[/url]
[b]
Action Mailer[/b] 是Rails的一个组件用来发送接收邮件,下面将演示,如何使用它创建SMTP邮件。从命令创建Rails工程开始:

C:\ruby\> rails emails


[b]Action Mailer 配置[/b]

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


这个配置是指定使用smtp,下面具体的使用参数:
也要在environment.rb中配置

ActionMailer::Base.server_settings = {
:address => "smtp.tutorialspoint.com",
:port => 25,
:domain => "tutorialspoint.com",
:authentication => :login,
:user_name => "username",
:password => "password",
}


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

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


接着是生成mailer命令如下:

C:\ruby\> cd emails
C:\ruby\emails> ruby script/generate mailer Emailer

生成文件:


class Emailer < ActionMailer::Base
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是关于发送的参数,同时我们还有六个标准参数
[quote]
*@subject主题.
* @body 是Ruby的hash结构。你可以创建三个值对title, email, message
* @recipients接受邮件的地址列表
* @from发件人地址列表.
* @sent_on发送时间.
* @headers是另外的hash结构标识header信息如,配置MIME typeplain text 或 HTML[/quote]

然后,创建发送模板

修改如下文件app/views/contact.rhtml

Hi!

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

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



创建对应controller处理逻辑
C:\ruby\emails> ruby script/generate controller Emailer

在 emailer_controller.rb中调用上面创建的Model实现发送逻辑:


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添加相应方法

def index
render :file => 'app\views\emailer\index.rhtml'
end


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

<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页面测试
如下:

[img]/upload/attachment/109793/11659000-dce3-3915-b7fe-0fa15cb9e353.gif[/img]

点击发送,就应该看到成功提示
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值