3

I am creating a draft message in GMail via API and the raw message is (before base64urlencoding):

X-Sender: sender@sender.com.au
X-Receiver: receiver@receiver.com.au
MIME-Version: 1.0
From: sender@sender.com.au
To: receiver@receiver.com.au
Subject: This email is from API
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

<html>
    <style>
        p{margin-top:0px; margin-bottom:0px;}
    </style>
    <body class="setupTab" style=" background-color:#CCCCCC; bEditID:b1st1; bLabel:body;">
        <center >
            <table cellpadding="0" width="500" cellspacing="0" id="topTable" height="450" >
                <tr valign="top" >
                    <td style=" background-color:#FFFFFF; bEditID:r1st1; bLabel:header; vertical-align:top; height:100; text-align:left;">
                        <img border="0" bEditID="r1sp1" bLabel="headerImage" id="r1sp1" src="https://cs31.salesforce.com/servlet/servlet.ImageServer?id=<someid>&oid=<someid>" ></img>
        .... 
    </body>
</html>

User is then redirected to https://mail.google.com/mail/u/0/#drafts?compose=<id> to review the email and then send.

However the images, part of the html, are not rendered/displayed. There are some other minor issues with the html rendering too but image is critical to have.

Any thoughts?

redfox88
  • 81
  • 5
  • [Somewhat related](http://stackoverflow.com/questions/4312687/how-to-embed-images-in-email) – Tholle May 30 '16 at 06:46

2 Answers2

1

You need to create a multipart/related message, where one part is the HTML content of the email and the other part is the image. The image part contains a Content-ID header that specifies an ID for the image, and the HTML image tag references that ID in the src attribute using the format cid:ID_HERE.

If creating this is hard, you can use scripts and libraries like those mentioned in here: creating a MIME email template with images to send with python / django

Also a similar question: GMAIL API for sending Email with attachment

Community
  • 1
  • 1
Moradnejad
  • 3,466
  • 2
  • 30
  • 52
  • Thanks, that works ! Converting html with src= to embedded base64 image will be a challenge though in Salesforce :) – redfox88 May 31 '16 at 01:26
1

I tried following alternative approach and it works without converting the message to multi-part.

Replace all '=' sign with '=3D' like so:

<img border=3D"0" bEditID=3D"r1sp1" bLabel=3D"headerImage" id=3D"r1sp1" src="https://cs31.salesforce.com/servlet/servlet.ImageServer?id=<someid>&oid=<someid>" ></img>

And use Contact-Transfer-Encoding: quoted-printable

the image was then rendered in GMail draft correctly.

redfox88
  • 81
  • 5