2016-07-27 64 views
4

我正嘗試使用Opencart的附件發送HTML電子郵件。有一個內置函數$mail->addAttachment。一切都很好,除了附件的地方是Apple Mail中的白色方塊。在iOs郵件應用程序中,附件完全不顯示。在Gmail它的確定:Apple Mail未正確顯示來自PHP的附件

enter image description here

附件是在Apple Mail提供過,因爲如果我加倍的白色區域中單擊,附件被打開。

這裏是消息,在打開的GMail的源(I除去X頭):

X-Mailer: PHP/5.4.39-0+deb7u2 
Content-Type: multipart/related; boundary="----=_NextPart_fefb9509ef8523a96a17066ecf8472c8" 

------=_NextPart_fefb9509ef8523a96a17066ecf8472c8 
Content-Type: multipart/alternative; boundary="----=_NextPart_fefb9509ef8523a96a17066ecf8472c8_alt" 

------=_NextPart_fefb9509ef8523a96a17066ecf8472c8_alt 
Content-Type: text/plain; charset="utf-8" 
Content-Transfer-Encoding: 8bit 

Some text (text/plain) 


------=_NextPart_fefb9509ef8523a96a17066ecf8472c8_alt 
Content-Type: text/html; charset="utf-8" 
Content-Transfer-Encoding: 8bit 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd"> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
       <title>Test title</title> 
    </head> 
    <body> 
    <p>Some html text(text/html)</p> 
    </body> 
</html> 

------=_NextPart_fefb9509ef8523a96a17066ecf8472c8_alt-- 
------=_NextPart_fefb9509ef8523a96a17066ecf8472c8 
Content-Type: application/pdf; name="form.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="form.pdf" 
Content-ID <%2Fhome%2Fhtml%2Fdownload%2form.pdf> 
X-Attachment-Id: %2Fhome%2Fhtml%2Fdownload%2form.pdf 


JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G 
bGF0ZURlY29kZSA+PgpzdHJlYW0KeAGdW0tzHLcRvs+vQGInGbqk0bwfuTl2qqKkbMkRk1Q58mFJ 
jkJylzvS7pIu8Q/o5oMuLlfpmB+Un5SvHwDmtdylSyXNAgM0uhuN7q8bo3fmO/POxPhTNIWp89Rs 
WvMvszbPvtom5nxrEv6zPccIenvjxq34V2xWPHsVXJo3TCtLoiYr49ykdZQVpsjqqKqqxlR1GWWW 
+jtQpUUTk5WmKjNzfmP+dGr+fAoStHKAlel9bLCyH1ympspqHRxHcRwn5vTcJDkP1cfpjXl2ekp8 
... 
------=_NextPart_fefb9509ef8523a96a17066ecf8472c8-- 

有關部分從/system/library/mail.php:

foreach ($this->attachments as $attachment) { 
      if (file_exists($attachment)) { 
       $handle = fopen($attachment, 'r'); 

       $content = fread($handle, filesize($attachment)); 

       fclose($handle); 

       $message .= '--' . $boundary . $this->newline; 
       $message .= 'Content-Type: application/pdf; name="' . basename($attachment) . '"' . $this->newline; 
       $message .= 'Content-Transfer-Encoding: base64' . $this->newline; 
       $message .= 'Content-Disposition: attachment; filename="' . basename($attachment) . '"' . $this->newline; 
       $message .= 'Content-ID: <' . basename(urlencode($attachment)) . '>' . $this->newline; 
       $message .= 'X-Attachment-Id: ' . basename(urlencode($attachment)) . $this->newline . $this->newline; 
       $message .= chunk_split(base64_encode($content)); 
      } 
     } 

EDIT :

我剛剛意識到Content-ID和X-Attachment-Id是問題:

basename(urlencode($attachment) 

應該是:

urlencode(basename($attachment)) 

它在Apple Mail正常工作了,但附件仍然在iOS(iPhone/iPad的)失蹤。任何想法?

+0

如果有什麼東西在你的「源」的代碼,可能是相關的,你應該發表它。你做了標籤爲PHP,但沒有「代碼」來支持這個問題。如果你有任何HTML/JS等共享,然後張貼它。它可以幫助加快速度。 –

+1

確保'$ message'確實被定義了,如果你沒有打開'$ message =「Something」;'那麼你可能需要去掉'$ message。=' - '中的第一個點。 $邊界。 $這個 - >換行;'。通過錯誤報告檢查錯誤,這可能會有所幫助。 –

回答

4

標題必須是這樣的:

MIME-Version: 1.0 
Content-Type: multipart/alternative; boundary=boundary42 

在大多數情況下找到丟失的MIME版本標籤。你必須在邊界錯誤,讓我先給規則body of a multipart entity有語法(只重要的部分):

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

的前述--是強制性的消息中使用的每個邊界和後--是強制性爲結束邊界(結束分隔符)。因此,一個多體三體部分與boundary邊界可以是這樣的:

--boundary42 
Content-Type: text/plain; charset=us-ascii 

...plain text version of message goes here.... 

--boundary42 
Content-Type: text/richtext 

.... richtext version of same message goes here ... 

--boundary42 
Content-Type: text/x-whatever 

.... fanciest formatted version of same message goes here... 

--boundary42-- 

閱讀文檔RFC