1

I am using endroid/qr-code https://github.com/endroid/qr-code to generate QrCode. Now I need to send this QR to my email too.

I have used following codes:

    $result = Builder::create()
        ->writer(new PngWriter())
        ->writerOptions([])
        ->data("This ist test page.")
        ->encoding(new Encoding('UTF-8'))
        ->size(200)
        ->margin(10)
        ->roundBlockSizeMode(new RoundBlockSizeModeMargin())
        ->logoPath($webPath)
        ->labelText('SCAN')
        ->build();

    return $result->getDataUri();

At my email twig page I have

<img src = {{ $return_from_result_getDataUri }} >

I am getting broken img from above line.

When I use {{ $return_from_result_getDataUri }}, this gives me data:image/png;base64..... text displayed.

Can anybody help me here.

Thanks.

S S
  • 1,443
  • 2
  • 12
  • 42
  • Please show us exactly what "broken image" is and also an example of a data URI that is "broken". – Sergiu Paraschiv May 29 '21 at 08:15
  • I already got the answer to my question. Broken image means image not showing. data URI is returning base64 encoded text as I have written in my question. – S S May 29 '21 at 10:30
  • I dont know why people here in SO likes to close questions often. – S S May 29 '21 at 10:32

2 Answers2

2

Support for Embedding images with Data URI in Emails is still not optimal and many email clients don't support it at all.

See this analysis from Campaign Monitor showing many clients not rendering the Data URI image.

I strongly suggest you to upload the QR code somewhere and then link it as a normal image.

If your services are hosted on AWS, you can use S3.

Otherwise you can use free services but I don't have any experience with them. This is the first result I found: https://apidocs.imgur.com/

PS: Just adding that you might be missing some double quotes as well in your code.

<img src="{{ $return_from_result_getDataUri }}" >
Andrea Olivato
  • 2,450
  • 1
  • 18
  • 30
1
<img src = {{ $return_from_result_getDataUri }} >

In PHP, we would write the above as follows

<img src="<?php echo $return_from_result_getDataUri; ?>" >

But this may not work with all email clients. There are two methods to overcome this issue.

Method 1: As Andrea Mentioned, Save the file somewhere

 $result->saveToFile(`__DIR__`.'/qrcode.png');

then use the url in html img tag.

<img src="<?php echo $fullImageUrl; ?>">

Method 2: Embed the image as an attachment and Refer it using cid(content id)

This is well explained here

Indra Kumar S
  • 2,818
  • 2
  • 16
  • 27