Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer send base64 image

I am attempting to email an image from a MySQL database via PHPMailer.

Currently I take the image out of the database, base64_decode it, and then replace all spaces with plusses to give:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfEAAAOzCAYAAACoPT8zAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7P3nc13ZdfaL6m+4X/ix69T9oKpzT5U+dp17zzn9+rUkKlOyZbUty2pn+vVrmbYluyV1juxAEgwIBAkSOedABAIgSIJEIgIzmNlJTcWmuiU1Fewz7vOMscZecy+sDQIgWi3qrNn11DNmWGFvovdvjrnWXvsj/8vX90imTJkyZcqU6f7R+Pi46iMf/UaLZMqUKVOmTJnuH73xxhuqj/yvjw5KpkyZMmXKlOn+0S9+8QvVR/4/jx2RTJkyZcqUKdP9o//6r/9SfeR/e/yoZMqUKVOmTJnuH3nJIJ4pU6ZMmTLdZ/KSQTxTpkyZMmW6z+Qlg3imTJkyZcp0n8lLBvFMmTJlypTpPpOXDOKZMmXKlCnTfSYvGcQzZcqUKVOm+0xeMohnypQpU6ZM95m8ZBDPlClTpkyZ7jN5WSXEb8mNaMP88ht568absjl1G5dv+wvpTO1fvf64/Xsy884dGT+U3r9SffLQj+TG+3pyWt5957bUVKWPTVPnD227G3Pp9XX...

I then use PHPMailers AddStringAttachment:

$mail->AddStringAttachment($base64image, "Something Something.png", "base64", "image/png")

This sends fine (There are other settings but they are not relevant). However, once I receive the email, it says that the file is corrupt. Does anyone know the correct routine for sending base64 images in PHPMailer?

Edit 1

I removed all my modifications of the image, and am now sending it straight from the database. It comes out as:

ZGF0YTppbWFnZS9wbmc7YmFzZTY0LGlWQk9SdzBLR2dvQUFBQU5TVWhFVWdBQUFmRUFBQU96Q0FZQUFBQ29QVDh6QUFBQUFYTlNSMElBcnM0YzZRQUFBQVJuUVUxQkFBQ3hqd3Y4WVFVQUFBQUpjRWhaY3dBQURzTUFBQTdEQWNkdnFHUUFBUCBsU1VSQlZIaGU3UDNuYzEzWmRmYUw2bSA0WC9peDY5VDlvS3B6VDVVIGRwMTd6em45IHJVa0tsT3laYlV0eTJwbiB2VnJtYllsdXlWMWp1eEFFZ3dJQkFrU09lZEFCQUlnU0lKRUlnSXptTmxKVGNXbXVpVTFGZXd6N3ZPTXNjWmVjeSBzRFFJZ1dpM3FyTm4xMURObVdHRnZvdmR2anJuV1h2c2ovOHZYOTBpbVRKa3laY3FVNmY3UiBQaTQ2aU1mL1VhTFpNcVVLVk9tVEpudUg3M3h4aHVxai95dmp3NUtwa3laTW1YS2xPbiAwUzkgOFF2VlIvNC9qeDJSVEpreVpjcVVLZFA5by8vNnIvOVNmZVIvZS95b1pNcVVLVk9tVEpudUgzbkpJSjRwVTZaTW1UTGRaL0tTUVR4VHBreVpNbVc2eiBRbGczaW1USmt5WmNwMG44bExCdkZNbVRKbHlwVHBQcE9YRE9LWk1tWEtsQ25UZlNZdkdjUXpaY3FVS1ZPbSAweGVNb2hueXBRcFU2Wk05NW04WkJEUGxDbFRwa3laN2pONVdTWEViOG1OYU1QODhodDU2OGFic2psMUc1ZHYgd3ZwVE8xZnZmNjQvWHN5ODg0ZEdUIFUzcjlTZmZMUWogVEcgM3B5V3Q1OTU3YlVWS1dQVFZQbkQyMjdHM1BwOVhYVHRndFNjIFVYOHU2dmJmOHM3NzczTSBudlBwVSBQbE9tVEpreS9WN0t5enBCM01xUHJpeW1iUE5CNmFyTUtIaC9JelAzQXZGdGI4cGlBT...

This still gives an error when attempting to open.

Resolution

$base = base64_decode($row['image']);
$resource = base64_decode(str_replace(" ", "+", substr($base, strpos($base, ","))));
$mail->addStringAttachment($resource, "Filename.png", "base64", "image/png");

Turns out I was only doing a single decode, when I needed to do 2 in order to get the binary data. Thank you to those who commented.

If you are attaching a file that was previously a data URI, posted directly from javascript as string for example, you maybe do not need to double-decode:

$base = $_POST['image'];
$resource = base64_decode(str_replace(" ", "+", substr($base, strpos($base, ","))));
$mail->addStringAttachment($resource, "Filename.png");
like image 930
JosephGarrone Avatar asked Aug 13 '14 23:08

JosephGarrone


1 Answers

I happened to have this issue now. Anyway, the solution is, if you check the source code of PHPMailer, it has stated clearly that, the first parameter of the function, is taking the binary data of the file.

Which means, to successfully send the image, if you already have the base64_encoded representation of your file, you should be just do base64_decode it without changing anything, and then pass it in. It should work.

You do not need to replace any strings from the long base64 encoded string. (Of course, decode it without the data:image/png;base64, heading texts).

like image 69
Lionel Chan Avatar answered Nov 15 '22 08:11

Lionel Chan