<?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>Richard Lord &#187; Mail</title>
	<atom:link href="http://www.richardlord.net/blog/tag/mail/feed" rel="self" type="application/rss+xml" />
	<link>http://www.richardlord.net</link>
	<description>Actionscript/Flex, PHP and Java developer</description>
	<lastBuildDate>Fri, 25 Jun 2010 07:34:13 +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>PHP mail() function and GMail</title>
		<link>http://www.richardlord.net/blog/php-mail-and-gmail</link>
		<comments>http://www.richardlord.net/blog/php-mail-and-gmail#comments</comments>
		<pubDate>Fri, 01 Dec 2006 17:26:12 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[GMail]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/php-mail-and-gmail/</guid>
		<description><![CDATA[<p>or Another reason to avoid PHP's mail() function...</p>]]></description>
			<content:encoded><![CDATA[<p>or <strong>Another reason to avoid PHP&#8217;s mail() function</strong></p>

<p>Since launching <a href="http://www.giefan.com/">Giefan.com</a> on Monday we&#8217;ve fixed a few bugs that only came to light as a result of the way other people work. One such was an email problem with messages sent to GMail (thanks to Alan for catching this). I&#8217;ve never had a GMail account so it wasn&#8217;t one of the email applications we tested the messages with.</p>

<p>Email messages were arriving at GMail with messages garbled into a string of apparently random characters and with the sender listed as unknown. I was using <a href="http://php.net/manual/en/function.mb-send-mail.php">mb_send_mail()</a> to send UTF-8 email messages from PHP. I always intended to implement a different email solution at some point but evaluating the various possibilities and making a choice didn&#8217;t seem as important for the launch period as many of the other features so I chose to start with mb_send_mail() and switch to another solution later.</p>

<p>mb_send_mail() was sending messages as UTF-8 and base64 encoded, and it seems that GMail failed to correctly understand this from the email headers. As a result, GMail simply displayed the base64 encoded data. The quick fix just to get it working was to switch to PHP&#8217;s <a href="http://php.net/manual/en/ref.mail.php">mail()</a> function. This would suffice for a few hours while we sought a better solution.</p>

<p>The other problem was GMail displaying the sender as unknown, despite having the sender data in the From: header. I tried various different formats for the From: header (quotes around the name, extra spaces etc.) but none seemed to work for GMail. Instead, it always displayed the From: header line in the body of the message (there were no blank lines in the header so that&#8217;s not the problem, and the messages worked with a number of other email applications we tested them with).</p>

<p>I still haven&#8217;t discovered what the problem was but have three possibilities.</p>
<ol>
<li>Does GMail dislike the unconventional (but not technically wrong) ordering of the headers that mail() and mb_send_mail() produce?</li>
<li>Does GMail distrust messages in which the X-Mailer header lists &#8220;PHP mailer&#8221; as the system used and consequently displays the sender as unknown and places the from data in the message?</li>
<li>Is mail() doing something wrong in its headers, and most email applications are simply ignoring the mistake and figuring out what&#8217;s intended?</li>
</ol>
<p>If anyone knows the actual cause please add a comment below.</p>

<p>Anyway, the obvious solution was to avoid PHP&#8217;s mail functions altogether so I hurriedly added Chris Corbyn&#8217;s <a href="http://www.swiftmailer.org/">Swift Mailer</a> script and altered my code to use it, and all was well. I haven&#8217;t thoroughly tested Swift Mailer yet but it seems to be working fine &#8211; the messages are in UTF-8 and GMail is happy with them.</p>

<p>Swift Mailer is just one of <a href="http://phpmailer.sourceforge.net/">many</a> <a href="http://pear.php.net/package/Mail">alternatives</a> <a href="http://framework.zend.com/manual/en/zend.mail.html">to the</a> mail() function but I like it for it&#8217;s simplicity of use. Hopefully, when I test it out more thoroughly later this month I&#8217;ll still be very happy with it.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/php-mail-and-gmail/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
