<?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>Leeiio Chaos Made. &#187; 邮件头</title>
	<atom:link href="http://leeiio.me/tag/%e9%82%ae%e4%bb%b6%e5%a4%b4/feed/" rel="self" type="application/rss+xml" />
	<link>http://leeiio.me</link>
	<description>The site is redesigning,please be patient  :P</description>
	<lastBuildDate>Sun, 29 Jan 2012 16:40:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Mail to Commenter插件的发件人邮箱以及名称的问题修正</title>
		<link>http://leeiio.me/mail-to-commenter-sendmail-from/</link>
		<comments>http://leeiio.me/mail-to-commenter-sendmail-from/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 14:59:34 +0000</pubDate>
		<dc:creator>Leeiio</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[$headers]]></category>
		<category><![CDATA[邮件头]]></category>
		<category><![CDATA[Mail to Commenter]]></category>
		<category><![CDATA[发件人邮箱]]></category>

		<guid isPermaLink="false">http://leeiio.me/?p=1718</guid>
		<description><![CDATA[Mail to Commenter是一款评论辅助插件，通过判断评论内容是否含有‘@user ‘或‘@user:’的代码，然后将评论内容用邮件发送给user。我一直在使用这款插件，当然同时也推荐给还没有听闻或听闻了但还在观望的朋友。可是这款插件有个问题，相信很多朋友都遇到了，就是发件人名称以及邮箱地址的问题。 WordPress默认的发邮件的地址是wordpress@yourdomain.com，当然这个地址也是可以修改的。wordpress安装目录下，打开wp-includes\pluggable.php文件，搜索“wordpress@”，有两处，替换成你希望的名字就可以了。不过我们这里不讨论这个问题，虽然这个问题我也顺便说了解决方法了，相信也有不少朋友会想修改这个地址。 没错，正常情况下确实是这样的，通过 wordpress 后台发送给用户的邮件地址应该是 wordpress@yourdomain.com，可是偏偏给我碰到了不正常的情况，在我为解决之前搜索了下发现确实也有很多的朋友都有这个问题，Mail to Commenter作为一款优秀的使用广泛的插件就遭遇了这个问题。通常如果你的主机是合租性质的，并且不像 Hostmonster之类的可以通过在每个子账户根目录复制一个php.ini文件来配置的话，那么在使用 Mail to Commenter 插件通过你的 WordPress 程序发送给@user中的user的信件的邮件地址的后缀极有可能是root帐户配置的那个域名后缀。 比如我的主机是在淫荡的大猫的庙那里买的合租的 MediaTemple，root帐户配置的邮件地址是root(at)yslow(dot)info，那么我的 Mail to Commenter插件发送给每一个被我@user的user用户收到的邮件都将会显示这个邮件地址而不是我希望的默认的 wordpress@yourdomain.com或者我希望的任意一个指定的email地址。当然奇怪的是并非所有人都会碰到这个问题，所以作者也并没有完全修正这个问题。在这个YD的感冒飞扬的日子里，我留着鼻涕，打着喷嚏，打开了Mail to Commenter插件里的mailtocommenter_functions.php文件。因为本身就是插件，自然没必要请出淫荡的add_filter钩子大仙出山。邮件地址的问题无非是wp_mail()插件里面的$header变量引起的，那就找找看插件里有没有对$header的定义咯。 这里有必要对wp_mail()函数进行一些粗略简单的介绍((wp_mail()函数的更具体描述请看官方codex文档))。wp_mail()从命名方式就可以看出这个是wordpress发送email的核心函数。wp_mail()和php自带的mail()函数的参数差不多，注意它们都有对header信息的定义只不过都是可选的参数并不是必填参数，而当header参数未填写的时候，发件人也就是sendmail_from的定义就存在于php.ini里面了。 &#60;?php wp_mail&#40; $to, $subject, $message, $headers, $attachments &#41;; ?&#62; 继续回到mailtocommenter_functions.php文件。搜索header查找到了这么一段函数。 function mailtocommenter_send_email&#40;$to,$subject,$message&#41;&#123; $blogname &#8230; <a href="http://leeiio.me/mail-to-commenter-sendmail-from/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://imgs.leeiio.me/blogimgs/2009/11/20091124_sendmail.gif" alt="sendmail邮件头引发的血案" /><br />
<a rel="nofollow" href="http://www.thinkagain.cn/archives/989.html">Mail to Commenter</a>是一款评论辅助插件，通过判断评论内容是否含有‘@user ‘或‘@user:’的代码，然后将评论内容用邮件发送给user。我一直在使用这款插件，当然同时也推荐给还没有听闻或听闻了但还在观望的朋友。可是这款插件有个问题，相信很多朋友都遇到了，就是发件人名称以及邮箱地址的问题。</p>
<p>WordPress默认的发邮件的地址是wordpress@yourdomain.com，当然这个地址也是可以修改的。wordpress安装目录下，打开wp-includes\pluggable.php文件，搜索“wordpress@”，有两处，替换成你希望的名字就可以了。不过我们这里不讨论这个问题，虽然这个问题我也顺便说了解决方法了，相信也有不少朋友会想修改这个地址。<br />
<span id="more-1718"></span><br />
没错，正常情况下确实是这样的，通过 wordpress 后台发送给用户的邮件地址应该是 wordpress@yourdomain.com，可是偏偏给我碰到了不正常的情况，在我为解决之前搜索了下发现确实也有很多的朋友都有这个问题，Mail to Commenter作为一款优秀的使用广泛的插件就遭遇了这个问题。通常如果你的主机是合租性质的，并且不像 <a rel="nofollow" href="http://www.hostmonster.com/track/leeiio">Hostmonster</a>之类的可以通过在每个子账户根目录复制一个php.ini文件来配置的话，那么在使用 Mail to Commenter 插件通过你的 WordPress 程序发送给@user中的user的信件的邮件地址的后缀极有可能是root帐户配置的那个域名后缀。</p>
<p>比如我的主机是在淫荡的<a rel="nofollow" href="http://ooxx.me">大猫</a>的<a rel="nofollow" href="http://miao.in">庙</a>那里买的合租的 MediaTemple，root帐户配置的邮件地址是root(at)yslow(dot)info，那么我的 Mail to Commenter插件发送给每一个被我@user的user用户收到的邮件都将会显示这个邮件地址而不是我希望的默认的 wordpress@yourdomain.com或者我希望的任意一个指定的email地址。当然奇怪的是并非所有人都会碰到这个问题，所以作者也并没有完全修正这个问题。在这个YD的感冒飞扬的日子里，我留着鼻涕，打着喷嚏，打开了Mail to Commenter插件里的mailtocommenter_functions.php文件。因为本身就是插件，自然没必要请出淫荡的<a href="http://codex.wordpress.org/Function_Reference/add_filter" rel="nofollow">add_filter</a>钩子大仙出山。邮件地址的问题无非是wp_mail()插件里面的$header变量引起的，那就找找看插件里有没有对$header的定义咯。</p>
<p>这里有必要对wp_mail()函数进行一些粗略简单的介绍((wp_mail()函数的更具体描述请看官方<a rel="nofollow" href="http://codex.wordpress.org/Function_Reference/wp_mail">codex</a>文档))。wp_mail()从命名方式就可以看出这个是wordpress发送email的核心函数。wp_mail()和php自带的<a rel="nofollow" href="http://php.net/manual/en/function.mail.php">mail()</a>函数的参数差不多，注意它们都有对header信息的定义只不过都是可选的参数并不是必填参数，而当header参数未填写的时候，发件人也就是sendmail_from的定义就存在于php.ini里面了。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_mail<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attachments</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>继续回到mailtocommenter_functions.php文件。搜索header查找到了这么一段函数。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mailtocommenter_send_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$blogname</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blogname'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//获取博客名称赋值到$blogname变量里(leeiio注)</span>
	<span style="color: #000088;">$charset</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog_charset'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$headers</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: <span style="color: #006699; font-weight: bold;">$blogname</span> <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//设置header里面的from为$blogname(leeiio注)</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;MIME-Version: 1.0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: text/html;charset=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$charset</span><span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #339933;">@</span>wp_mail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>也就是说，这段函数将博客的名称设置成了From，难怪我的博客通过Mail to Commenter发出去的发件人都显示成了如图所示。<br />
<img src="http://imgs.leeiio.me/blogimgs/2009/11/20091124_sendmail1.gif" alt="Mail to Commenter插件的发件人邮箱以及名称的问题修正" /><br />
十分诡异，就算获取博客名称也应该是Leeiio Chaos Made@yslow.info才对，可能是其中获取参数的时候程序处理秀逗了，因为我对php也是七窍通了六窍，所以各种的缘由还有待大师来解惑。(呃，懒得去翻wordpress的函数去查原因。。)</p>
<p>既然如此，那么我就只好采取强硬的手段来达到我的目的了。于是修改后的函数为：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mailtocommenter_send_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$blogname</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blogname'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$charset</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog_charset'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'From: Leeiio Chaos Made &lt;no-reply@leeiio.me&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//指定了发件人名称以及发件人邮箱，注意&lt;&gt;括号之前和发件人名称用空格隔开</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;MIME-Version: 1.0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: text/html;charset=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$charset</span><span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #339933;">@</span>wp_mail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>于是，如图所示。<br />
<img src="http://imgs.leeiio.me/blogimgs/2009/11/20091124_sendmail2.gif" alt="Mail to Commenter插件的发件人邮箱以及名称的问题修正" /></p>
<p>根据我的一知半解，邮件header信息里面的From:要么是填写纯粹的邮件地址xxx@xxx.com要不是填写带<>的<xxx@xxx.com又或者想指定显示名称的话就填写XYZ <xxx@xxx.com>。但切勿只填写发件人名称，按照wordpress的wp_mail()函数处理的话，就有可能造成上图的诡异现象了。</p>
<p>废话一大堆，希望你们看懂了。不懂的话就在下面提问吧，本大湿会一一解答的。</p>
<p><em>题外话：现在很多的垃圾邮件都通过伪造邮件头信息来达到蒙蔽广大群众的目的，望各位火眼金睛，分辨仔细。并不是看到发件人是xxx@mircosoft.com的邮件就是微软发给你的中奖邮件，也有可能是骗子发给你的。</em><br />
延伸阅读：<a rel="nofollow" href="http://www.chinamyhosting.com/email/forge.php">有人用我的电子邮件帐户发垃圾邮件？</a></p>
]]></content:encoded>
			<wfw:commentRss>http://leeiio.me/mail-to-commenter-sendmail-from/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
	</channel>
</rss>

