Sylar
Sylar

Reputation: 12092

How to embed a base64 data into Laravel 6 email

I'm string to embed an image raw data into any email client using Laravel 6 Embedding Raw Data Attachments.

In my test.blade.php:

<body>
    Here is an image from raw data:

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

public function build()
    {
        return $this->view('emails.test')
                    ->with([
                        'data' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA.....',
                        'name' => 'foobar',
                       ]);
    }

I've created a route to view the email and the image wont show. If I do:

[...]
<img src="{{ $data }}">

The image shows (in browser) but not in the email. Fair enough. How to get $message->embedData($data, $name) to work? Am I missing something from the docs?

Upvotes: 1

Views: 2195

Answers (1)

Sylar
Sylar

Reputation: 12092

I had to analysed my post about image upload and see what I did there. So the php version is:

        $data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA.....";
        $img = preg_replace('/^data:image\/\w+;base64,/', '', $data);
        $type = explode(';', $data)[0];
        $type = explode('/', $type)[1];
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);
        return $this->view('emails.test')
                    ->with([
                        'name' => 'foobar',
                        'type' => $type, // png, jpg etc
                        'base64Data' => $data
        ]);

Then in the blade template:

<img src="{{ $message->embedData($base64Data, $name . '.' . $type) }}">

Hope this helps others who are looking.

Upvotes: 4

Related Questions