<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fwolf's Blog &#187; attach - Fwolf's Blog</title>
	<atom:link href="http://www.fwolf.com/blog/post/tag/attach/feed" rel="self" type="application/rss+xml" />
	<link>http://www.fwolf.com/blog</link>
	<description>随心·随意·随缘·努力～</description>
	<lastBuildDate>Wed, 07 Jul 2010 07:07:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>让phpmailer支持中文名称的附件</title>
		<link>http://www.fwolf.com/blog/post/176</link>
		<comments>http://www.fwolf.com/blog/post/176#comments</comments>
		<pubDate>Tue, 23 May 2006 04:00:00 +0000</pubDate>
		<dc:creator>Fwolf</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[attach]]></category>
		<category><![CDATA[chinese]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[messy]]></category>
		<category><![CDATA[phpmailer]]></category>

		<guid isPermaLink="false">http://www.fwolf.com/blog/post/176</guid>
		<description><![CDATA[phpmailer设置使用utf-8编码发送邮件以后，已经能够正常的发送中文邮件了，当然你调用时传进去的中文参数必须也是utf-8编码才行，但是我发现，即使这样，发送中文文件名的附件的时候，附件名称不能正确的显示。 比如我们要发送的附件是“测试.txt”，如果在添加附件的时候强制使用指定文件名的方式： $mail->AddAttachment($attach, $attach); 那么发送过去的附件文件名将会是乱码，如果不指定： $mail->AddAttachment($attach, $attach); 那么发送过去的文件名中的中文干脆没了，成了“.txt”。 究其原因，打开class.phpmailer.php，在大概第1007行左右，函数AddAttachment中，有一句 $filename = basename($path); 原因就在这里，现在我们使用的php 5.1.2，包括他以前的很多版本，basename函数和dirname函数都是不支持中文文件名的，所以一解析就把中文给过滤掉了。而如果强行指定文件名为什么还乱码呢，这是因为phpmailer虽然会自动根据你设定的编码方式给主题、正文进行utf-8编码化，但是却不会给附件的文件名编码。现在，只需要修改上面这一句，就能够同时解决这两方面的问题了。修改结果如下： //$filename = basename($path); if (false === strpos($path, '/')) $filename = $this-&#62;EncodeHeader($path); else $filename = $this-&#62;EncodeHeader(substr($path, strrpos($path, '/') + 1)); 不使用basename函数了，改用自己的方法来得到文件名，并且借用了主题Subject的编码函数EncodeHeader来生成utf-8编码形式的附件名称，搞定。 Update @ 2008-04-14 在phpMailer 2.1.0 Beta 2中，这个问题依然没有得到处理，需要作的修改还是一样，不过代码的位置在1018行附近。 另外在发送中文邮件的时候，中文会出现乱码，看了网上有处理的方式，没有讲原因，也粗暴了点，直接把函数截断了，还要改两个地方。 我看了一下源码，乱码的产生大概是在将邮件标题转成几个小的=?utf-8?B?...?=时，可能是无意中把中文给截断了产生的，所以我的修改更简单而又略微温柔一点，修改第1185行： $maxlen = 75 - 7 - strlen($this-&#62;CharSet); 改成： $maxlen = 75000 - [...]]]></description>
			<content:encoded><![CDATA[<p>phpmailer设置使用utf-8编码发送邮件以后，已经能够正常的发送中文邮件了，当然你调用时传进去的中文参数必须也是utf-8编码才行，但是我发现，即使这样，发送中文文件名的附件的时候，附件名称不能正确的显示。</p>

<p>比如我们要发送的附件是“测试.txt”，如果在添加附件的时候强制使用指定文件名的方式：</p>

<div class="code">
$mail->AddAttachment($attach, $attach);
</div>

<p>那么发送过去的附件文件名将会是乱码，如果不指定：</p>

<div class="code">
$mail->AddAttachment($attach, $attach);
</div>

<p>那么发送过去的文件名中的中文干脆没了，成了“.txt”。</p>

<p>究其原因，打开class.phpmailer.php，在大概第1007行左右，函数AddAttachment中，有一句</p>

<div class="code">
$filename = basename($path);
</div>

<p>原因就在这里，现在我们使用的php 5.1.2，包括他以前的很多版本，basename函数和dirname函数都是不支持中文文件名的，所以一解析就把中文给过滤掉了。而如果强行指定文件名为什么还乱码呢，这是因为phpmailer虽然会自动根据你设定的编码方式给主题、正文进行utf-8编码化，但是却不会给附件的文件名编码。现在，只需要修改上面这一句，就能够同时解决这两方面的问题了。修改结果如下：</p>

<pre><code>//$filename = basename($path);
if (false === strpos($path, '/'))
    $filename = $this-&gt;EncodeHeader($path);
else
    $filename = $this-&gt;EncodeHeader(substr($path, strrpos($path, '/') + 1));
</code></pre>

<p>不使用basename函数了，改用自己的方法来得到文件名，并且借用了主题Subject的编码函数EncodeHeader来生成utf-8编码形式的附件名称，搞定。</p>

<h4>Update @ 2008-04-14</h4>

<p>在phpMailer 2.1.0 Beta 2中，这个问题依然没有得到处理，需要作的修改还是一样，不过代码的位置在1018行附近。</p>

<p>另外在发送中文邮件的时候，中文会出现乱码，看了网上有<a href="http://hi.baidu.com/heroman/blog/item/fbe330a8f0aac3b0cb130c9e.html/cmtid/764ba3865a2ff83a67096ea4">处理的方式</a>，没有讲原因，也粗暴了点，直接把函数截断了，还要改两个地方。</p>

<p>我看了一下源码，乱码的产生大概是在将邮件标题转成几个小的<code>=?utf-8?B?...?=</code>时，可能是无意中把中文给截断了产生的，所以我的修改更简单而又略微温柔一点，修改第1185行：</p>

<pre><code>$maxlen = 75 - 7 - strlen($this-&gt;CharSet);
改成：
$maxlen = 75000 - 7 - strlen($this-&gt;CharSet);
</code></pre>

<p>就行了，把字符串的值设大点，让它不分段就行了。</p>

	Tags: <a href="http://www.fwolf.com/blog/post/tag/attach" title="attach" rel="tag">attach</a>, <a href="http://www.fwolf.com/blog/post/tag/chinese" title="chinese" rel="tag">chinese</a>, <a href="http://www.fwolf.com/blog/post/tag/mail" title="mail" rel="tag">mail</a>, <a href="http://www.fwolf.com/blog/post/tag/messy" title="messy" rel="tag">messy</a>, <a href="http://www.fwolf.com/blog/post/tag/php" title="PHP" rel="tag">PHP</a>, <a href="http://www.fwolf.com/blog/post/tag/phpmailer" title="phpmailer" rel="tag">phpmailer</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.fwolf.com/blog/post/155" title="终于能够通过phpmailer使用gmail账号发送邮件了 (2006-04-14)">终于能够通过phpmailer使用gmail账号发送邮件了</a> (50)</li>
	<li><a href="http://www.fwolf.com/blog/post/399" title="Gregarius编辑feed时中文乱码的解决 (2008-05-02)">Gregarius编辑feed时中文乱码的解决</a> (1)</li>
	<li><a href="http://www.fwolf.com/blog/post/133" title="截取固定长度的中文字符串 (2006-02-06)">截取固定长度的中文字符串</a> (2)</li>
	<li><a href="http://www.fwolf.com/blog/post/434" title="在GLPI中输出中文PDF文件 (2009-04-02)">在GLPI中输出中文PDF文件</a> (2)</li>
	<li><a href="http://www.fwolf.com/blog/post/300" title="针对$_SERVER['PHP_SELF']的跨站脚本攻击（XSS） (2007-03-18)">针对$_SERVER['PHP_SELF']的跨站脚本攻击（XSS）</a> (3)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.fwolf.com/blog/post/176/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
