笔记遇到一个问题:claws mail不识别phpmailer发送的附件,经过烽数次在网上找资料,终于找到原因并解决,现在把原因及解决办法整理如下.
环境:claws mail 3.9.1, php 5.4.16,phpmailer 5.2.6 c5e9f7873f
现象:phpmailer 发送带附件的邮件,直接使用 addattachment() 方法
$mailer->addattachment($attach_file);
没有其他设置,claws mail 收到信以后,查看邮件内容为空白, 附件栏显示:
message/rfc822
multipart/mixed
以下就是空白了, 而能够正常识别附件的邮件,附件栏内容一般为:
message/rfc822 multipart/mixed text/plain text/html (这个是附件的 mime 类型)
gmail 和 mutt 中识别这样的邮件是正常的.
分析:通过对比正常和不正常的邮件原始码,发现不正常邮件在声明内容是分节之后,多了一句传输编码声明,比如:
content-type: multipart/mixed; boundary=b1_95a848b14cb4385965320b915d5829dd content-transfer-encoding: base64
最后的 content-transfer-encoding 就是比正常邮件多的一行,由于邮件原始码的这个部分,只是用来声明后续邮件是多个部分组成,并定义了每个部分的辨识边界 boundary,并没有实际的内容,所以应当是不需要声明编码类型的,在 phpmailer 中相关代码为:
public function getmailmime() { $result = ''; switch($this->message_type) { case 'inline': $result .= $this->headerline('content-type', 'multipart/related;'); $result .= $this->textline(tboundary= . $this->boundary[1].''); break; case 'attach': case 'inline_attach': case 'alt_attach': case 'alt_inline_attach': $result .= $this->headerline('content-type', 'multipart/mixed;'); $result .= $this->textline(tboundary= . $this->boundary[1].''); break; case 'alt': case 'alt_inline': $result .= $this->headerline('content-type', 'multipart/alternative;'); $result .= $this->textline(tboundary= . $this->boundary[1].''); break; default: // catches case 'plain': and case '': $result .= $this->textline('content-type: '.$this->contenttype.'; charset='.$this->charset); break; } //rfc1341 part 5 says 7bit is assumed if not specified if ($this->encoding != '7bit') { $result .= $this->headerline('content-transfer-encoding', $this->encoding); }
特意加上了这个申明,因为按照 rfc1341,7bit 编码类型是默认的.
解决:或许问题是出在 claws mail 上,但我暂时只能修改 phpmailer 来适应这个问题了,上面的问题弄清楚之后,在 multipart 后面不添加传输编码声明即可:
//rfc1341 part 5 says 7bit is assumed if not specified // not after multipart/mixed, claws-mail will not recoginize attachment if (($this->encoding != '7bit') && (!in_array($this->message_type, array( 'attach', 'inline_attach', 'alt_attach', 'alt_inline_attach', )))) { $result .= $this->headerline('content-transfer-encoding', $this->encoding); }
终于解决了这个问题,现在我们可以放心的用phpmailer发送附件了.
永久链接:
转载随意!带上文章地址吧。
