使用
ActionMaile
发送邮件
更改
config
目录下的配置文件
environment.rb
在最下面追加一段:
ActionMailer::Base.delivery_method =
:smtp
#
以简单邮件传送协议发送邮件
ActionMailer::Base.default_charset = "GBK"
#
设置邮件的默认编码为国标码
否则发送的邮件主题可能会乱码
ActionMailer::Base.server_settings = {
:address => "192.168.1.110",
:port =>
25,
:domain => "xxx.com",
:authentication =>
:login,
:user_name => "xxx",
:password => "xxx",
}
1
、
:address => and :port =>
决定你将使用的
SMTP
的地址和端口。这些缺省值分别为
localhost
和
25
。
2
、
:domain =>
当识别自己是服务器时
mailer
应该使用的域名。这是对
HELO(
因为
HELO
是命令客户端发送服务来启动一个连接
)
域的调用。你通常应该使用顶级域名机制来发送
e-mail
,但这依赖于你的
SMTP
服务的设置
(some don’t check, and some check to try to reduce spam and socalled open-relay issues)
3
、
:user_name => and :password =>
如果
:authentication
被设置则要求有此。
4
、
:authentication => :plain
,
:login
,或
:cram_md
中的一个。你的服务器管理员将帮助选择正确的选项。当前没使用
TLS(SSL)
来从
Rails
连接邮件服务器的方式。这个参数应该被忽略,如果你的服务器不要求确认。
创建一个
mailer
的
models
class OrderMailer < ActionMailer::Base
def signup(domain, sent_at = Time.now)
@subject
= 'Welcome to Beast'
@body
= "hello world"
@recipients = "[email protected]"
@from = '[email protected]'
@sent_on
= sent_at
@headers
= {}
end
end
@subject:
邮件标题
@body:
邮件正文
可以使用
html
标签
但需要设置
参考下面
@recipients:
收件人可以接收数组进行群发
多人发送:
@recipients = [ "[email protected]"
,
"
[email protected]
"]
@from:
发件人
@sent_on:
用于设置邮件
Date: header
的
Time
对象
@headers:
一个
header name/value
对的哈希望表,用于添加任意
header
行给邮件
如:
@headers["Organization"] = "Pragmatic Programmers, LLC"
既要使用
HTML
格式发送邮件又要增加附件的话,需要在
model
里就对
content-type
进行设置
@content-type=”text/html”
创建一个
controller
用于发送邮件
def send_mailer
email = OrderMailer.deliver_signup(request.host_with_port)
Puts email.encoded #邮件内容打印
#email = OrderMailer.create_signup(request.host_with_port)
#email.set_content_type("text/html") 可在模型中设置
#OrderMailer.deliver(email)
#发送HTML格式的邮件的设置
end
发送
HTML
模板邮件
在views中创建一个模板:_mail_content.rhtml
……
model中的mailer类改成如下:
def signup(domain,content,sent_at = Time.now)
@subject
= "xxx"
@body
= content
@recipients = "[email protected]"
@from = '[email protected]'
@sent_on
= sent_at
@headers
= {}
end
controller中更改发送方法:
def send_mail
content =
render_to_string
:partial=>" mail_content "
email = OrderMailer.create_signup(request.host_with_port,content)
email.set_content_type("text/html")
OrderMailer.deliver(email)
render :text=>"发送成功"
end
render_to_string方法返回的是String 与render不同的是它返回后不会发送给客户端。
发送附件
修改model中的mailer类,如下:
def signup(domain,content,sent_at = Time.now)
@subject
= "xxx"
@body
= content
@recipients = "[email protected]"
@from = '[email protected]'
@sent_on
= sent_at
@headers
= {}
@data = ""
File.open("D:\\Tools\\FastAIT.rar", "rb") { |fp|
@data<
".asf" ContentType = "video/x-ms-asf"
".avi" ContentType = "video/avi"
".doc" ContentType = "application/msword"
".zip" ContentType = "application/zip"
".xls" ContentType = "application/vnd.ms-excel"
".gif" ContentType = "image/gif"
".jpg", "jpeg" ContentType = "image/jpeg"
".wav" ContentType = "audio/wav"
".mp3" ContentType = "audio/mpeg3"
".mpg", "mpeg" ContentType = "video/mpeg"
".rtf" ContentType = "application/rtf"
".htm", "html" ContentType = "text/html"
".txt" ContentType = "text/plain"
}
#参数的含义rb表示只读并且以二进制方式创建一个file对象
#不写r会出现丢失数据的问题,发送的附件也就被破坏了
``r''
|
Read-only, starts at beginning of file (default mode).
只读,清除原有内容(默认方式)
|
``r+''
|
Read-write, starts at beginning of file.
读写,清除原有内容
|
``w''
|
Write-only, truncates existing file to zero length or creates a new file for writing.
只写,创建一个新的文件覆盖旧的
|
``w+''
|
Read-write, truncates existing file to zero length or creates a new file for reading and writing.
读写,创建一个新的文件覆盖旧的
|
``a''
|
Write-only, starts at end of file if file exists, otherwise creates a new file for writing.
只写,追加
|
``a+''
|
Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing.
读写,追加
|
``b''
|
(DOS/Windows only) Binary file mode (may appear with any of the key letters listed above).
二进制模式
|
attachment
:content_type => "application/rar",
:filename
=> "FastAIT.rar" ,
:body =>
@data
end
邮件附件的content_type(内容类型表)
".asf" ContentType = "video/x-ms-asf"
".avi" ContentType = "video/avi"
".doc" ContentType = "application/msword"
".zip" ContentType = "application/zip"
".xls" ContentType = "application/vnd.ms-excel"
".gif" ContentType = "image/gif"
".jpg", "jpeg" ContentType = "image/jpeg"
".wav" ContentType = "audio/wav"
".mp3" ContentType = "audio/mpeg3"
".mpg", "mpeg" ContentType = "video/mpeg"
".rtf" ContentType = "application/rtf"
".htm", "html" ContentType = "text/html"
".txt" ContentType = "text/plain"
".pdf"
ContentType = "application/pdf"
其他 ContentType = "application/octet-stream"
其他 ContentType = "application/octet-stream"