论坛首页 编程语言技术论坛

使用ActionMaile发送邮件

浏览 5561 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-12-26  
使用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的地址和端口。这些缺省值分别为localhost25
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连接邮件服务器的方式。这个参数应该被忽略,如果你的服务器不要求确认。
 
 
 
创建一个mailermodels
class OrderMailer < ActionMailer::Base
 def signup(domain, sent_at = Time.now)
    @subject    = 'Welcome to Beast'
    @body       = "hello world"
    @recipients = "yyy@yyy.com"
    @from = 'yyy@yyy.com'
    @sent_on    = sent_at
    @headers    = {}
 end
end
 
@subject:邮件标题
@body:邮件正文可以使用html标签但需要设置参考下面
@recipients:收件人可以接收数组进行群发
多人发送:@recipients = [ "1@a.com""2@b.com"]
@from:发件人
@sent_on:用于设置邮件 Date: headerTime 对象
@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
      <html>
           ……
      </html>
      model中的mailer类改成如下:
       def signup(domain,content,sent_at = Time.now)
        @subject    = "xxx"
        @body       = content
        @recipients = "xxx@xxx.com"
        @from = 'xxx@xxx.com'
        @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 = "xxx@xxx.com"
        @from = 'xxx@xxx.com'
        @sent_on    = sent_at
        @headers    = {}
       @data = ""
        File.open("D:\\Tools\\FastAIT.rar", "rb") { |fp|
            @data<<fp.read()
           }
       #参数的含义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"
 
 
   发表时间:2007-07-06  
你这能发出去吗?你自己试过?
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics