I'm having trouble getting an inline image using PHPMailer. Without the following line (i.e. if I comment it out)
$success = $mail->AddEmbeddedImage($ImagePath, $ImageCID, $ImageName, $ImageEncoding, $ImageType, $ImageDisposition);
good results. If I add it back I get a blank email (tried Outlook and Gmail). I added "$success=" to make sure it was working and it did.
<?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("xx@gmail.com, XX", "xx@example.com, 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 = "exampele@gmail.com"; $mail->Password = "xxxxxxxxxxxxxxxxxxx"; // $mail->SMTPDebug = SMTP::DEBUG_SERVER; if (!isset($params)) { die("Kaput!"); } $SendTo = isset($params["SendTo"]) ? $params["SendTo"] : array("xx@example.com, 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('admin@example.com', 'example Admin'); $mail->addReplyTo('admin@example.com'); if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } } ?>
This is the screen output:
Pictures: ../images/Logo.jpg,Logo,Logo.jpg,,,inline
Pictures: ../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) "" ["Sender"]=> String(18) "admin@example.com" ["Sender Name"]=> String(14) "Example Admin"["Sender"]=> string(18)"admin@example.com" ["Subject"]=> string(27) "This is the default theme" ["Body"]=> string(215) "This is a test from SendEmail containing default text in the message body.There is also 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) "examplemail@gmail.com" ["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) "aa@gmail.com" 1=> string(10) "AA" } 1=> array(2) { [0]=> String(15) "aa@example.com" 1=> String(13) "A/example" } } ["cc":protected]=> array(0) { } ["bcc":protected]=> array(0) { } ["ReplyTo":protected]=> array(1) { ["admin@example.com"]=> array(2) { [0]=> String(18) "admin@example.com" 1=> String(0) "" } } ["all_recipients":protected]=> array(2) { ["aa@gmail.com"]=> bool(true) ["ag@example.com"]=> 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) "" ["exception": protected] = > bool(false) ["uniqueid":protected]=> string(0) "" } Message has been sent!
I couldn't find any comprehensive documentation either - not even on GitHub: 3731. The closest I got was this. But I still don't understand what exactly the third parameter is doing. Do I want to override the name? Why? What encoding should I use (I tried Base64 and left both blank)? What options exist besides "inline" disposal? etc...
P粉1649427912024-03-29 10:12:27
First of all, it seems that you are using an old version of PHPMailer because you are referencing a file (the autoloader) that has not been part of the library for years, so please update.
Your code looks like it's based on a very old example. You should not set up Mailer
yourself – isSMTP()
will do this for you.
If the file on disk has a different name that you don't want to expose to recipients, you may need to override that name, for example you may have logo-123456.png
locally and want to append it as logo.png
.
encoding will almost always need to be base64
as images tend to be binary and therefore not email secure, so leave it as default.
The MIME type should match the file extension, so you should be able to ignore this as well (PHPMailer will set it for you) unless you have a very specific reason to set a MIME type that doesn't em> match yours Content.
$disposition
Set the RFC2183. It doesn't make much sense to use anything other than inline for embedded images, so you should leave it at the default as well. This option exists because the RFC states that it should exist.
The upshot of all this is that all you really need to provide is the path to the file and the cid
.
It might help if you display SMTP logging (setting SMTPDebug = 2
), as this will show exactly what is happening with your messages.