Ad

Sending Base64 Images Using Laravel

I am using Laravel to send emails and plain text works fine. However, I am having an issue attaching images to the email. As of right now, images are stored in the DB as Base64 Strings.

This is the code that calls the Mail facade in Laravel:

            Mail::queue('emails.infraction', $data, function($message) use ($email){
            $message -> from($fromEmail, 'Blah);
            $message -> to($toEmail) -> subject('Testing');

        });

Basically the $data variable contains all the information I will be displaying in the HTML email that is being sent. I can access the variables just fine when displaying them in the email but I am having issues attaching the base64 images into the email.

This is what I have tried so far:

        Evidence of Intractis what I have tried so far:

        Evidence of Intraction:</p>
            <?php 
                $elementCount  = count($photos);
                for($x=0; $x<$elementCount; $x++){
                    echo '<p style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; margin-left: 80px;">';
                    echo "<img src=" . $message->embedData($data['photos'][$x], 'Blah') . ">";
                    echo "</p>";
                }
            ?>

But as you can see here, gmail does not properly encode that rawDataString into the image:

laravel

It just displays as if a picture was there but not the contents. I have verified the base 64 string and it is correct.

What could be the error?

Laravel Mail Documentation says to use something like this in the message builder:

laravel mailer

but I am a little lost on how to do that properly. The other option would be to convert the base64 string into an image on the fly and send it as an attachment to the user but I cannot store the images into the server and thus would have to convert it to an image within php and sending it through laravel mailer without an actual filepath.

Please advise

Ad

Answer

You have to decode the image first like this:

$message->embedData(base64_decode($data['photos'][$x]), 'Blah')
Ad
source: stackoverflow.com
Ad