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
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
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