Heim  >  Artikel  >  Backend-Entwicklung  >  PHPMailer发送邮件中文附件名乱码的解决办法

PHPMailer发送邮件中文附件名乱码的解决办法

WBOY
WBOYOriginal
2016-07-25 08:59:421413Durchsuche
  1. $mail->AddAttachment($attach, $attach);
复制代码

发送过去的附件文件名将会是乱码,如果不指定:

  1. $mail->AddAttachment($attach, $attach);
复制代码

发送过去的文件名中的中文直接没有了,变成了“.txt”。

解决办法一

打开class.phpmailer.php,在大概第1007行左右,函数AddAttachment中,有一句:

  1. //$filename = basename($path);
  2. if (false === strpos($path, ‘/’))
  3. $filename = $this->EncodeHeader($path);
  4. else
  5. $filename = $this->EncodeHeader(substr($path, strrpos($path, ‘/’) + 1));
复制代码

解决办法二

如果想设置文件名为中文,则在调用AddAttachment时提供中文的name参数(第二个参数)。 比如

  1. $mail->AddAttachment(‘temp/2011/test.rar’, ‘测试.rar’);
复制代码

其它问题:发送中文邮件的时候,中文会出现乱码

乱码的产生大概是在将邮件标题转成几个小的=?utf-8?B?...?=时,可能是无意中把中文给截断了产生的,修改第1185行:

  1. $maxlen = 75 - 7 - strlen($this->CharSet);
复制代码

改成:

  1. $maxlen = 75000 - 7 - strlen($this->CharSet);
复制代码

附,PHPMailer邮件发送类V5.1下载地址。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn