ruby中发送带附件,中文,html的email

mailfactory是rubyforge上一个简单的邮件发送包, 通过它我们可以很方便的发送带附件和html格式的邮件,不过目前的mailfactory在支持中文上有一些问题, 需要打一个补丁

 

先需要通过 gem install mailfactory 安装

 

然后是对mailfactory的补丁,可以直接修改mailfactory的源文件,也可以单独作一个文件mailfactory_enhence.rb,只要在require 'mailfactory'以后require 它就可以了

 

#  对mailfactory打补丁,增加了一个encoding参数
class MailFactory
  attr_accessor 
: encoding
  
  def html
= (newhtml)
  
@html   =   " <html>\n<head>\n<meta content=\ " text / html;charset = # {encoding}\" http-equiv=\"Content-Type\">\n</head>\n<body bgcolor=\"#ffffff\" text=\"#000000\">\n#{newhtml}\n</body>\n</html>"
  end
  def body_to_s()
  body 
=  Array . new()
  
  
#  simple message with one part
   if ( ! multipart ? ())
   
return ( @text )
  
else
   body 
<<   " This is a multi-part message in MIME format.\r\n\r\n--#{@attachmentboundary}\r\nContent-Type: multipart/alternative; boundary=\ " # {@bodyboundary}\""
   
   
if ( @attachments . length   >   0 )
    
#  text part
    body  <<   " #{buildbodyboundary( " text / plain; charset = # {encoding}; format=flowed", '7bit')}\r\n\r\n#{@text}"
    
    # html part

    body  <<   " #{buildbodyboundary( " text / html; charset = # {encoding}", '7bit')}\r\n\r\n#{@html}"
    
    body 
<<   " --#{@bodyboundary}-- "
    
    
#  and, the attachments
     if ( @attachments . length   >   0 )
     
@attachments . each () {  | attachment |
      body 
<<   " #{buildattachmentboundary(attachment)}\r\n\r\n#{attachment['attachment']} "
     }
     body 
<<   " \r\n--#{@attachmentboundary}-- "
    end
   
else
    
#  text part
    body  <<   " #{buildbodyboundary( " text / plain; charset = # {encoding}; format=flowed", '7bit')}\r\n\r\n#{@text}"
    
    # html part

    body  <<   " #{buildbodyboundary( " text / html; charset = # {encoding}", '7bit')}\r\n\r\n#{@html}"
    
    body 
<<   " --#{@bodyboundary}-- "
   end
   
   
return (body . join ( " \r\n\r\n " ))
  end
 end 
end 


最后开始使用: mailfactory的使用非常简单, 例子如下

 

# 使用方法非常简单
    mail = MailFactory . new 
    mail
. encoding = " GBK "   # 设置encoding
    mail . to = [ ' a@sina.com ' , ' b@sina.com ' ] . join ( ' , ' # 多个收件人
    mail . from = ' from@host.com '  
    mail
. subject = ' subject '
    mail
. html = ' </font color="red">htmlconternt</font> '
    mail
. text = ' please use html view '
    mail
. attach( ' /usr/local/test.file ' )
    Net
:: SMTP . start( @smtp_host do   | smtp |
      smtp
. send_message(msgstr , from , to)
    end

 

 

转载于:https://www.cnblogs.com/baoz/archive/2006/02/06/2269856.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值