<?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>API Programming &#187; PHP</title>
	<atom:link href="http://www.brangle.com/wordpress/category/tutorials/php-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brangle.com/wordpress</link>
	<description>Just another computer weblog</description>
	<lastBuildDate>Wed, 14 Dec 2011 07:35:21 +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>How To Create a Simple Contact Us Page in PHP</title>
		<link>http://www.brangle.com/wordpress/2009/08/how-to-create-a-simple-contact-us-page-in-php/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/how-to-create-a-simple-contact-us-page-in-php/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 06:27:27 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=107</guid>
		<description><![CDATA[Every website should have a &#34;Contact Us&#34; form, with spam as horrific as it is in this day age webmasters can no longer feel safe just pasting there email address on the websites. I created what I consider to be a bare bones contact us form, which you can see a <a href="http://www.brangle.com/api/how-to-create-a-simple-contact-us-page-in-php.php" target="_blank">very similar example</a> of. Also, make sure to scroll past the source code to view some recommended changes and additions to the form. [...]]]></description>
			<content:encoded><![CDATA[<p>Every website should have a &quot;Contact Us&quot; form, with spam as horrific as it is in this day age webmasters can no longer feel safe just pasting there email address on the websites.  I created what I consider to be a bare bones contact us form, which you can see a <a href="http://www.brangle.com/api/how-to-create-a-simple-contact-us-page-in-php.php" target="_blank">very similar example</a> of.  Also, make sure to scroll past the source code to view some recommended changes and additions to the form.<span id="more-107"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;body&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;b&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">':&lt;/b&gt; '</span><span style="color: #339933;">.</span><span style="color: #990000;">nl2br</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'example@example.com'</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'example@example.com'</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'New Message: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'subject'</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;">'MIME-Version: 1.0'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<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;">'Content-type: text/html; charset=UTF-8'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<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;">'From: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<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;">'Reply-To: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<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;">'Return-Path: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <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;">$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: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;b&gt;Your message has been sent&lt;/b&gt;'</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;form method=&quot;post&quot; action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
   &lt;td align=&quot;right&quot;&gt;Name:&lt;/td&gt;
   &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;Name&quot; value=&quot;&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td align=&quot;right&quot;&gt;Email Address:&lt;/td&gt;
   &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;Email&quot; value=&quot;&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td align=&quot;right&quot;&gt;Subject:&lt;/td&gt;
   &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;Subject&quot; value=&quot;&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td align=&quot;right&quot;&gt;Message:&lt;/td&gt;
   &lt;td&gt;&lt;textarea name=&quot;Message&quot; rows=&quot;10&quot; cols=&quot;30&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td&gt;&amp;nbsp; &lt;/td&gt;
   &lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Send Message&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><b>Recommended changes to the above script</b></p>
<ul>
<li>Add additional fields, I used a for each loop for a reason, so you can add as many fields as you want to without having to add them individually to the email message. For example, if we wanted to allow the user to enter their order number we can just add the following at line 38:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>38
39
40
41
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>td align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;right&quot;</span><span style="color: #339933;">&gt;</span>Order Number<span style="color: #339933;">:&lt;/</span>td<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;OrderNumber&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<li>Add additional data, it&#8217;s always a good idea to copy the users IP address and username (if they are logged in). One thing I like to do is append this additional information into the message. For example, if my site relies on sessions, I would append that to my emails, which you can accomplish by adding this:
<pre><span>$message .= &quot;&lt;hr&gt;&lt;pre&gt;&quot;. print_r($_SESSION, true) .&quot;&lt;/pre&gt;&quot;;</span></pre>
<p> If your site uses cookies, then you can replace <span class="BRinlineCode">$_SESSION</span> with <span class="BRinlineCode">$_COOKIES</span> and for overkill if you wanted to record the server data you could also append the <span class="BRinlineCode">$_SERVER</span> data.</p>
<li>Verify data before sending it, all of the info that the user entered above was never checked for accuracy. Obviously we can never make a foolproof checker, but we can eliminate 99% of common mistakes (IMO).  There&#8217;s the simple and the more advanced method of verifying user input, they are&#8230;
<ul>
<li>Checking string length using the <span class="BRinlineCode">strlen</span> function. For example, let&#8217;s take the shortest email address we can think of that would still be technically valid, x@x.ca this comes out to 6 characters.  So trimming off any white space
<pre><span>if (strlen(trim($_POST['Email'])) < 6)</span></pre>
<p> then we know we have an invalid email address. We might also want to check that the name, message, etc length is at least 2 characters long.</li>
<li>Regular expressions can be used to verify that accurate information was entered.  For example, if we wanted to check for an email address we could do that by doing something similar to
<pre><span>$isValidEmailAddr = ereg('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$', $_POST['Email']);</span></pre>
<p> if this is true, then we have a valid email address. Just remember, that not all regular expressions are created equally.</li>
</ul>
<li>Add a free <a href="http://www.captcha.net/" target="_blank">CAPTCHA</a> to prevent being flooded with spam.  Add this little captcha at the end of the form and if it&#8217;s not entered correctly you will need to refill in all of the users old information into the form</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/how-to-create-a-simple-contact-us-page-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generate MD5 and SHA1 Password Hash</title>
		<link>http://www.brangle.com/wordpress/2009/08/generate-md5-and-sha1-password-hash/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/generate-md5-and-sha1-password-hash/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 07:09:56 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=72</guid>
		<description><![CDATA[<p>It&#8217;s really simple to generate a MD5 and SHA1 hash using PHP, it&#8217;s so simple that I even considered not posting this blog. But then I found myself Googling a website to generate a MD5 hash while I was at work. So I created a simple form for you to create your own MD5 and [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s really simple to generate a MD5 and SHA1 hash using PHP, it&#8217;s so simple that I even considered not posting this blog. But then I found myself Googling a website to generate a MD5 hash while I was at work.  So I created a simple form for you to create your own MD5 and SHA1 hash.<span id="more-72"></span></p>
<p>To generate your MD5 and SHA-1 password hash, enter your text here:<br />
<iframe name="FRAME2" src="http://www.brangle.com/api/generate-md5-and-sha1-password-hash.php" width="500" height="125" frameborder="0" ></iframe></p>
<p>What can I do make my passwords more secure?<br />
One word: Salt. You want to salt all your hashes, and keep your salt private.  A salt is equivalent to adding extra miscellaneous data to your user&#8217;s password.  For example, according to <a href="http://en.wikipedia.org/wiki/Salt_%28cryptography%29" target="_blank">Wikipedia</a>:<br />
<blockquote>Assume a user’s (encrypted) secret key is stolen and he is known to use one of 200,000 English words as his password. The system uses a 32-bit salt. The salted key is now the original password appended to this random 32-bit salt. Because of this salt, the attacker’s pre-calculated hashes are of no value. He must calculate the hash of each word with each of 232 (4,294,967,296) possible salts appended until a match is found. The total number of possible inputs can be obtained by multiplying the number of words in the dictionary with the number of possible salts:</p>
<p>2^(32) * 200 000 = 8.58993459 * 10^(14)</p>
<p>To complete a brute-force attack, the attacker must now compute about 800 trillion hashes, instead of only 200,000. Even though the password itself is known to be simple, the secret salt makes breaking the password radically more difficult.</p></blockquote>
<p>So, how do I salt my password?<br />
Easy, just append any string, either at the beginning or end of the user&#8217;s password. Just remember, the most important factor is to keep it secret, do not share it with anyone!  Let&#8217;s assume the following is my PHP code
<pre><span>$salt = 'fenfnk4n2kt4$#%$%&amp;l%UR&agrave;EGM&Uuml;EWQTF4%&copy;YRTesadss&ocirc;d av  frH^%Ewqefty75$%^&amp;*()(765432';
$hash = md5($salt . $_POST['password']);</span></pre>
<p>Now everytime a user submits there password you can append this password, at the front, end or even both.  An added benefit to generating a hash using the above function call is when comparing this value to the one in your database, your value will be returned with raw_output set to false.  This means that it will return a 32bit hexadecimal digits (ie: only the numbers 0-9 and the letters a-f) with no special sql characters trying to cause some damage via a nasty sql injection.</p>
<p>What is hashing?<br />
To put it simple, hashing was created to be a one-way encryption.  Of course there&#8217;s no such thing as secure in this day in age, especially with brute force crackers out there. Ideally, hashing is done so that there is no way to recover the password, a good example is like taking a digital hammer which smashes a string exactly the same way each and every single time.  For example, if we were to take this digital hammer and try and create a MD5 hash for <b>123Password</b> we would get <b>d54b609242c7d758f6daca654bda1d26</b> every single time. If we were to add a space in the middle any other character for that matter it would &quot;break&quot; in a completely different pattern, for example <b>123 Password</b> would create the following hash: <b>ffeefad21f5f4a9f2b44992342d551ba</b>.  As you can see adding one space between 123 and Password created a completely different encryption key.</p>
<p>How to generate a MD5 and SHA1 hash in PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MySup3rSecretPassw0rd'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$md5Hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sha1Hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>How to generate a MD5 and SHA1 hash in MySQL</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">md5</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'MySup3rSecretPassw0rd'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #000099;">sha1</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'MySup3rSecretPassw0rd'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>How to generate a MD5 and SHA1 hash in binary using MSSQL. Remember, this is a binary output, not hex.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> HashBytes<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'MD5'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'password'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>HashBytes<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'SHA1'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'password'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/generate-md5-and-sha1-password-hash/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sending eMails via PHP using the mail() function</title>
		<link>http://www.brangle.com/wordpress/2009/08/sending-emails-via-php-using-the-mail-command/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/sending-emails-via-php-using-the-mail-command/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 07:23:20 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=23</guid>
		<description><![CDATA[Looking up the official <a href="http://www.php.net/manual/en/function.mail.php" target="_blank">PHP</a> guide, we can see how to use the mail function, I'll try and explain them here and give a few examples of how to add additional headers. Let's move along the function header from left to write, it is declared as the following: <pre><span>bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )</span></pre> The first thing we see is that the mail function returns a <a href="http://us2.php.net/manual/en/language.types.boolean.php" target="_blank">bool</a>, or boolean. So every time this function is run it will either tell you whether it was successfully sent or not. Now I want to be careful here, just because the message has been sent does not mean that the message ever made it to the users mailbox. The message could bounce back because you entered the wrong address, it was flagged as spam, the email address doesn't exist anymore, etc... This is kind of like mail with the post office, just like when you put a stamp on a letter and mail it, you can say that the message was sent, but it doesn't mean the message will ever be received. [...]]]></description>
			<content:encoded><![CDATA[<p>Looking up the official <a href="http://www.php.net/manual/en/function.mail.php" target="_blank">PHP</a> guide, we can see how to use the mail function, I&#8217;ll try and explain them here and give a few examples of how to add additional headers.  Let&#8217;s move along the function header from left to right, the mail() function is declared as:</p>
<pre><span>bool mail  ( string $to  , string $subject  , string $message  [, string $additional_headers  [, string $additional_parameters  ]] )</span></pre>
<p>The first thing we see is that the mail function returns a <a href="http://us2.php.net/manual/en/language.types.boolean.php" target="_blank">bool</a>, or boolean.  So every time this function is run it will either tell you whether it was successfully sent or not.  Now I want to be careful here, just because the message has been sent does not mean that the message ever made it to the users mailbox.  The message could bounce back because you entered the wrong address, it was flagged as spam, the email address doesn&#8217;t exist anymore, etc&#8230;  This is kind of like mail with the post office, just like when you put a stamp on a letter and mail it, you can say that the message was sent, but it doesn&#8217;t mean the message will ever be received.<span id="more-23"></span></p>
<pre><span>$to = "noreply@brangle.com";
$subject = 'Test Message with the Mail() command';
$message = "Lorem ipsum dolor sit amet, consectetur
     adipisicing elit, sed do eiusmod tempor incididunt ut labore
     et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud";

if (mail($to, $subject, $message)) {
   echo 'Your message was sent!';
} else {
   echo 'Your message failed to send!';
}
</span></pre>
<p>If you were to execute the above script it would send an email to noreply@brangle.com and everything would be honkey dorey.  But now what if we wanted to improve on it and add some HTML, then we&#8217;re going to need the modify the headers of the message. It&#8217;s also important to note, that I am using only \n and not \r\n this is for a variety of reasons, particularly <a href="https://mail.google.com/" target="_blank">GMail</a>.</p>
<p><strong>Using the $message variable</strong></p>
<pre><span>$headers  = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\n";
$headers .= 'To: John Doe &lt;john@example.com&gt;, Jane Doe &lt;jane.doe@example.com&gt;' . "\n";
$headers .= 'Cc: babydoe@example.com' . "\n";
$headers .= 'Bcc: brangle-archive@example.com' . "\n";
$headers .= 'From: Brangle.com Webmaster <noreply@brangle.com>' . "\n";
$headers .= 'Reply-To: webmaster@example.com' . "\n";
$headers .= 'Return-Path: webmaster@example.com' . "\n";
$headers .= 'X-Mailer: PHP/' . phpversion() ."\n";</span></pre>
<p>So I entered a whole bunch of headers above, but they&#8217;re 4 headers which you should always use, even if your only sending text, and they are: Content-Type, From, Reply-To, Return-Path header.  If you want to send your email in HTML then you absolutely need to include the Content-Type header. Now unless your sending email in some Asian languages then UTF-8 should always work for you, but please correct me in the comments if I&#8217;m wrong.  If you forget to send the envelope from address in the headers then the PHP mail() function will automatically do this for you without you even knowing, the problem is what if it grabs the wrong email address, or even a non-existant email address?!? That&#8217;s why we want to explicitly define it. You can check your PHP.ini file to see who your email is defaulted from and change it there if you&#8217;d like, but if you&#8217;re like me and don&#8217;t have access to this file then the easiest way is to create the following php file&#8230;</p>
<pre><span>&lt;? phpinfo(); &gt;</span></pre>
<p>Now visit this file on your webserver and search for sendmail_from this will tell you who all your email is &quot;sent from.&quot; Now continuing on, you can easily remove any line from the php headers that I outlined above without any PHP parsing errors.</p>
<p>if you want to send my message with a high priority, then just add the following headers:</p>
<pre><span>$headers .= 'X-Priority: 1 (Highest)' . '"\n";
$headers .= 'X-MSMail-Priority: High\n' . '"\n";
$headers .= 'Importance: High' . '"\n";</span></pre>
<p><strong>For more advanced users&#8230;</strong><br />
I think you should always log all outgoing emails, either to a text file or to your database, now these emails are just for archival purposes and can probably be flushed (deleted) within 90 to 365 days, or whenever you deem necessary. When you generate an email to send, you should log the email id number and include it in the email message that way if a user emails you back about an email they received you can go back and look at what exactly you sent.</p>
<p>If you have a flaky email server, I would recommend changing your web host, I have used <a href="http://www.dreamhost.com/r.cgi?90832" target="_blank">DreamHost</a> for several years and have never had a problem with them.  But now for the rest of you out there who refuse to change your webhost, I can think of only one recommendation.   But since this is a tutorial on how to use the mail() function, I&#8217;m only going to provide some psuedocode. If there&#8217;s enough request, I could develop this into a full fledged tutorial, but until then&#8230;</p>
<pre><span>$i = 0;
while (!mail($to, $subject, $message, $headers)) {
   sleep(10); //this will sleep for ten seconds
   $i++;
   if ($i >= 3) {
      //perform a sql insert with the following values $to, $subject, $message, $headers, time()
      //This will allow you to save the email in your database where you can create a cron job
      //to execute a query ever hour or so until your mail server is back up, and when it is sort
      //thru all those emails and send them out and flush them from the database.
   }
}
echo 'You should receive your email within 24 hours';</span></pre>
<p>One important thing to remember for all webmasters to note, if you have a flaky mail server and you feel like you have a reputable hosting provider then the issue may be the amount of emails that you are allowed to send daily.  Many hosting providers put a cap on the number of mail() function calls you can make in a day, you should definitely look into this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/sending-emails-via-php-using-the-mail-command/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

