Apple Mail未正确显示PHP中的附件

我正在尝试发送带有Opencart附件的HTML电子邮件.内置函数$mail-> addAttachment.一切都很好,除了在附件的位置是Apple Mail中的白色框.在iOs Mail app中,附件根本没有显示.在GMail上没关系:

enter image description here

附件在Apple Mail中也可用,因为如果我双击白色区域,则附件将打开.

这是在GMail中打开的消息来源(我删除了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));
            }
        }

编辑:

我刚刚意识到Content-ID和X-Attachment-Id是问题所在:

basename(urlencode($attachment)

应该:

urlencode(basename($attachment))

它现在在Apple Mail中正常工作,但iOS(iPhone / iPad)上仍然缺少附件.任何的想法?

解决方法:

标题必须是这样的:

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

在大多数情况下发现缺少mime版本标记.你有边界错误让我先给出规则body of a multipart entity有语法(只有重要的部分):

06001

前面的 – 对于消息中使用的每个边界都是强制性的,并且尾随 – 对于结束边界(close-delimiter)是强制性的.因此,具有三个以边界为边界的主体部分的多部分主体可以如下所示:

--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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...