在iphone中无法正确呈现内嵌图像的html电子邮件

我希望有人可以帮助我解决我在下面的代码中出错的问题 – 我的电子邮件被发送,图像在大多数电子邮件客户端(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 = "http://sofzh.miximages.com/iphone/image.png" mail.attachments[attachment] = File.read(attachment) inline_html = inline_body_with_attachments("An inline image", 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:  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:  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:  An inline image ----==_mimepart_519f71eab2260_2a1d9e076471d6 Date: Fri, 24 May 2013 13:58:02 +0000 Mime-Version: 1.0 Content-Type: image/png; charset=UTF-8; filename=https://stackoverflow.com/questions/16736878/html-email-with-inline-images-not-rendered-properly-in-iphone/image.png Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=https://stackoverflow.com/questions/16736878/html-email-with-inline-images-not-rendered-properly-in-iphone/image.png Content-ID:  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 = "http://sofzh.miximages.com/iphone/image.png" #mail.attachments[attachment] = File.read(attachment) other_part.add_file(attachment) inline_html = inline_body_with_attachments("An inline image", 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 5上运行。我正在发送到Microsoft Exchange服务器。

 require 'mail' mail = Mail.deliver do to 'myaddress@domain.com' from 'test@domain.com' subject 'Inline Image test' add_file './banner.png' pic = attachments['banner.png'] html_part do content_type 'text/html; charset=UTF-8' body "" end end 

希望这可以帮助。