Unable to embed base64 image URI in mail using PHPMailer

Philippe Vermeulen

I am generating QR codes using https://github.com/endroid/qr-code and getting their URI to embed them in a mail using PHPMailer (https://github.com/PHPMailer/PHPMailer).

building the mail object:

$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->isSMTP();
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->SMTPDebug = 1; 
$mail->Host = "smtp.gmail.com";
$mail->Port = "587";
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->setFrom("somewhere");
$mail->isHTML(true);
$mail->Subject = "QR";

And then generating the URI and embedding it in the mail

$qrURI = generateQR($requestId)
$mail->addStringEmbeddedImage($qrURI,"qr","qr.png",'base64','image/png');
$mail->Body = 'QR code: <img src="cid:qr" alt="QR code">'

The result in the mail sent is this: problem

I have the SMTPDebug=1 and it is not giving me any error

Here is a URI that I'm generating as an example:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAFACAIAAABC8jL9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFEUlEQVR4nO3dQY7bQBAEQdPw/78sP2HHBhutJCPOC61EKTGnwlyfz+cX0PR7+w0A/0/AECZgCBMwhAkYwgQMYQKGMAFDmIAhTMAQJmAIEzCE/Tn8u+u6Rt/HM5wvQ573PN/82SccPk8nMIQJGMIEDGEChjABQ5iAIUzAECZgCBMwhAkYwgQMYQKGMAFD2Oka6dzz7mqZWM9MbHd2X3OC39KPnMAQJmAIEzCECRjCBAxhAoYwAUOYgCFMwBAmYAgTMIQJGMIEDGH3r5HO7d6R8+aly5s/+4TF5+kEhjABQ5iAIUzAECZgCBMwhAkYwgQMYQKGMAFDmIAhTMAQJmAI21wjseXNu6WHcQJDmIAhTMAQJmAIEzCECRjCBAxhAoYwAUOYgCFMwBAmYAgTMIRZI3278z3QxMbIbunLOYEhTMAQJmAIEzCECRjCBAxhAoYwAUOYgCFMwBAmYAgTMIQJGMI210j2K/ea2BhVvPa35ASGMAFDmIAhTMAQJmAIEzCECRj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AIEzCECRjCrsMhxe7WpGJxlTKksoU6t/vkb3+eTmAIEzCECRjCBAxhAoYwAUOYgCFMwBAmYAgTMIQJGMIEDGEChrDTNRLwhZzAECZgCBMwhAkYwgQMYQKGMAFDmIAhTMAQJmAIEzCECRjC/gIsHA2FiBfxDQAAAABJRU5ErkJggg==

EDIT: It seems the format of data is wrong, how should I output the QR code so i can embed it in the mail?

Here is my generateQR function after trying out what @Synchro said:

function generateQR($requestId){
    try {
        $writer = new PngWriter();
        $qrCode = QrCode::create($requestId)
        ->setEncoding(new Encoding('UTF-8'))
        ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
        ->setSize(300)
        ->setMargin(10)
        ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
        ->setForegroundColor(new Color(0, 0, 0))
        ->setBackgroundColor(new Color(255, 255, 255));

        $result = $writer->write($qrCode);
        echo $result->getString()
        } 
    catch (\Throwable $th) {
            echo $th;
        }
}
Synchro

PHPMailer doesn't have any support for data URIs, largely because it's not a good idea to use them because they have so many compatibility issues across email clients. Better to use a regular embedded image. To do that all you need to do is pass binary PNG data into that first param of the call to addStringEmbeddedImage instead of the URI-encoded data. I assume you'll need to amend your generateQR function to give you the data in that format.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive