<?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; mail - Fwolf's Blog</title>
	<atom:link href="http://www.fwolf.com/blog/post/tag/mail/feed" rel="self" type="application/rss+xml" />
	<link>http://www.fwolf.com/blog</link>
	<description>随心·随意·随缘·努力～</description>
	<lastBuildDate>Tue, 29 Dec 2009 14:58:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Wine下运行的广发委托系统退出时可能会误删mail目录</title>
		<link>http://www.fwolf.com/blog/post/418</link>
		<comments>http://www.fwolf.com/blog/post/418#comments</comments>
		<pubDate>Thu, 24 Jul 2008 17:12:23 +0000</pubDate>
		<dc:creator>Fwolf</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[del]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[stock]]></category>
		<category><![CDATA[wine]]></category>

		<guid isPermaLink="false">http://www.fwolf.com/blog/?p=418</guid>
		<description><![CDATA[这个问题不是第一次出现了，网上也查不到相关资料，所以感觉有必要说一说，看有没有和我同样遭遇的朋友。

用wine运行广发委托软件，是比较老的版本了，用起来没什么问题的，新版wine下中文的表现还改进了：



偶尔的，好像是打开以后发呆时间比较长了更容易出现，点左上角“退出”按钮的时候，原本应该一下子就关闭的，突然变得迟钝、没有响应了。后来一检查，发现$HOME/mail下的文件都没了，把文件删完之后委托程序才正常关闭。

猜测是委托软件为了防病毒、盗号之类的，在进行安全检查的时候，会检查目录下有没有mail文件（因为一般盗号会采用生成邮件，然后发送到自己邮箱的方式），如果有就删除之。windows下一般人很少会用到mail目录的，用wine运行的委托软件不知怎地阴差阳错的就找到了系统默认的mail目录，文件还删得挺干净。

原先想过把mail放到别处，然后ln过来，可并不解决问题；并且mail这个目录也不宜放到别处，因为那样系统生成的邮件就放不过来了，比如crontab里程序运行的输出。

所以现在只能采取两个临时措施，以观后效。一是在用wine启动程序的时候，先cd到委托软件所在目录，即当前目录下就没有mail目录了；二是在winecfg里把到$HOME下的映射都删掉：



话再说回来，在$HOME下操作其实也挺危险的，因为对于个人用计算机来说，系统坏了倒是小事，资料丢了才心疼呢。

	Tags: del, mail, stock, wine

	Related posts
	
	让phpmailer支持中文名称的附件 (5)
	终于能够通过phpmailer使用gmail账号发送邮件了 (50)
	我的email客户端软件选择历程 (7)
	侠盗猎车·圣安得列斯 on Cedega (4)
	[Mutt]用msmtp替代esmtp作发信代理 (2)


]]></description>
			<content:encoded><![CDATA[<p>这个问题不是第一次出现了，网上也查不到相关资料，所以感觉有必要说一说，看有没有和我同样遭遇的朋友。</p>

<p>用wine运行广发委托软件，是比较老的版本了，用起来没什么问题的，新版wine下中文的表现还改进了：</p>

<p><a href="http://www.flickr.com/photos/fwolf/2696169034/" title="stock_trade_client by fwolf, on Flickr"><img src="http://farm4.static.flickr.com/3176/2696169034_2a54b5970b_o.png" width="288" height="284" alt="stock_trade_client" /></a></p>

<p>偶尔的，好像是打开以后发呆时间比较长了更容易出现，点左上角“退出”按钮的时候，原本应该一下子就关闭的，突然变得迟钝、没有响应了。后来一检查，发现<code>$HOME/mail</code>下的文件都没了，把文件删完之后委托程序才正常关闭。</p>

<p>猜测是委托软件为了防病毒、盗号之类的，在进行安全检查的时候，会检查目录下有没有mail文件（因为一般盗号会采用生成邮件，然后发送到自己邮箱的方式），如果有就删除之。windows下一般人很少会用到mail目录的，用wine运行的委托软件不知怎地阴差阳错的就找到了系统默认的mail目录，文件还删得挺干净。</p>

<p>原先想过把mail放到别处，然后ln过来，可并不解决问题；并且mail这个目录也不宜放到别处，因为那样系统生成的邮件就放不过来了，比如crontab里程序运行的输出。</p>

<p>所以现在只能采取两个临时措施，以观后效。一是在用wine启动程序的时候，先cd到委托软件所在目录，即当前目录下就没有mail目录了；二是在<code>winecfg</code>里把到<code>$HOME</code>下的映射都删掉：</p>

<p><a href="http://www.flickr.com/photos/fwolf/2695351535/" title="wine_remove_my_documents by fwolf, on Flickr"><img src="http://farm4.static.flickr.com/3273/2695351535_84f9b7f385.jpg" width="362" height="500" alt="wine_remove_my_documents" /></a></p>

<p>话再说回来，在<code>$HOME</code>下操作其实也挺危险的，因为对于个人用计算机来说，系统坏了倒是小事，资料丢了才心疼呢。</p>

	Tags: <a href="http://www.fwolf.com/blog/post/tag/del" title="del" rel="tag">del</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/stock" title="stock" rel="tag">stock</a>, <a href="http://www.fwolf.com/blog/post/tag/wine" title="wine" rel="tag">wine</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.fwolf.com/blog/post/176" title="让phpmailer支持中文名称的附件 (2006-05-23)">让phpmailer支持中文名称的附件</a> (5)</li>
	<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/17" title="我的email客户端软件选择历程 (2005-05-23)">我的email客户端软件选择历程</a> (7)</li>
	<li><a href="http://www.fwolf.com/blog/post/378" title="侠盗猎车·圣安得列斯 on Cedega (2008-01-10)">侠盗猎车·圣安得列斯 on Cedega</a> (4)</li>
	<li><a href="http://www.fwolf.com/blog/post/379" title="[Mutt]用msmtp替代esmtp作发信代理 (2008-01-12)">[Mutt]用msmtp替代esmtp作发信代理</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.fwolf.com/blog/post/418/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Mutt]用msmtp替代esmtp作发信代理</title>
		<link>http://www.fwolf.com/blog/post/379</link>
		<comments>http://www.fwolf.com/blog/post/379#comments</comments>
		<pubDate>Sat, 12 Jan 2008 15:49:37 +0000</pubDate>
		<dc:creator>Fwolf</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[msmtp]]></category>
		<category><![CDATA[mutt]]></category>
		<category><![CDATA[queue]]></category>
		<category><![CDATA[tls]]></category>

		<guid isPermaLink="false">http://www.fwolf.com/blog/post/379</guid>
		<description><![CDATA[刚开始使用mutt的时候，由于也没有什么经验，在MDA选择方面抓了一个esmtp就用上了。说实话，esmtp也一直工作的很好，就是发信的时候需要傻傻等着(参见muttrc的sendmail_wait参数)，如果不等待也可以，发送失败你就不知道了。虽然失败的情况比较少，一般都是邮件比较大或者网络不太好的情况才会发生，但我们外发的邮件往往都是比较重要的，真想有一种能够自动重试的发信代理，于是就查到了msmtp。

msmtp和esmtp两个软件作者都不多，但msmtp确实更新频繁一些，而esmtp作者说现在已经不需要怎么添加功能了，只是维护bug。在多账户配置方面，好像msmtp的配置方法要“聪明”一些，账号规则有一些&#8221;继承&#8221;的小把戏。在发信队列方面，esmtp在Features中明显声明不支持，而msmtp有一个msmtpqueue，国内用的人非常少，只看到cu blog上提到了一点，smtpqueue能够把要外发的邮件集中在一起，单独发送，但没有说失败处理的事情；再到msmtp在sf.net上的cvs中查msmtpqueue的README文件，终于搞清楚了，和我的需要比较吻合。

msmtpqueue会&#8221;冒充&#8221;成一个MDA，当mutt发信时，它接收外发邮件并返回成功信息，而实际上只是把信件保存在QUEUEDIR里了，当调用msmtp-runqueue.sh的时候，再进行实际的发送处理。发送成功一封信，就会从QUEUEDIR中删除，发送失败就继续保持不动，下次再调用msmtp-runqueue.sh的时候自然又会处理(相当于重发)了。比起mutt中的邮件失败重发还会多生成一个邮件副本(我设定了发信的同时record在当前目录)应该是好多了。

Mails sent successfully will be deleted from the queue directory.
Mails whose delivery failed will be left untouched; you may want to edit
them, delete them by hand or simply run msmtp-runqueue.sh at a later time.


工作开始，首先通过apt安装msmtp，很小的，加上TLS/SSL所需的libgsasl7，一共才182k。然后配置账号文件~/.msmtprc，格式和~/.esmtprc比较类似：

# Default value for all following accounts
defaults
#      tls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
#   [...]]]></description>
			<content:encoded><![CDATA[<p>刚开始使用mutt的时候，由于也没有什么经验，在MDA选择方面抓了一个esmtp就用上了。说实话，esmtp也一直工作的很好，就是发信的时候需要傻傻等着(参见muttrc的sendmail_wait参数)，如果不等待也可以，发送失败你就不知道了。虽然失败的情况比较少，一般都是邮件比较大或者网络不太好的情况才会发生，但我们外发的邮件往往都是比较重要的，真想有一种能够自动重试的发信代理，于是就查到了msmtp。</p>

<p><a href="http://msmtp.sourceforge.net/">msmtp</a>和<a href="http://esmtp.sourceforge.net/">esmtp</a>两个软件作者都不多，但msmtp确实更新频繁一些，而esmtp作者说<a href="http://esmtp.sourceforge.net/roadmap.html">现在已经不需要怎么添加功能了，只是维护bug</a>。在多账户配置方面，好像msmtp的配置方法要“聪明”一些，账号规则有一些&#8221;继承&#8221;的小把戏。在发信队列方面，esmtp在Features中明显声明不支持，而msmtp有一个msmtpqueue，国内用的人非常少，只看到<a href="http://blog.chinaunix.net/u1/37261/showart_288583.html">cu blog</a>上提到了一点，smtpqueue能够把要外发的邮件集中在一起，单独发送，但没有说失败处理的事情；再到msmtp在sf.net上的cvs中查<a href="http://msmtp.cvs.sourceforge.net/msmtp/msmtpqueue/README?revision=1.2&amp;view=markup">msmtpqueue的README文件</a>，终于搞清楚了，和我的需要比较吻合。</p>

<p>msmtpqueue会&#8221;冒充&#8221;成一个MDA，当mutt发信时，它接收外发邮件并返回成功信息，而实际上只是把信件保存在QUEUEDIR里了，当调用<code>msmtp-runqueue.sh</code>的时候，再进行实际的发送处理。发送成功一封信，就会从QUEUEDIR中删除，发送失败就继续保持不动，下次再调用<code>msmtp-runqueue.sh</code>的时候自然又会处理(相当于重发)了。比起mutt中的邮件失败重发还会多生成一个邮件副本(我设定了发信的同时record在当前目录)应该是好多了。</p>

<pre><code>Mails sent successfully will be deleted from the queue directory.
Mails whose delivery failed will be left untouched; you may want to edit
them, delete them by hand or simply run msmtp-runqueue.sh at a later time.
</code></pre>

<p>工作开始，首先通过apt安装msmtp，很小的，加上TLS/SSL所需的libgsasl7，一共才182k。然后配置账号文件<code>~/.msmtprc</code>，格式和<code>~/.esmtprc</code>比较类似：</p>

<pre><code># Default value for all following accounts
defaults
#      tls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
#   keepbcc on
    logfile ~/log/msmtp.log

# Gmail
account gmail
    host smtp.gmail.com
    port 587
    auth on
    tls on

account mailbox@gmail.com : gmail
    from mailbox@gmail.com
    user "mailbox@gmail.com"
    password "secret"

# Another mail service
account another_mailbox@domain.com
    host smtp.domain.com
    auth on
    from another_mailbox@domain.com
    user "another_mailbox"
    password "another_secret"

# Default account
account default : mailbox@gmail.com
</code></pre>

<p>Copy自<a href="http://msmtp.sourceforge.net/doc/msmtp.html#A-user-configuration-file">官方文档</a>，语法很简单，最开始的<code>defaults</code>是默认设置，然后的account就是各个账号的设置了，账号名称最好和邮件中的<code>From:</code>对应，这样msmtp就能知道调用哪个账号了。最有意思的是，account可以用冒号<code>:</code>来&#8221;继承&#8221;其它已定义账号的设置，使用这个机制，就为所有gmail邮箱指定了host、port、auth、tls四个属性，然后在具体的账号设置中，只要再设定from、user、password就可以了，很方便，&#8221;继承&#8221;多个账号的话用逗号隔开。最后默认发信账号(匹配寻找account失败时使用)<code>default</code>可以直接&#8221;继承&#8221;某个账号的设置过来。</p>

<p>然后设置<code>~/.muttrc</code>，用msmtp来发信：</p>

<pre><code>set sendmail="/path/to/msmtp"
set use_from=yes
set from="Fwolf &lt;mailbox@domain.com&gt;"
set envelope_from=yes
set sendmail_wait=0
</code></pre>

<p>测试一下，发信正常，现在可以让msmtpqueue出场了。由于msmtpqueue只是在&#8221;转达&#8221;msmtp发信的命令，所以需要先把msmtp调试好以后再配置。</p>

<p>msmtpqueue已经随msmtp安装上了，放在<code>/usr/share/doc/msmtp/examples</code>下，按照这里README文件的说明，把<code>msmtp-enqueue.sh</code>和<code>msmtp-runqueue.sh</code>两个文件加上执行属性，然后ln到<code>$HOME/.mutt</code>下，再在这里创建一个新目录<code>msmtpqueue</code>，ln成<code>~/.msmtpqueue</code>(两个脚本默认的QUEUEDIR)，最后修改muttrc中的sendmail：</p>

<pre><code>set sendmail = "~/.mutt/msmtp-enqueue.sh"
</code></pre>

<p>好了，现在再来发信，是不是瞬间就完成了？再看<code>~/.mutt/msmtpqueue</code>目录下，也多了两个文件，分别以<code>.mail</code>和<code>.msmtp</code>结尾，他们分别包含了刚才所发信件的信件内容和发送命令。现在运行<code>msmtp-runqueue.sh</code>：</p>

<pre><code>~/.mutt$ ./msmtp-runqueue.sh 
*** Sending 2008-01-12-23.03.52.mail...
2008-01-12-23.03.52.mail sent successfully
</code></pre>

<p>邮件就这样顺利的发出去了。</p>

<p>工作到这里基本就都完成了，发信时按y嗖一下就完成的感觉真不错，为了更方便的发送邮件，还可以在mutt中定义启动邮件发送程序的快捷键：</p>

<pre><code>macro generic S "!~/.mutt/msmtp-runqueue.sh\n"
</code></pre>

<p>ubuntu的包里没有，但<a href="http://msmtp.cvs.sourceforge.net/msmtp/msmtpqueue/">svn上</a>还有一个<code>msmtp-listqueue.sh</code>脚本，列出当前QUEUEDIR中信件的From/To/主题，凑合能用，但用RFC2047编码方式的中文标题自然是不能正常显示的了。</p>

<h4>Update @ 2009-03-27</h4>

<p>知道gmail的两个端口，465和587有区别么？还真不完全一样，我用msmtp试出来的结果是这样的：</p>

<ul>
<li>如果使用465端口，则置tls_starttls off</li>
<li>如果使用587端口，则置tls_starttls on</li>
</ul>

<p>否则不是连不上就是无回馈数据。是看了<a href="http://www.linuxsir.org/bbs/thread301660.html">标题: 我翻译的 msmtp 的手册页</a>后受启发发现的。</p>

	Tags: <a href="http://www.fwolf.com/blog/post/tag/gmail" title="gmail" rel="tag">gmail</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/msmtp" title="msmtp" rel="tag">msmtp</a>, <a href="http://www.fwolf.com/blog/post/tag/mutt" title="mutt" rel="tag">mutt</a>, <a href="http://www.fwolf.com/blog/post/tag/queue" title="queue" rel="tag">queue</a>, <a href="http://www.fwolf.com/blog/post/tag/tls" title="tls" rel="tag">tls</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.fwolf.com/blog/post/431" title="[Git]提交后自动发email (2009-03-27)">[Git]提交后自动发email</a> (2)</li>
	<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/176" title="让phpmailer支持中文名称的附件 (2006-05-23)">让phpmailer支持中文名称的附件</a> (5)</li>
	<li><a href="http://www.fwolf.com/blog/post/17" title="我的email客户端软件选择历程 (2005-05-23)">我的email客户端软件选择历程</a> (7)</li>
	<li><a href="http://www.fwolf.com/blog/post/375" title="安装配置fluxbox桌面环境 (2007-12-15)">安装配置fluxbox桌面环境</a> (7)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.fwolf.com/blog/post/379/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Message rejected for Sector 5 policy reasons</title>
		<link>http://www.fwolf.com/blog/post/284</link>
		<comments>http://www.fwolf.com/blog/post/284#comments</comments>
		<pubDate>Sat, 03 Feb 2007 01:21:43 +0000</pubDate>
		<dc:creator>Fwolf</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[508]]></category>
		<category><![CDATA[accessbility]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[law]]></category>
		<category><![CDATA[mail]]></category>

		<guid isPermaLink="false">http://www.fwolf.com/blog/post/284</guid>
		<description><![CDATA[用邮件订阅了一些google groups讨论组，发现有些文章值得保存的话，我会用mail forward转发到自己的另外一个讨论组，这样便于以后查找，可有些邮件在转发的时候会失败，gmail会返回一封“Delivery Status Notification (Failure)”发送失败的邮件，内容如下：


This is an automatically generated Delivery Status Notification

Delivery to the following recipient failed permanently:

    somewhere@googlegroups.com

Technical details of permanent failure:
PERM_FAILURE: Message rejected for Sector 5 policy reasons

  &#8212;&#8211; Original message &#8212;&#8211;
&#8230;&#8230;


退信的原因是“Sector 5“，而这个神秘的“Sector 5“ google并没有给出详细的内容，哪怕是出处也没有。网上也有很多人遇到了类似的情况，甚至在一些google groups中也有讨论，但都没有结果，google官方也没有出面指明。不过，今天在看某个电影的时候，发现了这个：



这是大家常见的“FBI WARNING”信息，就是警告信息，一般正规影碟前面都有的，告诉观众不得非法传播、拷贝，否则最高可以判5年徒刑或者罚款25万美元（够恨的），而在这些的法律依据，就是“Title 17 U.S.Code, Section 501, 506 and 508”。

所以我觉得gmail退信的原因，和这个Section 501等等有关系，没准就是同一部法律，或者类似的条款，都是关于版权保护的内容。gmail在我转发邮件的时候，发现转发的内容不是我写的，并且和其他groups中的文章惊人的相似，所以就以“Sector 5”为由，拒绝我传播“盗版”了。

Update @ 2008-03-23

找到一篇和508沾边但仍未完整解答我的疑问的文章：section [...]]]></description>
			<content:encoded><![CDATA[<p>用邮件订阅了一些google groups讨论组，发现有些文章值得保存的话，我会用mail forward转发到自己的另外一个讨论组，这样便于以后查找，可有些邮件在转发的时候会失败，gmail会返回一封“Delivery Status Notification (Failure)”发送失败的邮件，内容如下：</p>

<div class="code">
This is an automatically generated Delivery Status Notification

Delivery to the following recipient failed permanently:

    somewhere@googlegroups.com

Technical details of permanent failure:
PERM_FAILURE: Message rejected for Sector 5 policy reasons

  &#8212;&#8211; Original message &#8212;&#8211;
&#8230;&#8230;
</div>

<p>退信的原因是“Sector 5“，而这个神秘的“Sector 5“ google并没有给出详细的内容，哪怕是出处也没有。网上也有很多人遇到了类似的情况，甚至在一些google groups中也有讨论，但都没有结果，google官方也没有出面指明。不过，今天在看某个电影的时候，发现了这个：</p>

<p><a href="http://www.flickr.com/photos/fwolf/377881024/" title="Photo Sharing"><img src="http://farm1.static.flickr.com/186/377881024_99996a5675_m.jpg" width="240" height="180" alt="FBI WARNING" /></a></p>

<p>这是大家常见的“FBI WARNING”信息，就是警告信息，一般正规影碟前面都有的，告诉观众不得非法传播、拷贝，否则最高可以判5年徒刑或者罚款25万美元（够恨的），而在这些的法律依据，就是“Title 17 U.S.Code, Section 501, 506 and 508”。</p>

<p>所以我觉得gmail退信的原因，和这个Section 501等等有关系，没准就是同一部法律，或者类似的条款，都是关于版权保护的内容。gmail在我转发邮件的时候，发现转发的内容不是我写的，并且和其他groups中的文章惊人的相似，所以就以“Sector 5”为由，拒绝我传播“盗版”了。</p>

<h4>Update @ 2008-03-23</h4>

<p>找到一篇和508沾边但仍未完整解答我的疑问的文章：<a href="http://blog.163.com/mike_gz/blog/static/2475322008220103951817/">section 508</a></p>

	Tags: <a href="http://www.fwolf.com/blog/post/tag/508" title="508" rel="tag">508</a>, <a href="http://www.fwolf.com/blog/post/tag/accessbility" title="accessbility" rel="tag">accessbility</a>, <a href="http://www.fwolf.com/blog/post/tag/css" title="css" rel="tag">css</a>, <a href="http://www.fwolf.com/blog/post/tag/law" title="law" rel="tag">law</a>, <a href="http://www.fwolf.com/blog/post/tag/mail" title="mail" rel="tag">mail</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.fwolf.com/blog/post/176" title="让phpmailer支持中文名称的附件 (2006-05-23)">让phpmailer支持中文名称的附件</a> (5)</li>
	<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/17" title="我的email客户端软件选择历程 (2005-05-23)">我的email客户端软件选择历程</a> (7)</li>
	<li><a href="http://www.fwolf.com/blog/post/379" title="[Mutt]用msmtp替代esmtp作发信代理 (2008-01-12)">[Mutt]用msmtp替代esmtp作发信代理</a> (2)</li>
	<li><a href="http://www.fwolf.com/blog/post/93" title="WordPress在IE下Sidebar显示定位错误的问题已解决 (2005-12-04)">WordPress在IE下Sidebar显示定位错误的问题已解决</a> (3)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.fwolf.com/blog/post/284/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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 - 7 - strlen($this-&#62;CharSet);


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

	Tags: attach, chinese, mail, messy, PHP, phpmailer

	Related posts
	
	终于能够通过phpmailer使用gmail账号发送邮件了 (50)
	Gregarius编辑feed时中文乱码的解决 (1)
	截取固定长度的中文字符串 (2)
	在GLPI中输出中文PDF文件 [...]]]></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>
		<item>
		<title>终于能够通过phpmailer使用gmail账号发送邮件了</title>
		<link>http://www.fwolf.com/blog/post/155</link>
		<comments>http://www.fwolf.com/blog/post/155#comments</comments>
		<pubDate>Fri, 14 Apr 2006 08:25:16 +0000</pubDate>
		<dc:creator>Fwolf</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[phpmailer]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://www.fwolf.com/blog/post/155</guid>
		<description><![CDATA[phpmailer（现在的版本是1.73）是一个很好用的工具，可以很方便的使用php语言发送邮件，支持smtp及验证，我们一直都用它。

但是，由于gmail的smtp采用了ssl连接：


Outgoing Mail (SMTP) Server &#8211; requires TLS: smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587  


使用phpmailer就无法正常连接gmail的发信服务器了，并且这个问题一直没有得到phpmailer的官方解决，不过在sf.net上面的讨论里倒是找到了一点资料，采用下面这种方法就可以连接gmail了。

修改class.smtp.php，第101行，把


        $this->smtp_conn = fsockopen($host,    # the host of the server


改成


        $this->smtp_conn = fsockopen(&#8217;ssl://&#8217; . $host, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sourceforge.net/projects/phpmailer">phpmailer</a>（现在的版本是1.73）是一个很好用的工具，可以很方便的使用php语言发送邮件，支持smtp及验证，我们一直都用它。</p>

<p>但是，由于gmail的smtp采用了ssl连接：</p>

<div class="code">
Outgoing Mail (SMTP) Server &#8211; requires TLS: smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587  
</div>

<p>使用phpmailer就无法正常连接gmail的发信服务器了，并且这个问题一直没有得到phpmailer的官方解决，不过在<a href="http://sourceforge.net/forum/forum.php?thread_id=1222165&#038;forum_id=81620">sf.net上面的讨论</a>里倒是找到了一点资料，采用下面这种方法就可以连接gmail了。</p>

<p>修改class.smtp.php，第101行，把</p>

<div class="code">
        $this->smtp_conn = fsockopen($host,    # the host of the server
</div>

<p>改成</p>

<div class="code">
        $this->smtp_conn = fsockopen(&#8217;ssl://&#8217; . $host,    # the host of the server
</div>

<p>这样就可以了，就是指定使用ssl协议连接主机，当然php的openssl模块必须打开：</p>

<div class="code">
extension=php_openssl.dll
</div>

<p>但是这样就不能使用非ssl的主机了，我也不像在include目录下面放两份phpmailer，于是，又琢磨出了一种更好的方式：</p>

<p>打开class.phpmailer.php，在大概543行附近，函数SmtpConnect()中，找到：</p>

<div class="code">
        $this->smtp->do_debug = $this->SMTPDebug;
        $hosts = explode(&#8220;;&#8221;, $this->Host);
        $index = 0;
        $connection = ($this->smtp->Connected()); 

        // Retry while there is no connection
        while($index < count($hosts) &#038;&#038; $connection == false)
        {
            if(strstr($hosts[$index], ":"))
                list($host, $port) = explode(":", $hosts[$index]);
            else
            {
                $host = $hosts[$index];
                $port = $this->Port;
            }
</div>

<p>这一段的部分功能就是，如果你输入的主机名中带有端口号，自动的识别出来，设想虽好，但就是这部分让我们无法直接在调用的时候使用ssl格式的主机名，因为“ssl://xxx”的形式会被误认为是主机：端口号的形式，另外端口号一般都比较固定，我们手工设置好了，也不必一定要和主机一起赋值，所以在上面的代码后面添加：</p>

<div class="code">
            //Modify by Fwolf @ 2006-4-14, to enable ssl mail connection
            $host = $this->Host;
            $port = $this->Port;
</div>

<p>就可以了，使用正常smtp主机的时候，用法不变，使用gmail的时候，使用ssl://smtp.gmail.com作为主机名就可以了，唯一稍微麻烦一些的就是端口需要手工指定——其实也不麻烦。</p>

<p>按照上面的配置更改后，使用gmail账号发送邮件时还会有一条警告信息：</p>

<div class="code">
Warning: fgets(): SSL: fatal protocol error in H:\php_includes\phpmailer_ssl\cla
ss.smtp.php on line 1024
</div>

<p>这各警告信息在<a href="http://www.php.net/manual/en/function.fopen.php">php的帮助</a>里面有，好像是在使用ssl连接的时候，读文件读到文件尾的时候出现的问题，需要手工屏蔽，或者人为控制读的长度，我们用最简单的方式禁止警告信息显示就可以了，找到class.smtp.php的1024行，在fgets($this->smtp_conn,515)函数前面加上个@就可以了。</p>

<p>下面是一个完整的使用phpmailer发送邮件的函数：</p>

<div class="code">
function send_mail($to_address, $to_name ,$subject, $body, $attach = &#8221;)
{
    //使用phpmailer发送邮件
    require_once(&#8220;phpmailer/class.phpmailer.php&#8221;);
    $mail = new PHPMailer();
    $mail-&gt;IsSMTP();                                      // set mailer to use SMTP
    $mail-&gt;CharSet = &#8216;utf-8&#8242;;
    $mail-&gt;Encoding = &#8216;base64&#8242;;
    $mail-&gt;From = &#8216;fwolf.mailagent@gmail.com&#8217;;
    $mail-&gt;FromName = &#8216;Fwolf&#8217;;
    //$mail-&gt;Sender = &#8216;fwolf.mailagent@gmail.com&#8217;;
    //$mail-&gt;ConfirmReadingTo = &#8216;fwolf.mailagent@gmail.com&#8217;;     //回执？
    
    $mail-&gt;Host = &#8217;ssl://smtp.gmail.com&#8217;;
    $mail-&gt;Port = 465;       //default is 25, gmail is 465 or 587
    $mail-&gt;SMTPAuth = true;
    $mail-&gt;Username = &#8220;fwolf.mailagent@gmail.com&#8221;;
    $mail-&gt;Password = &#8220;xxx&#8221;;
    
    $mail->&gt;ddAddress($to_address, $to_name);
    //$mail-&gt;AddReplyTo(&#8216;fwolf.mailagent@gmail.com&#8217;, &#8220;Fwolf&#8221;);   //针对gmail无用，gmail是In-Reply-To:，phpmailer默认生成的是Reply-to:
    $mail-&gt;WordWrap = 50;
    if (!empty($attach))
        $mail-&gt;AddAttachment($attach);
    $mail-&gt;IsHTML(false);
    $mail-&gt;Subject = $subject;
    $mail-&gt;Body    = $body;
    //$mail-&gt;AltBody = &#8220;This is the body in plain text for non-HTML mail clients&#8221;;

    if(!$mail-&gt;Send())
    {
       echo &#8220;Mail send failed.\r\n&#8221;;
       echo &#8220;Error message: &#8221; . $mail-&gt;ErrorInfo . &#8220;\r\n&#8221;;
       return false;
    }
    else
    {
        echo(&#8220;Send $attach to $to_name &lt;$to_address&gt; successed.\r\n&#8221;);
        return true;
    }
    //echo &#8220;Message has been sent&#8221;;
    //
}
</div>

<h4>update @ 2006-11-3</h4>

<p>感谢RainChen提供的fgets出错提示的更好解决办法（位置大概在class.smtp.php的1024行）：
将：</p>

<div class="code">fgets($this-&gt;smtp_conn,515)</div>

<p>改为：</p>

<div class="code">!feof($this-&gt;smtp_conn) &#038;&#038; $str = fgets($this-&gt;smtp_conn,515)</div>

<p>这样的代码就规范多了，极力不提倡使用@来屏蔽错误信息。
（未测试， 但有网友反映这种改法是不行的）</p>

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

<p>试了最新的phpMailer 2.1.0 Beta 2，不用作任何修改，就可以用gmail账号发送邮件了，<a href="http://phpmailer.codeworxtech.com/changelog.html">官方文档</a>中还给出了例子，和我用的方法不太一样。</p>

<p>不过<a href="176">中文附件的问题</a>依然没有解决，需要自己hack。</p>

	Tags: <a href="http://www.fwolf.com/blog/post/tag/gmail" title="gmail" rel="tag">gmail</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/php" title="PHP" rel="tag">PHP</a>, <a href="http://www.fwolf.com/blog/post/tag/phpmailer" title="phpmailer" rel="tag">phpmailer</a>, <a href="http://www.fwolf.com/blog/post/tag/smtp" title="smtp" rel="tag">smtp</a>, <a href="http://www.fwolf.com/blog/post/tag/ssl" title="ssl" rel="tag">ssl</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.fwolf.com/blog/post/176" title="让phpmailer支持中文名称的附件 (2006-05-23)">让phpmailer支持中文名称的附件</a> (5)</li>
	<li><a href="http://www.fwolf.com/blog/post/379" title="[Mutt]用msmtp替代esmtp作发信代理 (2008-01-12)">[Mutt]用msmtp替代esmtp作发信代理</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>
	<li><a href="http://www.fwolf.com/blog/post/443" title="配置安全的共享web服务器（抛砖引玉） (2009-06-09)">配置安全的共享web服务器（抛砖引玉）</a> (4)</li>
	<li><a href="http://www.fwolf.com/blog/post/273" title="配置Apache的ssl安全连接 (2006-12-27)">配置Apache的ssl安全连接</a> (3)</li>
</ul>

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