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
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架