PHPMailer send base64 image

10,398

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).

Share:
10,398
JosephGarrone
Author by

JosephGarrone

I am in my 4th year of an Electrical + Computer Engineering/Computer Science dual major dual degree at the University of Queensland. Programming begun as a little hobby way back in year 7 and has now grown to become my part time job. I'm always looking to learn more and help overcome problems in unique and new ways.

Updated on June 08, 2022

Comments

  • JosephGarrone
    JosephGarrone almost 2 years

    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");