gpt4 book ai didi

iphone - 带有内联图像的 html 电子邮件在 iphone 中无法正确呈现

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

我希望有人能帮我解决我在下面的代码中做错了什么 - 我的电子邮件已发送,图像在大多数电子邮件客户端(gmail web,android)中正确内联,但它无法正确呈现iphone/ipad:只显示最后附上的图片,不显示任何html内容或文本内容。我了解每个电子邮件客户端以不同方式解释和呈现有效负载,我不知道如何让它在 iPhone 上运行。

感谢任何帮助!

ruby 代码:

require 'mail'

def inline_body_with_attachments(html, attachments)
attachments.each do |attachment|
if (html =~ /#{attachment.filename}/)
html = html.sub(attachment.filename, "cid:#{attachment.cid}")
end
end
return html
end


mail = Mail.new({
:from => "foo@bar.com",
:to => "you@gmail.com",
:subject => "html email with inline images"
})

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

mail.text_part = text_part

# Load the attachments
attachment = "image.png"
mail.attachments[attachment] = File.read(attachment)

inline_html = inline_body_with_attachments("<b>An inline image</b><img src='image.png'/>", mail.attachments)

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

mail.html_part = html_part
mail.deliver!

电子邮件看起来像:

Date: Fri, 24 May 2013 13:58:02 +0000
From: foo@bar.com
To: you@gmail.com
Message-ID: <519f71eab9175_2a1d9e0764742c@foo.com.mail>
Subject: test mail with attachment- raw
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_519f71eab2260_2a1d9e076471d6";
charset=UTF-8
Content-Transfer-Encoding: 7bit



----==_mimepart_519f71eab2260_2a1d9e076471d6
Date: Fri, 24 May 2013 13:58:02 +0000
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <519f71eab3e04_2a1d9e076472b9@foo.com.mail>

some text

----==_mimepart_519f71eab2260_2a1d9e076471d6
Date: Fri, 24 May 2013 13:58:02 +0000
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <519f71eab692b_2a1d9e076473d4@foo.com.mail>

<b>An inline image</b><img src='cid:519f71eab047e_2a1d9e07647047@foo.com.mail'/>

----==_mimepart_519f71eab2260_2a1d9e076471d6
Date: Fri, 24 May 2013 13:58:02 +0000
Mime-Version: 1.0
Content-Type: image/png;
charset=UTF-8;
filename=image.png
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=image.png
Content-ID: <519f71eab047e_2a1d9e07647047@foo.com.mail>

iVBORw0KGgoAAAANSUhEUgAAASwAAABaCAYAAAACcWsdAAAXUUlEQVR4nGJk
[.... more image encoding ...]
DhwFowAXGE0go2CQAAAAAAD//wMAFOkCtHNhXPkAAAAASUVORK5CYII=


----==_mimepart_519f71eab2260_2a1d9e076471d6--

最佳答案

为了在 iphone 上工作,我不得不将带有图像的 html 内容包装到它们自己的 multipart/related 部分。

require 'mail'


def inline_body_with_attachments(html, attachments)
attachments.each do |attachment|
if (html =~ /#{attachment.filename}/)
html = html.sub(attachment.filename, "cid:#{attachment.cid}")
end
end
return html
end


mail = Mail.new({
:from => "demo@etsy.com",
:to => "nkammah@etsy.com",
:subject => "test mail with attachment- raw4",
:content_type => 'multipart/alternative'
})

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

mail.add_part( text_part)

other_part = Mail::Part.new do
content_type 'multipart/related;'
end

# Load the attachments
attachment = "image.png"
#mail.attachments[attachment] = File.read(attachment)
other_part.add_file(attachment)

inline_html = inline_body_with_attachments("<b>An inline image</b><img src='image.png'/>", other_part.attachments)
html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body inline_html
end
other_part.add_part(html_part)

mail.add_part(other_part)

mail.deliver!

关于iphone - 带有内联图像的 html 电子邮件在 iphone 中无法正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16736878/

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