Home  >  Article  >  php教程  >  Claws Mail不识别PHPMailer发送的附件的原因及解决办法

Claws Mail不识别PHPMailer发送的附件的原因及解决办法

WBOY
WBOYOriginal
2016-05-23 13:07:321347browse

笔记遇到一个问题: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发送附件了.

永久链接:

转载随意!带上文章地址吧。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn