64

I'm generating some inline images for an email sent from the iPad. Looks great in all desktop email clients, but gmail doesn't seem to like the base64 image and it shows up as text.

Anyone have any luck embedding images with base64 and gmail? Or know of a better solution for sending HTML emails with images from the iPad?

Steven Baughman
  • 1,076
  • 2
  • 13
  • 22

8 Answers8

33

The links from Moin Zaman show test results that are outdated (from 2008). As of my thorough testing today Gmail does support displaying embedded images for both methods.

Use base64 encoding image inline within <img src="...">

<html><body><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA9QAAADmCAIAAAC77FroAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAO..."></body></html>

Use base64 encoded image as attachment

Message-ID: <BE0243A40B89D84DB342702BC5FD6D313EA3BE1B@BYMAIL.example.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: yes
X-MS-TNEF-Correlator:
x-originating-ip: [xxx.xxx.xxx.xxx]

Content-Type: multipart/related;
    boundary="_038_BE0243A40B89D84DB342702BC5FD6D313EA3BE1BBYMAIL_";
    type="multipart/alternative"
MIME-Version: 1.0
Return-Path: email@example.com
X-OriginatorOrg: example.com

--_038_BE0243A40B89D84DB342702BC5FD6D313EA3BE1BBYMAIL_
Content-Type: multipart/alternative;
    boundary="_000_BE0243A40B89D84DB342702BC5FD6D313EA3BE1BBYMAIL_"

...skipping Content-Type: text/plain which would be here for this example...

--_000_BE0243A40B89D84DB342702BC5FD6D313EA3BE1BBYMAIL_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html><body><img border=3D"0" width=3D"980" height=3D"230" id=3D"Picture_x0020_1" src==3D"cid:image001.png@01CDA268.204677C0"></body></html>

--_000_BE0243A40B89D84DB342702BC5FD6D313EA3BE1BBYMAIL_--

--_038_BE0243A40B89D84DB342702BC5FD6D313EA3BE1BBYMAIL_
Content-Type: image/png; name="image001.png"
Content-Description: image001.png
Content-Disposition: inline; filename="image001.png"; size=32756;
    creation-date="Mon, 08 Oct 2012 15:27:07 GMT";
    modification-date="Mon, 08 Oct 2012 15:27:07 GMT";
Content-ID: <image001.png@01CDA268.204677C0>
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAA9QAAADmCAIAAAC77FroAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAO
xAAADsQBlSsOGwAAf5lJREFUeF7tvQlgVdW18L+ZR20mpsSLCYlBQKwgwRCMr9TAqzg0CAl98Y9a
ikBfHxL1A/r0tUr77Feg1mDav4LUijxTk8hLRIstQ2mJhEiAWAEpNCGRa8KUSQXCzLf2cOZz7j33
5s5Zx6j3nruHtX97n33WWWfttbtdv36d4IEEkAASQAJIAAkgASSABJCA/wl0938VWAMSQAJIAAkg
ASSABJAAEkAClAAq3zgOkAASQAJIAAkgASSABJBAgAig8h0g0FgNEkACSAAJIAEkgASQABJA5RvH
...

To do your own testing, you can send email with inline embedded image using one of the following techniques

Send an email using one of the above to your Gmail account, then open the Email in Gmail Web Client (any browser that works) and use the Down-Arrow next to the Reply button to choose the Show Original option. This will show you how it is received.

I think best practice is to use the embedded image as attachment method.

In my testing with Gmail Web Client, if I sent 30 images in a single email of different sizes, a few would not load successfully showing image container but not the image. If that happens, try reloading the page.

