首页  >  问答  >  正文

PHPMailer 的 AddEmbeddedImage 给我空白电子邮件

我在使用 PHPMailer 获取内联图像时遇到问题。没有以下行(即如果我将其注释掉)

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

效果很好。如果我重新添加它,我会收到一封空白电子邮件(尝试过 Outlook 和 Gmail)。我添加了“$success =”以确保它正在工作并且确实如此。

<?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!";
    }
}
?>

这是屏幕输出:

图片:../images/Logo.jpg、Logo、Logo.jpg、、、内联

图片:../podbanks/images/XX.jpg、XX、XX.jpg、、、内联


邮件:对象(PHPMailer\PHPMailer\PHPMailer)#1 (71) { ["优先级"]=> NULL ["CharSet"]=> 字符串(10) "iso-8859-1" ["ContentType"]=> 字符串(9) "text/html" ["编码"]=> 字符串(4) "8bit" ["ErrorInfo"]=> 字符串(0) "" ["发件人"]=> 字符串(18) "admin@example.com" ["发件人名称"]=> 字符串(14) “示例管理员”[“发件人”]=> string(18)“admin@example.com” ["Subject"]=> string(27) "这是默认主题" ["Body"]=> string(215) "这是来自 SendEmail 的测试,包含默认文本 在消息正文中。还有一个图像..." ["AltBody"]=> string(0) "" ["Ical"]=> 字符串(0) "" ["MIMEBody":protected]=> 字符串(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) "" ["主机名"]=> 字符串(0) "" ["MessageID"]=> 字符串(0) "" ["MessageDate"]=> 字符串(0) "" ["主机"]=> 字符串(14) "smtp.gmail.com" ["端口"]=> int(587) ["Helo"]=> 字符串(0) "" ["SMTPSecure"]=> 字符串(3) “tls” [“SMTPAutoTLS”]=> bool(true) [“SMTPAuth”]=> bool(true) ["SMTPOptions"]=> 数组(0) { } ["用户名"]=> 字符串(22) “examplemail@gmail.com” [“密码”]=> string(16) “shotlzacfvdubehm” ["AuthType"]=> 字符串(0) "" ["oauth":protected]=> NULL ["超时"]=> 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"]=> 字符串(0) "" ["DKIM_passphrase"]=> 字符串(0) "" ["DKIM_domain"]=> 字符串(0) "" ["DKIM_private"]=> 字符串(0) "" ["DKIM_private_string"]=> 字符串(0) "" ["action_function"]=> 字符串(0) "" ["XMailer"]=> 字符串(0) "" ["smtp":protected]=> NULL ["to":protected]=> 数组(2) { [0]=> 数组(2) { [0]=> 字符串(19) "aa@gmail.com" 1=> 字符串(10) "AA" } 1=> 数组(2) { [0]=> 字符串(15) "aa@example.com" 1=> 字符串(13) "A/example" } } ["cc":protected]=> 数组(0) { } ["bcc":protected]=> 数组(0) { } ["ReplyTo":protected]=> 数组(1) { ["admin@example.com"]=> 数组(2) { [0]=> 字符串(18) "admin@example.com" 1=> 字符串(0) "" } } ["all_recipients":protected]=> 数组(2) { ["aa@gmail.com"]=> bool(true) ["ag@example.com"]=> bool(true) } ["RecipientsQueue":protected]=> 数组(0) { } ["ReplyToQueue":protected]=> array(0) { } ["attachment":protected]=> 数组(2) { [0]=> 数组(8) { [0]=> 字符串(31) "../images/clients/Logo.jpg" 1=> string(13) "Logo.jpg" 2=> 字符串(13) "Logo.jpg" [3]=> 字符串(0) "" [4]=> 字符串(10) "图像/jpeg" [5]=> bool(false) [6]=> string(6) "内联" [7]=> string(9) "徽标" }1=> 数组(8) { [0]=> 字符串(35) "../images/XX.jpg" 1=> 字符串(16) "XX.jpg" 2=> 字符串(16) "XX.jpg" [3]=> 字符串(0) "" [4]=> 字符串(10) “图像/jpeg” [5]=> bool(false) [6]=> 字符串(6) “内联”[7]=> string(3) "XX" } } ["CustomHeader":protected]=> array(0) { } ["lastMessageID":protected]=> string(0) "" ["message_type":protected]=> string(0) "" ["boundary":protected]=> 数组(0) { } ["语言":受保护]=> 数组(0) { } ["error_count":protected]=> int(0) ["sign_cert_file":protected]=> 字符串(0) "" ["sign_key_file":protected]=> 字符串(0) "" [“sign_extracerts_file”:受保护]=> string(0)“” [“sign_key_pass”:受保护] = > 字符串(0)“” [“例外”:受保护] = > bool(false) ["uniqueid":protected]=> string(0) "" } 消息已发送!

我也找不到任何综合文档 - 甚至在 GitHub 上也找不到:3731。我最接近的是这个。但我仍然不明白第三个参数到底在做什么。我想覆盖该名称吗?为什么?我应该使用什么编码(我尝试了 Base64 并将两者都留空)?除了“内联”处置之外,还存在哪些选项?等等...

P粉702946921P粉702946921179 天前263

全部回复(1)我来回复

  • P粉164942791

    P粉1649427912024-03-29 10:12:27

    首先,您似乎正在使用旧版本的 PHPMailer,因为您引用了一个多年来一直不属于库的文件(自动加载器),因此请更新。

    您的代码看起来像是基于一个非常古老的示例。您不应该自己设置 MailerisSMTP() 会为您完成此操作。

    如果磁盘上的文件具有您不想向收件人公开的不同名称,您可能需要覆盖该名称,例如您本地可能有 logo-123456.png,并希望将其附加为 logo.png

    编码几乎总是需要为 base64,因为图像往往是二进制的,因此电子邮件不安全,因此请将其保留为默认值。

    MIME 类型应该与文件扩展名匹配,因此您也应该能够忽略这一点(PHPMailer 将为您设置它),除非您有非常具体的原因来设置不匹配的 MIME 类型 em> 匹配您的内容。

    $disposition 设置来自 RFC2183。对于嵌入图像使用除内联之外的任何内容都没有多大意义,因此您也应该将其保留为默认值。该选项存在是因为 RFC 规定它应该存在。

    所有这些的结果是,您实际需要提供的只是文件的路径和 cid

    如果您显示 SMTP 记录(设置 SMTPDebug = 2),可能会有所帮助,因为这将准确显示您的邮件中发生的情况。

    回复
    0
  • 取消回复