首頁  >  問答  >  主體

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粉702946921178 天前261

全部回覆(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
  • 取消回覆