In my testing (Windows 7)...

  • Chrome (latest) needed a couple of reloads to successfully load/show all 30 images
  • Opera (latest) wouldn't successfully show all 30 images regardless of number of reloads
  • Firefox (latest) consistently showed all 30 images without issue
  • Internet Explorer 9 (latest) consistently showed all 30 images without issue
  • Safari (latest) consistently showed all 30 images without issue
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
cusman
  • 1,711
  • 1
  • 17
  • 17
  • 3
    I tested with large number of images, but I think if large size images are used, that might also cause them not to load/show in time. Gmail Web Client is a little sensitive to how much time it would take for those embedded images to load/show and this is somewhat browser dependent as well. Your machine/network speed may also be contributing factors. But in terms of base support, Gmail does support embedded images. It may just be some intentional time-to-load protections from Google against many/large embedded images from harming user experience. – cusman Oct 08 '12 at 17:19
  • 16
    Doesn't seem to support it anymore (Gmail).. it just removed the src attributes and adds the alt one instead.. – Márcio Duarte Feb 22 '16 at 09:42
  • 46
    Testing today, Gmail does *not* support images using `data:` URLs. It just uses the `alt` text instead. – jameshfisher Feb 26 '16 at 16:17
  • 12
    I tested this today (11/7/2016) and Gmail does not display data: URLs. – user3246173 Nov 07 '16 at 17:22
  • 16
    Further testing, inline data: still not being displayed. Jan 2017 – Olivier Butler Jan 18 '17 at 12:10
  • 8
    gmail not supporting base64 images 15/02/2017 – StackUP Feb 15 '17 at 02:27
  • 1
    Not even `Some text` to add a 16x16px icon before `Some text` works. Gmail removes `data:image/png;base64,...` so it ends like this: `Some text`. – Antônio Medeiros Apr 08 '17 at 16:50
  • 2
    Today (23 jun 2018) Gmail did support a base64 encoded image: I did receive my ``. As the answer was accepted in 2015 and supposedly stopped working in 2017 we'd better hurry before Google changes its mind again :o) ! There is still quite a challenge as Gmail messes up with style though – shevket Jun 23 '18 at 11:30
  • 4
    2019 Jan 12. Gmail doesn't support (again). – John Pang Jan 12 '19 at 10:20
  • 4
    2019-05-03: Gmail does not support baste64 images. – fritzmg May 03 '19 at 12:23
  • What does reloading accomplish? Does it only load if it comes from a cache, so it needs to load a second time, or something else? – Jared Dec 17 '19 at 02:09
  • 1
    I tried today. Inline base64 encoded images are not working in Gmail. – Dave Dec 24 '19 at 16:54
  • 3
    Tested May 4, 2020. Gmail still doesn't support inlne data:image/png;base64 encoded images in img tag. – AdityaKapreShrewsburyBoston May 04 '20 at 14:30
  • 1
    This answer is now only half correct; as of 2021-02-01; inline b64 images (` – Caius Jard Feb 11 '21 at 11:29
  • 4
    2021-05-20 Gmail (Web/Mobile) does not support inline base 64 images – Nimantha May 20 '21 at 09:18
  • 1
    Gmail (Web/Mobile) does not support inline base 64 images – HM Tanbir Jan 24 '22 at 06:39
11

There doesn't seem to by any official documentation but Gmail definitely doesn't support this, inline or as an attachment in base64.

Here is some testing that campaign monitor tried:
Embedding images in email
Embedding images revisited

Moin Zaman
  • 25,281
  • 6
  • 70
  • 74
2

Make sure you set Content-Type: multipart/mixed; , boundary and Content-Transfer-Encoding: base64

valexa
  • 4,462
  • 32
  • 48
-1

I test that gmail don support also raw data uri images (without base64) - I use this snippet to generate image (which then was send to gmail addres) - but images not show up :(

To solve this issue you need to add images as attachements with cid and use that cid in img tags <img src="cid:123456"> - more details here

function convert() {
  let base64 = imageBase64.value.split('base64,')[1];
  let hex = [...atob(base64)].map(c => c.charCodeAt(0).toString(16).padStart(2, 0));
  let img = 'data:image/png,%' + hex.join('%');

  pic.src = img;
  msg.innerText = img;
}
Put your img base64 data uri here<br>
<input style="width:200px" id='imageBase64' value="data:image/bmp;base64,Qk0aAwAAAAAAABsAAAAMAAAAEAAQAAEAGAAAAAAACFpyAAAAAAAACB/NCB/NCB/NCB/NAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACFpyCFpyCFpyCB/NCB/NCB/NCB/NCB/NCB/NCB/NAAAAAAAAAAAAAAAAAAAAAAAAAAAACFpyCFpyCFpyCB/NCB/NCB/NCB/NCB/NCB/NCB/NCB/NCB/NAAAAAAAAAAAAKoH8AAAACFpyCB/NCB/NCB/NCB/NCB/NCB/NCB/NCB/NCB/NCB/NCFpyCFpyKoH8KoH8KoH8AAAACB/NCB/NCFpyCB/NCB/NKoH8CB/NCB/NKoH8CB/NCFpyCFpyKoH8KoH8CFpyCFpyCFpyCFpyCFpyCFpyCB/NCB/NCB/NCB/NCB/NAAAAAAAACFpyAAAACFpyCFpyCFpyCFpyCFpyCFpyCFpyCB/NCFpyCFpyCFpyCB/NAAAAAAAACFpyAAAAAAAACFpyCFpyCFpyCFpyCFpyCB/NCFpyCFpyCFpyCB/NCFpyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKoH8KoH8KoH8KoH8KoH8KoH8KoH8CFpyAAAAAAAAAAAAAAAAAAAAAAAACFpyCFpyKoH8KoH8KoH8KoH8CFpyCFpyCFpyCFpyCFpyAAAAAAAAAAAAAAAAAAAACFpyKoH8CFpyCFpyKoH8KoH8KoH8CFpyKoH8KoH8KoH8CFpyAAAAAAAAAAAAAAAACFpyKoH8CFpyKoH8KoH8KoH8CFpyKoH8KoH8CFpyCFpyCFpyAAAAAAAAAAAAAAAAAAAACFpyCFpyCFpyKoH8KoH8CFpyKoH8AAAACFpyCFpyCFpyAAAAAAAAAAAAAAAAAAAACB/NCB/NCB/NCB/NCB/NCB/NCB/NCB/NCB/NKoH8KoH8AAAAAAAAAAAAAAAAAAAAAAAACB/NCB/NCB/NCB/NCB/NAAAAAAAAKoH8KoH8KoH8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKoH8KoH8KoH8">
<button onclick="convert()">Convert</button><br> Result
<br>
<textarea id='msg' rows="4" cols="50"></textarea><br>
<img id='pic'>
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • how to set the content id ? i'm not understand how to use it? – rakesh shrestha Nov 19 '19 at 08:03
  • @ReyYoung more details about this you can find [here](https://stackoverflow.com/questions/9110091/base64-encoded-images-in-email-signatures/9110164#9110164) - the way of doing this highly depends of your backend technology - so you will need to conduct separate research in this direction. – Kamil Kiełczewski Nov 19 '19 at 09:27
  • The CID approach, like the embedded Base64 approach, will work for some email clients and not others. The only method that will work across the board (pretty much) is hosting the image in a CDN and referencing that in the HTML body. – Alan B Nov 10 '21 at 12:00
-1

I use this generator https://codebeautify.org/svg-to-base64-converter

When you get onto the website, you can upload your image, for example an SVG image, after uploading your image you will get a code snippet with and in-bedded Base64 image that you can simple copy and past to your html file.

<img src='data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMi4xNDciIGhlaWdodD0iNDAuMTMzIiB2aWV3Qm94PSIwIDAgMjIuMTQ3IDQwLjEzMyI+CiAgPGcgaWQ9IlBhdGhfNjExOCIgZGF0YS1uYW1lPSJQYXRoIDYxMTgiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEuNSAxLjUpIiBmaWx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zODE0Njk3MjY2IDIyLjAyNDgxNDYwNTcxMjg5IEwgLTEuNTAwMDAzODE0Njk3MjY2IDIwLjUyNDgxNDYwNTcxMjg5IEwgLTEuNTAwMDAzODE0Njk3MjY2IDEzLjkyNDk2NDkwNDc4NTE2IEwgLTEuNTAwMDAzODE0Njk3MjY2IDEyLjQyNDk2NDkwNDc4NTE2IEwgLTMuODE0Njk3MjY1NjI1ZS0wNiAxMi40MjQ5NjQ5MDQ3ODUxNiBMIDQuMDYyNzI2MDIwODEyOTg4IDEyLjQyNDk2NDkwNDc4NTE2IEwgNC4wNjI3MjYwMjA4MTI5ODggOC43MjQ4NTM1MTU2MjUgQyA0LjA2MjcyNjAyMDgxMjk4OCA1LjU5NzYxNDI4ODMzMDA3OCA1LjAzMTY0NTc3NDg0MTMwOSAyLjk4NzQ4Mzk3ODI3MTQ4NCA2Ljg2NDc0NjA5Mzc1IDEuMTc2NjU0ODE1NjczODI4IEMgOC42MzczNDYyNjc3MDAxOTUgLTAuNTc0NDI0NzQzNjUyMzQzOCAxMS4xMjM4NjYwODEyMzc3OSAtMS40OTk5OTYxODUzMDI3MzQgMTQuMDU1NDk2MjE1ODIwMzEgLTEuNDk5OTk2MTg1MzAyNzM0IFoiIHN0cm9rZT0ibm9uZSIgZmlsbD0iI2YzYjYxZiIvPgogIDwvZz4KPC9zdmc+Cg=='/>

Works great.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '22 at 22:44
-2

Try This Buddies.. I m sure this Would work to include an image as an attachment

                    $output_hex_string_img = $image;
                    $output_bin_string_img = base64_decode($output_hex_string_img);
                    //echo base64_encode( $output_bin_string_img );
                    $XXX = base64_encode( $output_bin_string_img );

                    $from_name = "Senders Name";
                    $from_mail = "yyyyyyyy@gmail.com";
                    $replyto = "yyyyyyyy@gmail.com";
                    $subject = "Device Missing Notification";
                $message = "Device Missing notification has been activated on your device. Please change this setting when you find your smartphone back. Best Of Luck!!\r\r ";



                    $mailto = 'xxxxxxxx@mail.com';



                        $file = $XXX;
                        $filename = "Print_shot.png";
                        $uid = md5(uniqid(time()));
                        $name = basename($file);
                        $header = "From: ".$from_name." <".$from_mail.">\r\n";
                        $header .= "Reply-To: ".$replyto."\r\n";
                        $header .= "MIME-Version: 1.0\r\n";
                        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                        $header .= "This is a multi-part message in MIME format.\r\n";
                        $header .= "--".$uid."\r\n";
                        $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
                        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
                        $header .= $message."\r\n\r\n";
                        $header .= "--".$uid."\r\n";
                        $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
                        $header .= "Content-Transfer-Encoding: base64\r\n";
                        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
                        $header .= $file."\r\n\r\n";
                        $header .= "--".$uid."--";
                        mail($mailto, $subject, "", $header);

But you have to include everything inside headers by using different Content-Type in headers. and whatever Appear in the Body which could be the third parameter in the mail function will be passed as a blank string..

Shivek Parmar
  • 2,865
  • 2
  • 33
  • 42
-2

This works fine: I set two src attribute, one with data:image/png;base64 and the other with the link to the image. When you use gmail, it use the src attribute with link and when you use other client of email it use the src attribute with data:image/png;base64. Try it!. you will see.

-11
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

[picker addAttachmentData:UIImageJPEGRepresentation(_tempImage,1) mimeType:mimeType fileName:filename];

[picker setMessageBody:emailBody isHTML:YES];

If isHTML is YES, addAttachmentData will auto change to base64 string, in email html can see you img.

If isHTML is NO, addAttachmentData is attachment.

Jack
  • 10,943
  • 13
  • 50
  • 65
Lion
  • 59
  • 5