问题描述
我希望有人可以帮助我在下面的代码中做错了什么 - 我的电子邮件被发送,大多数电子邮件客户端(gmail web,android)中的图像都已正确插入,但是它不能正常呈现一个iphone / ipad:只显示最后附加的图片,没有显示任何html内容或文本内容。我理解每个电子邮件客户端都会解释并提供不同的有效载荷,我不知道如何使其在iphone上工作。任何帮助赞赏!
ruby code:
require'mail'
def inline_body_with_attachments(html,附件)
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 =>[email protected],
:to =>[email protected],
:subject =>带内联图片的html电子邮件
})
text_part =邮件:: Part.new do
bodysome text
end
mail.text_part = text_part
#加载附件
附件=image.png
mail.attachments [attachment] = File.read(attachment)
inline_html = inline_body_with_attachments(< b>内联图像< / 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!
电子邮件看起来像:
日期:星期五,2013年5月24日13:58:02 +0000
从:[email protected]
至:[email protected]
Message- ID:< [email protected]>
主题:带附件的测试邮件
Mime版本:1.0
内容类型:multipart / alternative;
boundary = - == _ mimepart_519f71eab2260_2a1d9e076471d6;
charset = UTF-8
Content-Transfer-Encoding:7bit
---- == _ mimepart_519f71eab2260_2a1d9e076471d6
日期:Fri ,2013年5月24日13:58:02 +0000
Mime版本:1.0
内容类型:text / plain;
charset = UTF-8
Content-Transfer-Encoding:7bit
Content-ID:< [email protected]>
一些文本
---- == _ mimepart_519f71eab2260_2a1d9e076471d6
日期:Fri,24 May 2013 13:58:02 +0000
Mime版本:1.0
内容类型:text / html;
charset = UTF-8
Content-Transfer-Encoding:7bit
Content-ID:< [email protected]>
< b>内联图像< / b>< img src ='cid:[email protected]'/>
---- == _ mimepart_519f71eab2260_2a1d9e076471d6
日期:Fri,24 May 2013 13:58:02 +0000
Mime版本:1.0
内容类型:图像/ PNG;
charset = UTF-8;
filename = image.png
Content-Transfer-Encoding:base64
Content-Disposition:attachment;
filename = image.png
Content-ID:< [email protected]>
iVBORw0KGgoAAAANSUhEUgAAASwAAABaCAYAAAACcWsdAAAXUUlEQVR4nGJk
[....更多图像编码...]
DhwFowAXGE0go2CQAAAAAAD // wMAFOkCtHNhXPkAAAAASAOCOSCWC =
---- == _ mimepart_519f71eab2260_2a1d9e076471d6--
为了wrok on iphone,我不得不将图像的html内容包装到他们自己的多部分/相关的部分。
require'mail'
def inline_body_with_attachments(html,附件)
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 =>[email protected],
:to =>[email protected],
:subject =>带有attachment- raw4的测试邮件,
:content_type =>'multipart / alternative'
})
text_part = Mail :: Part.new do
bodysome text
end
mail.add_part(text_part)
other_part = Mail :: Part.new do
content_type'multipart / related;'
end
#加载附件
附件=image.png
#mail.attachments [attachment] = File.read(附件)
other_part.add_file(附件)
inline_html = inline_body_with_attachments(< b> ;一个内联图像< / b>< img src ='image.png'/>,other_part.attachments)
html_part = Mail :: Part.new do
conte nt_type'text / html; charset = UTF-8'
body inline_html
end
other_part.add_part(html_part)
mail.add_part(other_part)
邮件。交付!
I was hoping someone could help me out what I'm doing wrong in the below code - my email get sent, with the image inlined properly in most email client (gmail web, android), but it does not render properly on an iphone/ipad: only the last attached picture is displayed, without any html content or text content displayed. I understand every email client interprets and renders the payload differently, I don't know how to make it work on an iphone.
Any help appreciated!
ruby code:
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 => "[email protected]",
:to => "[email protected]",
: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!
The email looks like:
Date: Fri, 24 May 2013 13:58:02 +0000
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
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: <[email protected]>
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: <[email protected]>
<b>An inline image</b><img src='cid:[email protected]'/>
----==_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: <[email protected]>
iVBORw0KGgoAAAANSUhEUgAAASwAAABaCAYAAAACcWsdAAAXUUlEQVR4nGJk
[.... more image encoding ...]
DhwFowAXGE0go2CQAAAAAAD//wMAFOkCtHNhXPkAAAAASUVORK5CYII=
----==_mimepart_519f71eab2260_2a1d9e076471d6--
In order to wrok on the iphone, I had to wrap the html content with the image into their own multipart/related part.
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 => "[email protected]",
:to => "[email protected]",
: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!
这篇关于带有内联图片的html电子邮件在iPhone中无法正常显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!