Ich habe Probleme beim Abrufen von Inline-Bildern mit PHPMailer. Ohne die folgende Zeile (also wenn ich sie auskommentiere)
$success = $mail->AddEmbeddedImage($ImagePath, $ImageCID, $ImageName, $ImageEncoding, $ImageType, $ImageDisposition);
Es funktioniert gro?artig. Wenn ich es wieder hinzufüge, erhalte ich eine leere E-Mail (habe es mit Outlook und Gmail versucht). Ich habe ?$success=" hinzugefügt, um sicherzustellen, dass es funktioniert, und das tat es auch.
<?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!"; } } ?>
Das ist die Bildschirmausgabe:
Bilder: ../images/Logo.jpg,Logo,Logo.jpg,,,inline
Bilder: ../podbanks/images/XX.jpg, XX, XX.jpg,,, inline
Mail: Object(PHPMailerPHPMailerPHPMailer)#1 (71) { ["Priority"]=> NULL ["CharSet"]=> string(10) "iso-8859-1" ["ContentType"]=> "text/html" ["Encoding"]=> string(4) "8bit" ["ErrorInfo"]=> "" ["Absender"]=> String(18) "admin@example.com" ["Absendername"]=> ?Example Admin“[?Sender“]=> string(18)“admin@example.com“ ["Subject"]=> string(27) "Dies ist das Standardthema" ["Body"]=> string(215) ?Dies ist ein Test von SendEmail, der Standardtext enth?lt im Nachrichtentext.Es gibt auch ein Bild..." ["AltBody"]=> string(0) "" ["Ical"]=> string(0) "" ["MIMEBody":protected]=> ["MIMEHeader":protected]=> string(0) "" ["mailHeader":protected]=> string(0) "" ["WordWrap"]=> int(0) ["Mailer"]=> ["Sendmail"]=> string(18) "/usr/sbin/sendmail" ["UseSendmailOptions"]=> bool(true) ["ConfirmReadingTo"]=> "" ["Hostname"]=> string(0) "" ["MessageID"]=> ["MessageDate"]=> String(0) "" ["Host"]=> ["Port"]=> int(587) ["Helo"]=> string(0) "" ["SMTPSecure"]=> "tls" ["SMTPAutoTLS"]=> bool(true) ["SMTPAuth"]=> ["SMTPOptions"]=> array(0) { } ["Benutzername"]=> ?examplemail@gmail.com“ [?password“]=> string(16) ?shotlzacfvdubehm“ ["AuthType"]=> String(0) "" ["oauth":protected]=> int(300) ["SMTPDebug"]=> int(0) ["Debugoutput"]=> ["SMTPKeepAlive"]=> bool(false) ["SingleTo"]=> ["SingleToArray":protected]=> array(0) { } ["do_verp"]=> ["AllowEmpty"]=> bool(false) ["DKIM_selector"]=> ["DKIM_identity"]=> string(0) "" ["DKIM_passphrase"]=> ["DKIM_domain"]=> string(0) "" ["DKIM_private"]=> ["DKIM_private_string"]=> string(0) "" ["action_function"]=> "" ["XMailer"]=> String(0) "" ["smtp":protected]=> ["to":protected]=> 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]=> ["ReplyTo":protected]=> array(1) { ["admin@example.com"]=> [0]=> String(18) "admin@example.com" 1=> ["all_recipients":protected]=> array(2) { ["aa@gmail.com"]=> bool(true) ["ag@example.com"]=> ["RecipientsQueue":protected]=> array(0) { } ["ReplyToQueue":protected]=> array(0) { } ["attachment":protected]=> Array(2) { [0]=> Array(8) { [0]=> "../images/clients/Logo.jpg" 1=> string(13) "Logo.jpg" 2=> String(13) ?Logo.jpg“ [3]=> String(0) ?“ [4]=> [5]=> bool(false) [6]=> string(6) "inline" [7]=>1=> array(8) { [0]=> string(35) "../images/XX.jpg" 1=> "XX.jpg" 2=> string(16) "XX.jpg" [3]=> string(0) "" [4]=> ?image/jpeg“ [5]=> bool(false) [6]=> string(3) "XX" } } ["CustomHeader":protected]=> array(0) { } ["lastMessageID":protected]=> string(0) "" ["message_type":protected]=> string(0) "" ["boundary":protected]=> array(0) { } ["Sprache": protected]=> array(0) { } ["error_count":protected]=> int(0) ["sign_cert_file":protected]=> String(0) ?“ [?sign_key_file“:protected]=> ["sign_extracerts_file": protected] => string(0) "" ["sign_key_pass": protected] = > string(0) "" ["Exception": protected] = > bool(false) ["uniqueid":protected]=> string(0) "" } Nachricht wurde gesendet!
Ich kann auch keine umfassende Dokumentation finden – nicht einmal auf GitHub: 3731. Das, was mir am n?chsten kam, war dieses. Aber ich verstehe immer noch nicht, was genau der dritte Parameter macht. M?chte ich den Namen überschreiben? Warum? Welche Kodierung soll ich verwenden (ich habe Base64 ausprobiert und beide leer gelassen)? Welche M?glichkeiten gibt es neben der ?Inline“-Entsorgung? Warte...
首先,您似乎正在使用舊版本的 PHPMailer,因為您引用了一個多年來一直不屬于庫的文件(自動加載器),因此請更新。
您的代碼看起來像是基于一個非常古老的示例。您不應該自己設置 Mailer
– isSMTP()
會為您完成此操作。
如果磁盤上的文件具有您不想向收件人公開的不同名稱,您可能需要覆蓋該名稱,例如您本地可能有 logo-123456.png
,并希望將其附加為 logo.png
。
編碼幾乎總是需要為 base64
,因為圖像往往是二進制的,因此電子郵件不安全,因此請將其保留為默認值。
MIME 類型應該與文件擴展名匹配,因此您也應該能夠忽略這一點(PHPMailer 將為您設置它),除非您有非常具體的原因來設置不匹配的 MIME 類型 em> 匹配您的內容。
$disposition
設置來自 RFC2183。對于嵌入圖像使用除內聯之外的任何內容都沒有多大意義,因此您也應該將其保留為默認值。該選項存在是因為 RFC 規(guī)定它應該存在。
所有這些的結果是,您實際需要提供的只是文件的路徑和 cid
。
如果您顯示 SMTP 記錄(設置 SMTPDebug = 2
),可能會有所幫助,因為這將準確顯示您的郵件中發(fā)生的情況。