hetal gohel
hetal gohel

Reputation: 355

Laravel : image with base64 source ,not able to view in email

I am working with laravel below is steps which I have followed,

I have checked that base64 string is correct and showing me image also able to display that image with my system

but when I am going to send that image in email ,

that image is not visible for me.

I have checked there in image src attribute, there is showing like -

<img style="width:224px">

in gmail ,here src attribute is removed ,but I have checked that tag befor send email , there was src tag with proper base64 encoded string

Please help me to show image in email ,

To send mail I am using laravel own configuration for email as below,

use App\Events\SendMail;

$mailData['toName']       = $toUserFullname;
$mailData['toEmail']      = $toEmail;
$mailData['fromName']     = $fromName;
$mailData['fromEmail']    = $fromEmail;
$mailData['emailSubject'] =  $templateData['email_subject'];
$mailData['emailContent'] = $templateData['email_body'];
\Event::fire(new SendMail($mailData));

Upvotes: 0

Views: 3353

Answers (2)

Umair Khan
Umair Khan

Reputation: 1

I'm working with a Laravel application that uses an HTML editor where users can upload images. These images are embedded as Base64 strings in the HTML content. I want to convert these Base64 image tags to image paths stored on the server (specifically using Amazon S3). Below is a custom code snippet I implemented to achieve this.

preg_match_all('/<img[^>]+src="data:image\/[^;]+;base64,[^"]+"[^>]*>/i', $data['html'], $matches);

foreach ($matches[0] as $imgTag) {
    preg_match('/src="(data:image\/[^;]+;base64,[^"]+)"/', $imgTag, $srcMatch);

    if (isset($srcMatch[1])) {
        // Use a different variable name to avoid overwriting $data
        list(, $base64Data) = explode(',', $srcMatch[1]);
        $imageData = base64_decode($base64Data);

        // Create a temporary file to store the image
        $tempFilePath = tempnam(sys_get_temp_dir(), 'upload') . '.jpg';
        file_put_contents($tempFilePath, $imageData);

        // Upload the image to S3 or local
        $filePath = $this->uploadOne(new UploadedFile($tempFilePath, 'image.jpg'), 'images', null, 's3'); // Specify your S3 disk
        
        // Get the URL of the uploaded image
        $s3Url = Storage::disk('s3')->url($filePath);
        
        // Replace the Base64 src with the S3 URL in the HTML
        $data['html'] = str_replace($srcMatch[0], 'src="' . $s3Url . '"', $data['html']);
        
        // Delete the temporary file
        unlink($tempFilePath);
    }
}

Upvotes: -1

Volod
Volod

Reputation: 1437

Base64 encoded images are not well supported in emails. They aren't supported in most web email clients (including Gmail) and are completely blocked in Outlook.

So the first solution that comes to my mind is to create actual image file from base64 string before sending email. Something like that colud help:

$filename_path = md5(time().uniqid()).".jpg"; 
$decoded=base64_decode($base64_string_img); 
file_put_contents("path/to/save/image/".$filename_path,$decoded);

You also shouldn't forget to unlink this images in case you don't need them after.

You can also try using inline attachemnts.

<img src="{{ $message->embedData($data, $name) }}">

Upvotes: 1

Related Questions