本站首页    管理页面    写新日志    退出


«July 2025»
12345
6789101112
13141516171819
20212223242526
2728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7574576
建立时间:2006年5月29日




[Ruby on Rails]使用ActionMaile发送邮件
软件技术

lhwork 发表于 2007/2/7 15:19:41

更改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 = "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: 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 = "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<            }        #参数的含义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"


阅读全文(3331) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144755577 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号