Chiwda
Chiwda

Reputation: 1344

PHPMailer's AddEmbeddedImage gives me blank emails

I am having a problem getting inline images to work using PHPMailer. Without the following line (i.e. if I comment it out)

$success = $mail->AddEmbeddedImage($ImagePath, $ImageCID, $ImageName, $ImageEncoding, $ImageType, $ImageDisposition);

it works fine. If I add it back in, I get a blank email (tried Outlook as well as Gmail). I added the "$success = " to make sure it was working and it is.

<?php
$BaseURL = "../other/";
require_once($BaseURL . 'PHPMailer-master/src/PHPMailerAutoload.php');
require_once($BaseURL . 'PHPMailer-master/src/PHPMailer.php');
require_once($BaseURL . 'PHPMailer-master/src/SMTP.php');
require_once($BaseURL . 'PHPMailer-master/src/Exception.php');

use PHPMailer\PHPMailer\PHPMailer;

error_reporting(E_ALL);
ini_set('display_errors', 1);
// Test data begin
$encoding =""; // Also tried "base64"
$type=""; // Also tried "application/octet-stream" and "image/jpeg"
$disposition = "inline";
$ImageArray = array("../images/Logo.jpg|Logo|Logo.jpg|" . $encoding . "|" . $type . "|" . $disposition,
                    "../images/XX1.jpg|XX1|XX1.jpg|" . $encoding . "|" . $type . "|" . $disposition);
$Body = '<html><body style="font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;">';
$Body .= 'This is a test from SendEmail and contains default text in the message body.';
$Body .= 'And an image... <img src="cid:Logo">';
$Body .= '<br><br></body></html>';
$SendParams = array("SendTo"=>array("[email protected], XX", "[email protected], XX/example"), 
                    "Body"=>$Body,
                    "Subject"=>"This is the default Subject",
                    "Images"=>$ImageArray
                    );
SendEmail($SendParams);
// End Test data and calls

function SendEmail($params) {
    $mail = new PHPMailer;
    $mail->IsHTML(true);
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = "smtp.gmail.com";
    $mail->Mailer = "smtp";
    $mail->Port = 587; //use port 465 when using SMPTSecure = 'ssl'
    $mail->Username = "[email protected]";
    $mail->Password = "xxxxxxxxxxxxxxxxxxx";    // $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    if (!isset($params)) {
        die("Kaput!");
    }
    $SendTo = isset($params["SendTo"]) ?  $params["SendTo"] : array("[email protected], XX/example");
    $Body = isset($params["Body"]) ?  $params["Body"] : "No Message Body - may be a Test!";
    $Subject = isset($params["Subject"]) ?  $params["Subject"] : "No Message Subject - may be a Test!";
    foreach($SendTo as $recipient) {
        $recipient_array = explode(",", $recipient);
        $SendEmail = $recipient_array[0];
        $SendName = $recipient_array[1];
        $mail->AddAddress($SendEmail, $SendName);
    }
    if (isset($params["Images"])) {
        foreach($params["Images"] as $ImageData) {
            $ImageData_array = explode("|", $ImageData);
            $ImagePath = $ImageData_array[0];
            $ImageCID = $ImageData_array[1];
            $ImageName = $ImageData_array[2];
            $ImageEncoding = $ImageData_array[3];
            $ImageType = $ImageData_array[4];
            $ImageDisposition = $ImageData_array[5];
            // Below is the line causing the problem
            $success = $mail->AddEmbeddedImage($ImagePath, $ImageCID, $ImageName, $ImageEncoding, $ImageType, $ImageDisposition);
            // The below prints expected values for the variables ("Failed!" does not print)
            if ($success)
                echo "<br><br>Image: " . $ImagePath . ", " . $ImageCID . ", " . $ImageName . ", " . $ImageEncoding . ", " . $ImageType . ", " . $ImageDisposition;
            else
                echo "Failed!";
        }
    }
    $mail->Body = $Body;
    $mail->Subject = $Subject;
    $mail->SetFrom('[email protected]', 'example Admin');
    $mail->addReplyTo('[email protected]');
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message sent!";
    }
}
?>

This is the screen output:

Image: ../images/Logo.jpg, Logo, Logo.jpg, , , inline

Image: ../podbanks/images/XX.jpg, XX, XX.jpg, , , inline


