ROR中使用ActionMailer发送邮件

更改config目录下的配置文件environment.rb 在最下面追加一段:

ActionMailer::Base.delivery_method = :smtp

#以简单邮件传送协议发送邮件

ActionMailer::Base.default_charset = "GBK"

#设置邮件的默认编码为国标码 否则发送的邮件主题可能会乱码

ActionMailer::Base.server_settings = {

:address => "192.168.1.10",

:port => 25,

:domain => "a.com",

:authentication => :login,

:user_name => "xuxiaolai",

:password => "xxl123",

}

    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 = "xuxiaolai@a.com"

    @from = 'xuxiaolai@a.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    = "标题"

        @body       = content

        @recipients = "xuxiaolai@a.com"

        @from = 'xuxiaolai@a.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    = "标题"

        @body       = content

        @recipients = "xuxiaolai@a.com"

        @from = 'xuxiaolai@a.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"

".swf"    ContentType = "application/x-shockwave-flash"
 
其他      ContentType = "application/octet-stream"

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值