<?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>OrangeWeb. &#187; PHP</title>
	<atom:link href="http://www.web-design-melbourne.net/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-design-melbourne.net</link>
	<description>A Little Bit of Orange Never Hurt Anyone</description>
	<lastBuildDate>Tue, 24 Nov 2009 11:19:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Quick Automatic PHP Mailer</title>
		<link>http://www.web-design-melbourne.net/2009/06/05/quick-automatic-php-mailer/</link>
		<comments>http://www.web-design-melbourne.net/2009/06/05/quick-automatic-php-mailer/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 15:46:25 +0000</pubDate>
		<dc:creator>Michael Raffaele</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Website Development]]></category>
		<category><![CDATA[php mailer]]></category>
		<category><![CDATA[simple php mailer]]></category>

		<guid isPermaLink="false">http://www.web-design-melbourne.net/?p=67</guid>
		<description><![CDATA[I am not a web developer, nor do I pretend to be, but as a web designer I do like to dabble. I have built up quite a range of useful scripts that I have written and I continually use over and again.
A very useful and simple script, commonly used by myself, is an automatic [...]]]></description>
			<content:encoded><![CDATA[<p>I am not a web developer, nor do I pretend to be, but as a <a href="http://www.web-design-melbourne.net/">web designer</a> I do like to dabble. I have built up quite a range of useful scripts that I have written and I continually use over and again.</p>
<p>A very useful and simple script, commonly used by myself, is an automatic PHP mailer using a FOREACH loop to get all the POST data. This method is quick and easy to implement.</p>
<h5>Why should I use a FOREACH loop in a PHP mailer?</h5>
<p>The answer is simple, this script enables you to get all the field names and values from a form automatically. Lets have a look what I mean..</p>
<p><img class="alignnone size-full wp-image-70" title="simple-form" src="http://www.web-design-melbourne.net/wp-content/uploads/2009/06/simple-form.jpg" alt="simple-form" width="327" height="145" /><span class="caption">A very simple form using only 3 text fields</span></p>
<p>If I was to email the form results using PHP, on my confirm page I would have code very similar to the following&#8230;</p>
<div class="code"><span class="code-text">Code:</span><br />
&lt;?php<br />
$to = &#8220;test@thisisatestemail.com.au&#8221;;<br />
$from = &#8220;no-reply@thisisatestemail.com.au&#8221;;<br />
$subject = &#8220;This is my subject&#8221;;</p>
<p>$message = &#8220;Name: &#8221; . $_POST['fname'] . &#8220;\n&#8221;;<br />
$message .= &#8220;Last Name: &#8221; . $_POST['lname'] . &#8220;\n&#8221;;<br />
$message .= &#8220;Email: &#8221; . $_POST['email'];</p>
<p>$headers = &#8220;From: $from&#8221;;</p>
<p>mail($to, $subject, $message, $headers)<br />
?&gt;</p></div>
<p>Now, don&#8217;t get me wrong, this works great, and you do have ultimate control over what fields are displayed and how. But imagine a form with say 30+ fields? You don&#8217;t want to have to append the field name to the message 30+ times. For 3 immediate reasons:</p>
<p><strong>1.</strong> It can be a complete waste of time<br />
<strong>2. </strong> As there is more code, the chances for a syntax error greatly increase<br />
<strong>3.</strong> It is repetitive and boring to write</p>
<p>The easiest way around it is to create a FOREACH loop for all the values in POST. An example of said code is as follows:</p>
<div class="code"><span class="code-text">Code:</span><br />
$to = &#8220;test@thisisatestemail.com.au&#8221;;<br />
$from = &#8220;no-reply@thisisatestemail.com.au&#8221;;<br />
$subject = &#8220;This is my subject&#8221;;<br />
$message       = &#8220;&#8221;;<br />
$headers       = &#8220;From: $from\r\n&#8221;;<br />
//This is the loop that grabs POST values and adds them to the variable<br />
foreach($_POST AS $field =&gt; $value) {<br />
$message    .= $field . &#8220;: &#8221; . $value .&#8221;\r\n&#8221;;<br />
}<br />
mail($to, $subject, $message, $headers);</div>
<p>Basically this code grabs all the fields and values in POST and appends them to the variable $message, which is then mailed. You can take it a little bit further and exclude certain fields that you do not want to see in your email, such as the submit button.</p>
<p>You can download a working form using this PHP mailer <a href="http://www.web-design-melbourne.net/wp-content/uploads/2009/06/mailer.rar" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-melbourne.net/2009/06/05/quick-automatic-php-mailer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
