Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images not rendering in draft message created using Gmail API

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

X-Sender: [email protected]
X-Receiver: [email protected]
MIME-Version: 1.0
From: [email protected]
To: [email protected]
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?

like image 774
redfox88 Avatar asked Jun 22 '26 18:06

redfox88


1 Answers

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

like image 194
Moradnejad Avatar answered Jun 28 '26 14:06

Moradnejad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!