【问题标题】:Apple Mail not showing attachments correctly from PHPApple Mail 未正确显示 PHP 的附件
【发布时间】:2016-12-01 17:41:01
【问题描述】:

我正在尝试从 Opencart 发送带有附件的 HTML 电子邮件。有一个内置函数$mail->addAttachment。一切都很好,除了附件的位置是 Apple Mail 中的一个白框。在 iOs Mail 应用程序中,附件根本不显示。在 GMail 上没问题:

附件在 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) 上仍然缺少附件。有什么想法吗?

【问题讨论】:

  • 如果您的“源”代码中有相关内容,您应该发布它。您确实标记为 php,但没有“代码”来支持该问题。如果您有任何 HTML/JS 等要分享,请发布。它可以帮助加快进度。
  • 确保确实定义了$message,如果您没有打开$message = "Something";,那么您可能需要去掉$message .= '--' . $boundary . $this-&gt;newline; 中的第一个点。通过错误报告检查错误,它可能会有所帮助。
  • 你解决过这个问题吗? iPhone 收件人抱怨他们的收据未附在电子邮件中,我也遇到了完全相同的问题!

标签: php email opencart


【解决方案1】:

标题必须是这样的:

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

前面的-- 对于消息中使用的每个边界都是强制性的,而结尾的-- 对于闭合边界是强制性的(close-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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多