簡體   English   中英

盡管base64在標頭中發送,但附件仍未被識別

[英]Attachments not recognized despite base64 being sent in header

我在php郵件中發送附件時遇到問題。 問題是電子郵件似乎已發送並已收到,但未被識別為附件。 如果我進入郵件的來源,我可以看到它正在發送base64並包含附件名稱,因此我很困惑,為什么它不會被客戶端識別。 有任何想法嗎?

$uid = md5(uniqid(time()));
$headers = "From: ".$from_name." <".$mailto.">\n";
$headers .= "Reply-To: ".$from_mail."\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "--".$uid."\n";
$headers .= "Content-type:text/plain\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= $message."\n";

foreach ($_FILES as $file) {
    $name = $file['name'];
    $handle = fopen($file['tmp_name'], "r");
    $content = fread($handle, $file['size']);
    fclose($handle);
    $content = chunk_split(base64_encode($content));

    $headers .= "--{$uid}\n";
    $headers .= "Content-Type: {$file['type']}; name=\"{$name}\"\n";
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"{$name}\"\n";
    $headers .= $content;
}

$headers .= "--{$uid}--";
$mailto = 'myemail@email.com';
return mail($mailto, $subject, $message, $headers, '-f' . $mailto);

問題

您的代碼有很多問題:

  • 電子郵件標題必須由CRLF終止。 你只有LF
  • B64編碼的文件和消息將添加到標頭中。 它們應該作為消息正文發送。
  • 消息頭和每個部分中的消息正文之間必須有一個額外的CRLF
  • 身體部位必須用以CRLF開頭的分隔符分隔。

電郵語法

根據RFC 2046,這是多部分消息體的結構的摘錄。要特別注意CRLF的內容。 (BNF語法,有點簡化。)

multipart-body := [preamble CRLF]
                  dash-boundary CRLF
                  body-part *encapsulation
                  close-delimiter
                  [CRLF epilogue]

dash-boundary := "--" boundary

body-part := MIME-part-headers [CRLF *OCTET]

encapsulation := delimiter
                 CRLF body-part

delimiter := CRLF dash-boundary

close-delimiter := delimiter "--"

編碼

您的代碼可能如下所示:

$uid = md5(uniqid(time()));
$headers = "From: ".$from_name." <".$mailto.">\r\n";
$headers .= "Reply-To: ".$from_mail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";

$body = "This is a multi-part message in MIME format.\n";
$body .= "--".$uid."\r\n";
$body .= "Content-type:text/plain\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= "\r\n";
$body .= $message;

foreach ($_FILES as $file) {
  $name = $file['name'];
  $handle = fopen($file['tmp_name'], "r");
  $content = fread($handle, $file['size']);
  fclose($handle);
  $content = chunk_split(base64_encode($content));

  $body .= "\r\n--{$uid}\r\n";
  $body .= "Content-Type: {$file['type']}; name=\"{$name}\"\r\n";
  $body .= "Content-Transfer-Encoding: base64\r\n";
  $body .= "Content-Disposition: attachment; filename=\"{$name}\"\r\n";
  $body .= "\r\n";
  $body .= $content;
}

$body .= "\r\n--{$uid}--";
$mailto = 'myemail@email.example';
return mail($mailto, $subject, $body, $headers, '-f' . $mailto);

參考

暫無
暫無

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

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