PHPMailer send base64 image

I am trying to send an image from MySQL database via PHPMailer.

I am currently pulling the image out of the database base64_decode

, and then replacing all the spaces with pluses 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...

      

Then I use PHPMailers AddStringAttachment

:

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

      

This posts fine (there are other settings, but they are not relevant). However, as soon as I receive the email, it says the file is corrupted. Does anyone know the correct procedure for sending base64 images to PHPMailer?

Edit 1

I removed all my modifications to the image, and now I'm posting it straight from the database. It looks like:

ZGF0YTppbWFnZS9wbmc7YmFzZTY0LGlWQk9SdzBLR2dvQUFBQU5TVWhFVWdBQUFmRUFBQU96Q0FZQUFBQ29QVDh6QUFBQUFYTlNSMElBcnM0YzZRQUFBQVJuUVUxQkFBQ3hqd3Y4WVFVQUFBQUpjRWhaY3dBQURzTUFBQTdEQWNkdnFHUUFBUCBsU1VSQlZIaGU3UDNuYzEzWmRmYUw2bSA0WC9peDY5VDlvS3B6VDVVIGRwMTd6em45IHJVa0tsT3laYlV0eTJwbiB2VnJtYllsdXlWMWp1eEFFZ3dJQkFrU09lZEFCQUlnU0lKRUlnSXptTmxKVGNXbXVpVTFGZXd6N3ZPTXNjWmVjeSBzRFFJZ1dpM3FyTm4xMURObVdHRnZvdmR2anJuV1h2c2ovOHZYOTBpbVRKa3laY3FVNmY3UiBQaTQ2aU1mL1VhTFpNcVVLVk9tVEpudUg3M3h4aHVxai95dmp3NUtwa3laTW1YS2xPbiAwUzkgOFF2VlIvNC9qeDJSVEpreVpjcVVLZFA5by8vNnIvOVNmZVIvZS95b1pNcVVLVk9tVEpudUgzbkpJSjRwVTZaTW1UTGRaL0tTUVR4VHBreVpNbVc2eiBRbGczaW1USmt5WmNwMG44bExCdkZNbVRKbHlwVHBQcE9YRE9LWk1tWEtsQ25UZlNZdkdjUXpaY3FVS1ZPbSAweGVNb2hueXBRcFU2Wk05NW04WkJEUGxDbFRwa3laN2pONVdTWEViOG1OYU1QODhodDU2OGFic2psMUc1ZHYgd3ZwVE8xZnZmNjQvWHN5ODg0ZEdUIFUzcjlTZmZMUWogVEcgM3B5V3Q1OTU3YlVWS1dQVFZQbkQyMjdHM1BwOVhYVHRndFNjIFVYOHU2dmJmOHM3NzczTSBudlBwVSBQbE9tVEpreS9WN0t5enBCM01xUHJpeW1iUE5CNmFyTUtIaC9JelAzQXZGdGI4cGlBT...

      

It still gives an error when trying to open.

Resolution

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

      

It turns out I was only doing one decoder when I needed to do 2 to get binary data. Thanks to those who commented.

+3


source to share


1 answer


I had this problem now. Anyway, the solution is, if you check the PHPMailer source code , it clearly stated that the first parameter of the function accepts the binary data of the file.

This means that in order to send the image successfully, if you already have a base64_encoded representation of your file, you should simply render base64_decode

it without changing anything and then transfer it. It should work.



You don't need to replace strings from a long base64 encoded string. (Decode it without header texts of course data:image/png;base64,

).

+2


source







All Articles