簡體   English   中英

使用PHP發送zip文件時出現空消息

[英]Empty message when sending zip file with PHP

**更新:更新了代碼並添加了Gmail中顯示的電子郵件內容。 **

我有一個問題想解決幾天。 但是,由於我一直無法取得任何進展,因此我需要其他人的幫助。 我正在嘗試將zip文件從服務器發送到電子郵件。 但是,電子郵件被發送為空。 這是代碼:

`$headers = "From: $from";
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\"";   
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"

Attached, please find your backup file. 

--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment

--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";`

感謝所有閱讀並希望對您有所幫助。

在Gmail中看到的結果電子郵件為

附件中,請找到您的備份文件。

--PHP-mixed-e44b531b0e0538185289abc521eda78d
Content-Type: application/zip; name=backup-29-11-2014-1417275285-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment

UEsDBBQAAAAIAFZ8fUUs...sUEsFBgAAAAACAAIAeAAAAD4B

AAAAAA ==

--PHP-mixed-e44b531b0e0538185289abc521eda78d--

我通過以下更改使其正常工作:

  • 刪除了From:標頭中的空格字符,否則為無效標頭
  • boundary:更改為boundary: boundary=
  • 刪除了$message意圖,因為這似乎破壞了電子郵件。 邊界字符串--PHP-mixed-random將電子郵件的不同部分分開,例如純文本,html文本,附件。 當邊界分隔符前面有空格時,它不再被解釋為分隔符,而是普通文本。

這是代碼:

<?php
$from = "xxx@myserver";
$email = "xxx@gmail.com";
$subject = "backup file";
$date = "29-11-2014";
$time = "1417275285"; // test file "backup-29-11-2014-1417275285-9.zip" in the same folder

$headers = "From: $from"; // remove space
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\""; // : to =
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"

Attached, please find your backup file. 

--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment

--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM