Base64 images to gmail

HtmlEmailIpadGmailBase64

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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: <[email protected]>
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 protected]
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:[email protected]"></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: <[email protected]>
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

Solution 2 - Html

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

Solution 3 - Html

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

Solution 4 - Html

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'>

Solution 5 - Html

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+CiAgPGcgaWQ9IlBhdGhfNjExOCIgZGF0YS1uYW1lPSJQYXRoIDYxMTgiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEuNSAxLjUpIiBmaWxsPSJub25lIj4KICAgIDxwYXRoIGQ9Ik0xNS42NTEsNi4xNjVoMy41Vi4yNjFBNDYuNyw0Ni43LDAsMCwwLDE0LjA1NiwwQzkuMDE1LDAsNS41NjMsMy4wNzUsNS41NjMsOC43MjV2NS4ySDB2Ni42SDUuNTYzVjM3LjEzM0gxMi4zOFYyMC41MjVoNS4zMzhsLjg0OS02LjZIMTIuMzhWOS4zNzhjMC0xLjkwNy41MjktMy4yMTMsMy4yNzEtMy4yMTNaIiBzdHJva2U9Im5vbmUiLz4KICAgIDxwYXRoIGQ9Ik0gMTQuMDU1NDk2MjE1ODIwMzEgMy44MTQ2OTcyNjU2MjVlLTA2IEMgOS4wMTQ5NTY0NzQzMDQxOTkgMy44MTQ2OTcyNjU2MjVlLTA2IDUuNTYyNzI2MDIwODEyOTg4IDMuMDc1MDkyMzE1NjczODI4IDUuNTYyNzI2MDIwODEyOTg4IDguNzI0ODUzNTE1NjI1IEwgNS41NjI3MjYwMjA4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DAxOTUgLTAuNTc0NDI0NzQzNjUyMzQzOCAxMS4xMjM4NjYwODEyMzc3OSAtMS40OTk5OTYxODUzMDI3MzQgMTQuMDU1NDk2MjE1ODIwMzEgLTEuNDk5OTk2MTg1MzAyNzM0IFoiIHN0cm9rZT0ibm9uZSIgZmlsbD0iI2YzYjYxZiIvPgogIDwvZz4KPC9zdmc+Cg=='/>

Works great.

Solution 6 - Html

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 = "[email protected]";
					$replyto = "[email protected]";
					$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..

Solution 7 - Html

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.

https://img.newoldstamp.com/i/0/0/0/0/197.png?g6mv7" width="24" class="tmp-social-twitter" alt="twitter">

Solution 8 - Html

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteven BaughmanView Question on Stackoverflow
Solution 1 - HtmlcusmanView Answer on Stackoverflow
Solution 2 - HtmlMoin ZamanView Answer on Stackoverflow
Solution 3 - HtmlvalexaView Answer on Stackoverflow
Solution 4 - HtmlKamil KiełczewskiView Answer on Stackoverflow
Solution 5 - HtmlMedupi RamaboeaView Answer on Stackoverflow
Solution 6 - HtmlShivek ParmarView Answer on Stackoverflow
Solution 7 - HtmlIvan ExtremeraView Answer on Stackoverflow
Solution 8 - HtmlLionView Answer on Stackoverflow