Nilushan Costa
Nilushan Costa

Reputation: 306

Issue when embedding image in email body with PHPMailer

I want to generate a QR code using the phpqrcode library and send it as an image embedded in the email body (without attaching it to the email). I am using the PHPMailer library to create and send the email.

The code I am using is as follows

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

include ('../phpqrcode/qrlib.php');
ob_start();
QRcode::png('TextToGenerateTheQRCodeFrom');
$imageString = base64_encode( ob_get_contents() );
ob_end_clean();

$mail = new PHPMailer;
$mail->setFrom( '[email protected]', 'Test QR sender');
$mail->addAddress('xxxxxxxxxxx', 'John Doe');
$mail->Subject  = 'QR code';
$mail->isHTML(true);
$mail->addStringEmbeddedImage($imageString,'qrcode');

$mail->Body = "<p> Your QR code </p><img src=\"cid:qrcode\" />";

if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>

This is the email I receive.

.

As you can see, the image is not embedded within the email body. However there is a file attached. That file is a text file and contains the base64 value of the image.

What am I doing wrong and what can I do to fix it?

Upvotes: 0

Views: 2746

Answers (3)

sagar
sagar

Reputation: 94

This will work:

$imgSrc = str_replace('amp;','', 'https://chart.googleapis.com/chart? 
cht=qr&chs=250x250&choe=UTF-8');    

$mail->setFrom('[email protected]', 'Chris');
$mail->addAddress('[email protected]');

$mail->isHTML(true);
$mail->Subject = 'This is a PHPMailer Test';
$mail->msgHTML("<p>this is a <strong>test</strong> email</p><img src='$imgSrc'>");
if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

Upvotes: 0

Synchro
Synchro

Reputation: 37710

It would help if you read the documentation on the function you're using. This is linked from the readme, also on the project page on github.

The reason this is if not working is that you're passing it some binary data without telling it what kind of thing it is, so it falls back to a generic application/octet-stream MIME type, which will appear as a generic attachment, as you're seeing.

Chris' example works because he also supplied a filename containing an extension, and PHPMailer uses that to derive a MIME type (if it can) if you don't provide an explicit MIME type in the call to addStringEmbeddedImage.

In short, change your call to this:

$mail->addStringEmbeddedImage($imageString, 'qrcode', 'qrcode.png');

Upvotes: 0

Chris
Chris

Reputation: 4728

I have set something up myself and can confirm this this code works. I've sent an email to my Gmail account. I'll include a screenshot as proof ;-)

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$image = 'http://cdnqrcgde.s3-eu-west-1.amazonaws.com/wp-content/uploads/2013/11/jpeg.jpg';
$image = file_get_contents($image);

$body = '<p>this is a <strong>test</strong> email</p><p><img src="cid:qrcode" /></p>';

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->SMTPAuth = false;

    $mail->setFrom('[email protected]', 'Chris');
    $mail->addAddress('[email protected]');

    $mail->isHTML(true);
    $mail->Subject = 'This is a PHPMailer Test';
    $mail->Body    = $body;
    $mail->AltBody = $body;

    $mail->addStringEmbeddedImage($image,'qrcode','qrcode.jpg');

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

image

Upvotes: 1

Related Questions