Ruby send email with an attachment with /usr/sbin/sendmail

限于喜欢 提交于 2019-12-12 02:38:52

问题


I am trying to send an email with a csv file for attachement. I do the following but I only receive an email with a empty csv file (and not with the content of it). Can you please help me on that? I don't want to use any extra library so please don't tell me to use pony or so ;-)

to="me@exemple.com"
subject='The subject'
from='"Name" <you@exemple.com>'
description ="Desc"

csvnamefile = "/path/to/file/filename.csv"

puts  value = %x[/usr/sbin/sendmail #{to} << EOF
subject: #{subject}
from: #{from}
Content-Description: "#{csvnamefile}"
Content-Type: multipart/mixed; name="#{csvnamefile}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{csvnamefile}"
Description : #{description}
EOF]

Thanks


回答1:


/usr/sbin/sendmail doesn't know anything about attachments and treats email message body according to RFC 5322 as flat US-ASCII text. To send a file as attachment you need to format your message as MIME message according to RFC 2045. For example of such a message see Appendix A to RFC 2049.




回答2:


Thanks Alex. I could make it work with your informations. The final working result looks like this:

binary = File.read(csvnamefile)
encoded = [binary].pack("m")    # base64 econding
puts  value = %x[/usr/sbin/sendmail #{to} << EOF
subject: #{subject}
from: #{from}
Content-Description: "#{csvnamefile}"
Content-Type: text/csv; name="#{csvnamefile}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{csvnamefile}"
#{encoded}
EOF]


来源:https://stackoverflow.com/questions/6732596/ruby-send-email-with-an-attachment-with-usr-sbin-sendmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!