Ruby 使用 /usr/sbin/sendmail 发送带有附件的电子邮件

发布于 2024-11-24 22:41:03 字数 839 浏览 4 评论 0原文

我正在尝试发送一封带有 csv 文件作为附件的电子邮件。 我执行以下操作,但只收到一封包含空 csv 文件(而不包含其内容)的电子邮件。你能帮我一下吗? 我不想使用任何额外的库,所以请不要告诉我使用 pony 等;-)

to="[email protected]"
subject='The subject'
from='"Name" <[email protected]>'
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]

谢谢

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="[email protected]"
subject='The subject'
from='"Name" <[email protected]>'
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彡翼 2024-12-01 22:41:03

谢谢亚历克斯。我可以让它与你的信息一起工作。
最终的工作结果如下所示:

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]

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]
甲如呢乙后呢 2024-12-01 22:41:03

/usr/sbin/sendmail 对附件一无所知,并根据 RFC 5322 作为平面 US-ASCII 文本。要将文件作为附件发送,您需要根据 RFC 2045。有关此类消息的示例,请参阅RFC 2049 的附录 A

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文