vanilla php convert base64 to image in email attachment

I have a php script that sends me an email. Within this email data, I send base64 image data, but what I would like is that the base64 gets converted into a jpeg which then gets added as an attachment via email.

<?php
header('Access-Control-Allow-Origin: *');

if(isset($_POST['email'])){
    $to      = '[email protected]';
    $subject = "New submission from ".$_POST['email'];
    $message = $_POST['message'];
    $image = base64_to_jpeg( $_POST['image'], 'image.jpeg' ); //this is base64 image
    $subscribe = $_POST['subscribe'];
    $headers = "From: ".$_POST['email']." <".">\r\n"; $headers = "Reply-To: ".$_POST['email']."\r\n";
    $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
    'X-Mailer: PHP/' . phpversion();
    if(mail($to, $subject, $image, $headers)) echo json_encode(['success'=>true]);
    else echo json_encode(['success'=>false]);
    exit;
 }

function base64_to_jpeg($base64_string, $output_file) {
    // open the output file for writing
    $ifp = fopen( $output_file, 'wb' );

    // split the string on commas
    // $data[ 0 ] == "data:image/png;base64"
    // $data[ 1 ] == <actual base64 string>
    $data = explode( ',', $base64_string );

    // we could add validation here with ensuring count( $data ) > 1
    fwrite( $ifp, base64_decode( $data[ 1 ] ) );

    // clean up the file resource
    fclose( $ifp );

    return $output_file;
}
?>

$image is data:image/png;base64,iVBORw0KGg

The issue I'm having is that when the email gets sent, the email only inclues plain text "image.jpeg" rather than showing the image, or attaching the image...

Any idea how I can fix this?

Thank you.

Answers 1

  • Passing $image to the body of the mail would obviously send the name, because that's what you're returning from the function.

    But if you pass it with an image tag;

    mail($to, $subject, '<img src="' . $image . '" alt="'. $image . '">', $headers); // I used PHP template strings to create the img tag
    

    You're surely going to get the data string passed into the mail's body

    Here's a screenshot with the link you posted me Screenshot of the final image

    This is the complete code I'm running base64_to_jpeg is your function I got from the question

    <?php
    
    function base64_to_jpeg($base64_string, $output_file)
    {
        // open the output file for writing
        $ifp = fopen($output_file, 'wb');
    
        // split the string on commas
        // $data[ 0 ] == "data:image/png;base64"
        // $data[ 1 ] == <actual base64 string>
        $data = explode(',', $base64_string);
    
        // we could add validation here with ensuring count( $data ) > 1
        fwrite($ifp, base64_decode($data[1]));
    
        // clean up the file resource
        fclose($ifp);
    
        return $output_file;
    }
    
        $image = base64_to_jpeg(base64_string_from_link, 'image.jpeg')
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Base 64</title>
    </head>
    <body>
        <?php echo '<img src="' . $image . '" alt="' . $image . '">'; ?>
    </body>
    </html>
    

    I understand your problem now, When you pass the $image to the mail's body, the alt text is showing simply because, the mail doesn't know where to download the image from.

    When you're sending images through mail, the image links have to be absolute paths for the mail to know where to download the image from.

    Therefore, either you upload that image to your websites file manager and use an absolute path like so, <img src="https://picsum.photos/id/237/200/300">

    or

    don't convert the image to .jpeg, just pass the base 64 string as the image's src <img src="data:image/png;base64,iVBORw0KGg">

    I also saw in your code that you're not concatenating the $headers together rather just resetting the value. (i.e. you're using = instead of .=)


Related Articles