Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html email with inline images not rendered properly in iphone

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--
like image 809
user316054 Avatar asked May 24 '13 14:05

user316054


People also ask

Why are images not showing in my emails on iPhone?

Always show images If images don't load in Gmail, check your settings. On your iPhone or iPad, open the Gmail app . Settings . Always display external images.

How do I get images to show in emails iOS?

Step 1: Tap the Settings icon. Step 2: Scroll down and choose the Mail option. Step 3: Scroll to the Messages section of the menu and tap the button to the right of Load Remote Images to turn it on. I have it enabled in the image below.

What does rendered image mean on iPhone?

Original rendering mode renders an image as they appear in the original source image. This might be what we expected from most images, such as product images and onboarding illustrations. Example of illustration and article images. Not every image is created equals.


1 Answers

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!
like image 192
user316054 Avatar answered Oct 01 '22 18:10

user316054