mail: object(PHPMailer\PHPMailer\PHPMailer)#1 (71) { ["Priority"]=> NULL ["CharSet"]=> string(10) "iso-8859-1" ["ContentType"]=> string(9) "text/html" ["Encoding"]=> string(4) "8bit" ["ErrorInfo"]=> string(0) "" ["From"]=> string(18) "[email protected]" ["FromName"]=> string(14) "example Admin" ["Sender"]=> string(18) "[email protected]" ["Subject"]=> string(27) "This is the default Subject" ["Body"]=> string(215) "This is a test from SendEmail and contains default text in the message body.And an image..." ["AltBody"]=> string(0) "" ["Ical"]=> string(0) "" ["MIMEBody":protected]=> string(0) "" ["MIMEHeader":protected]=> string(0) "" ["mailHeader":protected]=> string(0) "" ["WordWrap"]=> int(0) ["Mailer"]=> string(4) "smtp" ["Sendmail"]=> string(18) "/usr/sbin/sendmail" ["UseSendmailOptions"]=> bool(true) ["ConfirmReadingTo"]=> string(0) "" ["Hostname"]=> string(0) "" ["MessageID"]=> string(0) "" ["MessageDate"]=> string(0) "" ["Host"]=> string(14) "smtp.gmail.com" ["Port"]=> int(587) ["Helo"]=> string(0) "" ["SMTPSecure"]=> string(3) "tls" ["SMTPAutoTLS"]=> bool(true) ["SMTPAuth"]=> bool(true) ["SMTPOptions"]=> array(0) { } ["Username"]=> string(22) "[email protected]" ["Password"]=> string(16) "shotlzacfvdubehm" ["AuthType"]=> string(0) "" ["oauth":protected]=> NULL ["Timeout"]=> int(300) ["SMTPDebug"]=> int(0) ["Debugoutput"]=> string(4) "html" ["SMTPKeepAlive"]=> bool(false) ["SingleTo"]=> bool(false) ["SingleToArray":protected]=> array(0) { } ["do_verp"]=> bool(false) ["AllowEmpty"]=> bool(false) ["DKIM_selector"]=> string(0) "" ["DKIM_identity"]=> string(0) "" ["DKIM_passphrase"]=> string(0) "" ["DKIM_domain"]=> string(0) "" ["DKIM_private"]=> string(0) "" ["DKIM_private_string"]=> string(0) "" ["action_function"]=> string(0) "" ["XMailer"]=> string(0) "" ["smtp":protected]=> NULL ["to":protected]=> array(2) { [0]=> array(2) { [0]=> string(19) "[email protected]" 1=> string(10) "AA" } 1=> array(2) { [0]=> string(15) "[email protected]" 1=> string(13) "A/example" } } ["cc":protected]=> array(0) { } ["bcc":protected]=> array(0) { } ["ReplyTo":protected]=> array(1) { ["[email protected]"]=> array(2) { [0]=> string(18) "[email protected]" 1=> string(0) "" } } ["all_recipients":protected]=> array(2) { ["[email protected]"]=> bool(true) ["[email protected]"]=> bool(true) } ["RecipientsQueue":protected]=> array(0) { } ["ReplyToQueue":protected]=> array(0) { } ["attachment":protected]=> array(2) { [0]=> array(8) { [0]=> string(31) "../images/clients/Logo.jpg" 1=> string(13) "Logo.jpg" 2=> string(13) "Logo.jpg" [3]=> string(0) "" [4]=> string(10) "image/jpeg" [5]=> bool(false) [6]=> string(6) "inline" [7]=> string(9) "Logo" } 1=> array(8) { [0]=> string(35) "../images/XX.jpg" 1=> string(16) "XX.jpg" 2=> string(16) "XX.jpg" [3]=> string(0) "" [4]=> string(10) "image/jpeg" [5]=> bool(false) [6]=> string(6) "inline" [7]=> string(3) "XX" } } ["CustomHeader":protected]=> array(0) { } ["lastMessageID":protected]=> string(0) "" ["message_type":protected]=> string(0) "" ["boundary":protected]=> array(0) { } ["language":protected]=> array(0) { } ["error_count":protected]=> int(0) ["sign_cert_file":protected]=> string(0) "" ["sign_key_file":protected]=> string(0) "" ["sign_extracerts_file":protected]=> string(0) "" ["sign_key_pass":protected]=> string(0) "" ["exceptions":protected]=> bool(false) ["uniqueid":protected]=> string(0) "" } Message sent!

I also can't find any comprehensive documentation - not even on GitHub :3731. The closest I have come is this. But I still don't understand exactly what that third parameter is doing. Do I want to override the name? Why? And what encoding should I use (I tried Base64 and blank both)? What options exist other than "inline" for disposition? And so on...

Upvotes: 0

Views: 467

Answers (1)

Synchro
Synchro

Reputation: 37710

Before anything else, it looks like you're using an old version of PHPMailer, as you have a reference to a file that has not been part of the library for several years (the autoloader), so update.

Your code looks like it is based on a very old example. You should not set Mailer yourself – isSMTP() does that for you.

You might want to override the name if the file on disk has a different name that you don't want to expose to the recipient, for example you might have logo-123456.png locally, and want to attach it as logo.png.

The encoding will nearly always need to be base64 because images tend to be binary and thus email-unsafe, so leave that as the default.

The MIME type should match the file extension, so you should be able to ignore that too (PHPMailer will set it for you), unless you have a very specific reason to set a MIME type that does not match your content.

$disposition sets the Content-Disposition header from RFC2183. It doesn't make much sense to use anything other than inline for an embedded image, so you should leave that as its default too. The option is there because the RFC says it should be.

The upshot of all that is that all you actually need to provide is the path to the file and the cid.

It might help if you showed your SMTP transcript (set SMTPDebug = 2) as that will expose exactly what's happening in your message.

Upvotes: 1

Related Questions