<?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>HTML form guide &#187; php form email</title>
	<atom:link href="http://www.html-form-guide.com/blog/category/php-form-email/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.html-form-guide.com/blog</link>
	<description>How to make good web forms</description>
	<lastBuildDate>Wed, 28 Apr 2010 11:41:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Why is my PHP script not sending emails?</title>
		<link>http://www.html-form-guide.com/blog/php-form-email/3/php-script-not-sending-email/</link>
		<comments>http://www.html-form-guide.com/blog/php-form-email/3/php-script-not-sending-email/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 04:52:18 +0000</pubDate>
		<dc:creator>prasanth</dc:creator>
				<category><![CDATA[php form email]]></category>
		<category><![CDATA[email form]]></category>
		<category><![CDATA[php email script]]></category>

		<guid isPermaLink="false">http://www.html-form-guide.com/blog/?p=3</guid>
		<description><![CDATA[ 
		digg_url = "http://www.html-form-guide.com/blog/php-form-email/3/php-script-not-sending-email/";
	 
	It has happened too often to me to get stuck with a &#8216;form to email&#8217; script. Everything appears right but the email sent by the script never reaches the destination address.
This article takes you through the steps to troubleshoot the problem.

Email : background 
email is one of the earliest communication systems [...]]]></description>
			<content:encoded><![CDATA[<div id="socialize-in-content"><div class="socialize-in-button"><script type="text/javascript"> 
		digg_url = "http://www.html-form-guide.com/blog/php-form-email/3/php-script-not-sending-email/";
	</script> 
	<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div></div><p>It has happened too often to me to get stuck with a &#8216;form to email&#8217; script. Everything appears right but the email sent by the script never reaches the destination address.</p>
<p>This article takes you through the steps to troubleshoot the problem.<br />
<span id="more-3"></span></p>
<h2>Email : background </h2>
<p>email is one of the earliest communication systems evolved on the internet. Though email is still the most prevalent communication system on the internet, email spam has grown to be a huge problem.</p>
<p>About <strong>100 billion</strong> spam emails are being sent every single day (yeah, not a typo; 100 billion, every day!). Over 94% of all email sent is spam. Every email server gets a fair share of that 100 billion and has to cope with the bombardment of such a huge amount of spam. Therefore, email servers (MTA: Mail Transfer Agents)  have equipped themselves with methods to scrutinize the email messages and reject the email if it suspects the message to be spam.</p>
<p>This is the reason why your script should craft the email carefully to avoid the message being rejected.<br />
<img src="http://www.html-form-guide.com/blog/wp-content/uploads/2009/09/email-bombardment.png" alt="email-bombardment" width="394" height="277" class="alignnone size-full wp-image-36" /></p>
<h2>The &#8216;From&#8217; field and source address spoofing</h2>
<p>An email message has two parts: a header and the message itself. The header contains a few pieces of identifying information. The &#8216;From&#8217; field is one of the email header fields. The purpose of &#8216;From&#8217; field is to indicate who sent the email. However, the email sender can set the &#8216;From&#8217; field to be any address of his choice.<br />
Spammers exploit this aspect of the protocol. For example, a spammer can send an email making it appear like the email is coming from security@bank.com and ask the victim to give the bank log-in details. The spammer does not need access to the bank server to do this. Adding a &#8220;From: security@bank.com&#8221; header to the email will make the users think that the email is coming from the bank.<br />
For details see  <a href="http://en.wikipedia.org/wiki/E-mail_source_address_spoofing">email source address spoofing</a></p>
<h2>The cure: SPF</h2>
<p><a href="http://en.wikipedia.org/wiki/Sender_Policy_Framework">Sender Policy Framework (SPF)</a> was introduced to prevent email source address spoofing.<br />
Here is how it works:<br />
Let&#8217;s say a spammer is sending you a fake message.</p>
<ul>
<li>Your email server receives the message from the spammer.</li>
<li>The email server looks for the &#8216;From&#8217; address in the email header.<br />
Suppose &#8216;From&#8217; field is &#8217;security@bank.com&#8217;</li>
<li>Your email server requests for the DNS records of bank.com domain ( in simple terms, DNS is for resolving a domain name like bank.com to an IP address ). The DNS records of a domain can contain SPF records. The SPF records will indicate which IP addresses can send email on its behalf. Most of the time, it will only be the main mail server (example: bank.com in this case). Since the spammer&#8217;s IP address is different, the email gets rejected.</li>
</ul>
<p>Imagine you received a phone call and the caller claimed that he is calling from the bank. You can verify it by looking up the incoming call&#8217;s phone number in the telephone directory. The SPF system follows the same method.</p>
<h2>Setting the &#8216;From&#8217; field right</h2>
<p>Have a &#8216;From&#8217; field in the emails that you sent through the script. The From address should belong to the domain from where you are running the script. If your script is running on your-website.com then the From address should be like someone@your-website.com.</p>
<p>Sample PHP code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$headers</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: someone@your-website.com&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span><span style="color: #000088;">$subj</span><span style="color: #339933;">,</span><span style="color: #000088;">$body</span><span style="color: #339933;">,</span><span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What if you want to have some-other.com as the from address?<br />
You have update the SPF records of some-other.com to indicate that your-website.com can send emails on behalf of some-other.com. See: <a href="http://www.openspf.org">openspf.org</a> for details.</p>
<h2>Replying to the visitor who submitted a form</h2>
<p>When sending email from a &#8216;form to email&#8217; script, it will be convenient to have the website visitor&#8217;s email address in the &#8216;From&#8217; field. You can reply to the message by just pressing the &#8216;Reply&#8217; button in the email client. However, this can result in the email not reaching you.</p>
<p>For example, suppose this is the form to email code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$visitor_email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$headers</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: <span style="color: #006699; font-weight: bold;">$visitor_email</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span><span style="color: #000088;">$subj</span><span style="color: #339933;">,</span><span style="color: #000088;">$body</span><span style="color: #339933;">,</span><span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, if someone submits their email address as &#8217;someone@a-company.com&#8217;, when the email server receives this email message, it checks whether your website can send emails on behalf of  a-company.com domain and ends up rejecting the email.</p>
<p>The right alternative is to use the &#8216;Reply-To&#8217; email header. Set the &#8216;From&#8217; to be an address belonging to your web site domain and add a &#8216;Reply-To&#8217; header with the web site visitor&#8217;s email address.</p>
<p>Example code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$headers</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: name@your-website.com&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Reply-To: <span style="color: #006699; font-weight: bold;">$visitor_email</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span><span style="color: #000088;">$subj</span><span style="color: #339933;">,</span><span style="color: #000088;">$body</span><span style="color: #339933;">,</span><span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Keep in mind that you need to <a href="http://www.phpro.org/examples/Sanitize-email.html">sanitize</a> all the values used in the header attribute of the mail().</p>
<h2>Make sure the php.ini is configured right</h2>
<p>If you are still not receiving the emails, the problem could be in the PHP setup. The mail should be configured in the PHP configuration file php.ini.</p>
<p>The following code shows a typical PHP mail configuration (using sendmail)</p>
<pre>
[mail function]
; Setup for Linux systems
sendmail_path = /usr/sbin/sendmail -t
sendmail_from = me@myserver.com
</pre>
<p>The following code shows SMTP configuration in php.ini</p>
<pre>
[mail function]
; Setup for Windows systems
SMTP = smtp.my.isp.net
sendmail_from = me@myserver.com
</pre>
<p>Note that you can update the php.ini only if you are running your own web server. Instead, if you are hosting the website with a web hosting service, contact the service provider to setup php mail right.</p>
<h2>A simple script to test your PHP &#8211;&gt; mail configuration</h2>
<p><a href="http://www.html-form-guide.com/downloads/sample-emailer.zip">Download this php email script</a>. Edit the file and update the $from_add and $to_add variables. (see the instructions in the code). Upload the script to your web site. Access the page. You will see a submit button on the page. Press the submit button to send an email to yourself.<br />
If you are not getting the email still, get help from your hosting service provider/network admin. Point them to this sample script that fails to send email.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.html-form-guide.com/blog/php-form-email/3/php-script-not-sending-email/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
