gpt4 book ai didi

ruby - 发送多部分邮件和附件

转载 作者:数据小太阳 更新时间:2023-10-29 07:36:24 25 4
gpt4 key购买 nike

我正在尝试使用带有 Ruby 1.9.3 的 gem 'mail' 发送电子邮件。它包含一个 text/html 和一个 text/plain 部分,应作为替代部分以及附件嵌入。

这是我当前的代码:

require 'mail'

mail = Mail.new
mail.delivery_method :sendmail
mail.sender = "me@example.com"
mail.to = "someguy@example.com"
mail.subject = "Multipart Test"
mail.content_type = "multipart/mixed"

html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body "<h1>HTML</h1>"
end

text_part = Mail::Part.new do
body "TEXT"
end

mail.part :content_type => "multipart/alternative" do |p|
p.html_part = html_part
p.text_part = text_part
end

mail.add_file :filename => "file.txt", :content => "FILE"

mail.deliver!

它会生成一封包含可用替代部分但没有附件的邮件。我正在使用 thunderbird 10.0.12 进行测试。

我已经在 github 上发布了这个,但不幸的是这些帖子并没有让我变得更聪明。 https://github.com/mikel/mail/issues/118#issuecomment-12276876 .也许有人比我更能理解上一篇文章;)

有人能让这个例子工作吗?

谢谢,克丽丝

最佳答案

我设法像这样修复它:

html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body html
end

text_part = Mail::Part.new do
body text
end

mail.part :content_type => "multipart/alternative" do |p|
p.html_part = html_part
p.text_part = text_part
end


mail.attachments['some.xml'] = {content: Base64.encode64(theXML), transfer_encoding: :base64}
mail.attachments['some.pdf'] = thePDF

mail.content_type = mail.content_type.gsub('alternative', 'mixed')
mail.charset= 'UTF-8'
mail.content_transfer_encoding = 'quoted-printable'

一点都不直观,但阅读Pony源代码有点帮助,以及将工作 .eml 与此 gem 生成的任何内容进行比较。

关于ruby - 发送多部分邮件和附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15008789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com