<?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>Who... What... Where... &#187; Email</title>
	<atom:link href="http://www.yeltuor.com/articles/tag/email/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yeltuor.com</link>
	<description>General things I find interesting</description>
	<lastBuildDate>Sun, 02 Aug 2009 09:58:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP &#8211; Email Advanced</title>
		<link>http://www.yeltuor.com/articles/how-to/php-email-advanced/</link>
		<comments>http://www.yeltuor.com/articles/how-to/php-email-advanced/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:48:10 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=63</guid>
		<description><![CDATA[For a simple introduction to the mail() function see the  PHP_Advanced article. This article describes some of the more advanced features  that can be achieved through the mail function.

Assign Names to email addresses
When receiving an email you will notice that the To field often  contains a name rather than the email address [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>For a simple introduction to the <strong>mail()</strong> function see the  PHP_Advanced article. This article describes some of the more advanced features  that can be achieved through the <em>mail</em> function.</p>
<p><a title="Assign_Names_to_email_addresses" name="Assign_Names_to_email_addresses"></a></p>
<h3>Assign Names to email addresses</h3>
<p>When receiving an email you will notice that the <em>To</em> field often  contains a name rather than the email address it was send to.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?PHP<br />
mail(&#8216;bob@email.com&#8217;, &#8216;Test email&#8217;,<br />
&#8216;This is a test email&#8217;,<br />
&#8220;To: Bob Jones &lt;bob@email.com&gt;\n&#8221; .<br />
&#8220;From: Jane Jones &lt;jane@email.com&gt;\n&#8221; .<br />
&#8220;cc: Another Person &lt;another@email.com&gt;\n&#8221; .<br />
&#8220;Bcc: Yet Another &lt;more@email.com\n&gt;&#8221;);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="HTML_Emails" name="HTML_Emails"></a></p>
<h3>HTML Emails</h3>
<p>The next stage is sending HTML email messages, this allows for standard HTML  tags to be used when composing the message content. When sending a message in  HTML it must be declared that is it HTML in the header of the email, this is  done through both the <em>Content-type:</em> and <em>MIME-Version headers&#8217;:</em></p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?PHP<br />
mail(&#8216;bob@email.com&#8217;, &#8216;Test email&#8217;,<br />
&#8216;&lt;html&gt;&lt;body&gt;&lt;b&gt;Hello! World&lt;/b&gt; \n &lt;i&gt;this is a test email&lt;/i&gt;&lt;/body&gt;&lt;/html&gt;&#8217;,<br />
&#8220;MIME-Version: 1.0\n&#8221; .<br />
&#8220;Content-type: text/html; charset=iso-8859-1&#8243;);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>The <em>MIME-Version</em> (<strong>M</strong>ulitpurpose  <strong>I</strong>nternet <strong>M</strong>ail <strong>E</strong>xtensions)  header indicates that the email follows the internet standards, following that  the <em>Content-type</em> header can declare the format being used;  <em>text/html;</em> followed by the <a class="external text" title="Character-sets" rel="nofollow" href="http://www.iana.org/assignments/character-sets" target="_blank">character set</a> being used <em>charset=iso-8859-1</em></p>
<p><a title="Mixed_Format_Emails" name="Mixed_Format_Emails"></a></p>
<h3>Mixed Format Emails</h3>
<p>Although the majority of email clients support HTML email messages, there are  some that don&#8217;t. The mixed format ensures that the email clients that do support  it see the HTML formatted message, where as the ones that don&#8217;t see a plain text  version.</p>
<p>The technique involved is to actually send two versions of the message and  rely on the email client to read and understand <em>Content-Type:  multipart/alternative;</em> header which will make the client only display the  supported version.</p>
<blockquote><p>*** PHP code not fully completed yet ***</p></blockquote>
<p><a title="Emailing_Attachments" name="Emailing_Attachments"></a></p>
<h3>Emailing Attachments</h3>
<p>Emailing file Attachments work in the same way that mixed format email  messages do. The header <em>Content-Type: multipart/mixed;</em> is used and the  message split into two parts; one the message and the other the file  attachment(s).</p>
<p>This is more complicated than previous email examples, all the steps required  are explained below. The examples assume that the email details including the  file to be emailed have been submitted to the PHP page from another page.</p>
<blockquote><p>$to      = $_POST['to'];<br />
$from    = $_POST['from'];<br />
$subject = $_POST['subject'];<br />
$message = $_POST['message'];</p></blockquote>
<p><a title="Attributes_of_the_file_attachment" name="Attributes_of_the_file_attachment"></a></p>
<h4>Attributes of the file attachment</h4>
<p>The first stage is to extract the required attributed from the file that has  been passed. The file details in PHP are stored in an array named  <em>$_FILES</em> which are extracted to variables.</p>
<blockquote><p>// example: /tmp/phpfile12345 &#8211; tmp file name and loc where uploaded<br />
$file_loc  = $_FILES['fileatt']['tmp_name'];<br />
// example: text/text &#8211; will vary depending on file type<br />
$file_type = $_FILES['fileatt']['type'];<br />
// example: mywork.txt &#8211; always the name of the file<br />
$file_name = $_FILES['fileatt']['name'];</p></blockquote>
<p><a title="Extract_data_from_file_attachment" name="Extract_data_from_file_attachment"></a></p>
<h4>Extract data from file attachment</h4>
<p>The data within the file is required to be placed into a variable then used  Base64 encoding to convert (possible) binary data into text. The <a class="external text" title="PHP function is_uploaded_file" rel="nofollow" href="http://uk.php.net/is_uploaded_file" target="_blank">is_uploaded_file</a> function is  used to ensure that the file <em>was</em> in fact uploaded by an http get  command, this helps to ensure no malicious activity.</p>
<blockquote><p>if (is_uploaded_file($file_loc)) {<br />
// Read the file in &#8216;rb&#8217; read binary<br />
$file = fopen($file_loc,&#8217;rb&#8217;);<br />
$filedata = fread( $file <em>(comma)</em> filesize ($file_loc));<br />
// Base64 encode the file data<br />
$filedata = chunk_split(base64_encode($filedata));</p></blockquote>
<p>The data is now in a format that is ready to be emailed, the next stage is  producing the standard mail parameters.</p>
<p><a title="Producing_mail_function" name="Producing_mail_function"></a></p>
<h4>Producing mail function</h4>
<p>The basic mail parameters are set in the same manor, the diffrences come in  the <em>header</em> and <em>message</em> parameters</p>
<p>The header parameter contains the <em>MIME</em> version, the  <em>Content-Type: multipart/mixed;</em> declares that there will be an  attachment, finally the boundary string (containing random text) is used as a  marker to split the message into the two sections.</p>
<blockquote><p>&#8220;\nMIME-Version: 1.0\n&#8221; .<br />
&#8220;Content-Type: multipart/mixed;\n&#8221; .<br />
&#8221; boundary=\&#8221;==Multipart_Boundary_x45985365x\&#8221;";</p></blockquote>
<p>The message section starts with a declaration which MIME compatible email  clients will not show, next is the Multipart Boundary string denoting the  beginning of the first section. Following this the usual header information is  declared, following by the desired message text.</p>
<blockquote><p>&#8220;This is a multi-part message in MIME format. you should not see this\n\n&#8221; .<br />
&#8220;&#8211;==Multipart_Boundary_x45985365x\n&#8221; .<br />
&#8220;Content-Type: text/plain; charset=\&#8221;iso-8859-1\&#8221;\n&#8221; .<br />
&#8220;Content-Transfer-Encoding: 7bit\n\n&#8221; .<br />
&#8220;This is the message contents, there should be a file attached to this message&#8221;</p></blockquote>
<p>After the text of the message, the next part is the message attachment which  follows the same format as above.</p>
<blockquote><p>&#8220;&#8211;==Multipart_Boundary_x45985365x\n&#8221; .<br />
&#8220;Content-Type: {$file_type};\n&#8221; .<br />
&#8221; name=\&#8221;{$file_} \n&#8221; .<br />
&#8220;Content-Disposition: attachment;\n&#8221; .<br />
&#8221; filename=\&#8221;{$file_name}\&#8221;\n&#8221; .<br />
&#8220;Content-Transfer-Encoding: base64\n\n&#8221; .<br />
$filedata . &#8220;\n\n&#8221; .<br />
&#8220;&#8211;==Multipart_Boundary_x45985365x&#8211;\n&#8221;;</p></blockquote>
<p>Then message should always have the message boundary string followed by  <strong>&#8211;</strong> to signify the end.</p>
<p>To see the fully working source code, please see <a class="external text" title="PHP Email Code" rel="nofollow" href="http://www.yeltuor.com/s/files/PHP_Mail.rar">here</a></p>
<p>For more information on different MIME types see <a class="external text" title="MIMe types" rel="nofollow" href="http://www.ietf.org/rfc/rfc2045.txt" target="_blank">here</a><br />
Many thanks to the tutorials where this information came from, <a class="external text" title="w3schools" rel="nofollow" href="http://www.w3schools.com/php/default.asp" target="_blank">W3Schools</a> &amp; <a class="external text" title="PHP Net" rel="nofollow" href="http://uk.php.net/manual/en/introduction.php" target="_blank">PHP</a> &amp; <a class="external text" title="Sitepoint" rel="nofollow" href="http://www.sitepoint.com/article/advanced-email-php" target="_blank">sitepoint</a></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/email/" title="Email" rel="tag">Email</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</a>, <a href="http://www.yeltuor.com/articles/tag/web/" title="Web" rel="tag">Web</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/how-to/php-email-advanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Advanced</title>
		<link>http://www.yeltuor.com/articles/script/php-advanced/</link>
		<comments>http://www.yeltuor.com/articles/script/php-advanced/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:37:40 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Sessions]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=58</guid>
		<description><![CDATA[This article will guide you through some of the more complex features of PHP. a more basic introduction article is also available.

User Data
When browsing the internet you will come across some websites that store information about you or your visits to the site. There are two main methods of storing this data, these are through [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>This article will guide you through some of the more complex features of PHP. a more basic introduction article is also available.</p>
<p><a title="User_Data" name="User_Data"></a></p>
<h3>User Data</h3>
<p>When browsing the internet you will come across some websites that store information about you or your visits to the site. There are two main methods of storing this data, these are through <strong>Cookies</strong> and <strong>Sessions</strong>.</p>
<p><a title="Cookies" name="Cookies"></a></p>
<h4>Cookies</h4>
<p>Cookies are the older of the two methods, it consists of a client side file that the browser writes to. With PHP you can create new cookies and also retrieve existing values, examples of both are below</p>
<p>To create new cookies the <strong>setcookie</strong> function is used, the main parameters are:</p>
<ul>
<li>Name &#8211; The Name of the cookie</li>
<li>Value &#8211; Data that the cookie is to store</li>
<li>Expire &#8211; Time &amp; Date when the cookie will expire</li>
</ul>
<p>When creating cookies in PHP you must call the setcookie function <strong>before</strong> any HTML tags are used in the page, an example is shown below:</p>
<blockquote><p>
	 &lt;?PHP<br />
	setcookie(&#8220;yeltuor&#8221;, &#8220;MyValue&#8221;, time()+86400);<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	The cookie named yeltuor has been &#8217;set&#8217;<br />
	It will expire 24 Hours (86400 seconds) from now.<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>With the cookie now created, the next stage is to read back the values stored in it. The values in the cookie can be called in the same way variables are:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	//Check whether the cookie named yeltuor is set<br />
	if (isset($_COOKIE["yeltuor"]))<br />
	echo &#8220;Welcome back &#8221; . $_COOKIE["yeltuor"] . &#8220;&lt;br&gt;&#8221;;<br />
	else<br />
	echo &#8220;Welcome newbe &lt;br&gt;&#8221;;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Sessions" name="Sessions"></a></p>
<h4>Sessions</h4>
<p>A Sessions is a server side file that is written to, again PHP support the creation and retrieval of session data. When a session is created a unique <em>session ID</em> is used to reference it, this is all taken care within the underlying PHP code.</p>
<p>The advantage of using sessions is that the user can&#8217;t view or edit the data within them; unlike the client side cookie&#8217;s. Before using sessions on a PHP page, the <em>session_start();</em> function must be called, after this a session can be created, however like the cookie functions all session calls must be done before any HTML code on the page:</p>
<p>Below is an example of setting up a server side session:</p>
<blockquote><p>
	 &lt;?PHP<br />
	//start session<br />
	session_start();<br />
	//if session variable total doesn&#8217;t exist create one<br />
	if(!isset($_SESSION['total']))<br />
	$_SESSION['total'] = &#8216;0&#8242;;<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	The session variable total set to 0, unless it already existed.<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>To reference existing session variables and dispose of variables that are no longer required, the following code can be used:</p>
<blockquote><p>
	 &lt;?PHP<br />
	//start session<br />
	session_start();<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	echo &#8220;The total is &#8221; . $_SESSION['total'];<br />
	//Finished with session variable, disposing of it<br />
	unset($_SESSION['total']);<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>For the security minded individuals, a list of more advanced session functions that are available can be seen <a class="external text" title="PHP Net" rel="nofollow" href="http://uk.php.net/session" target="_blank">here</a>.</p>
<p><a title="Files" name="Files"></a></p>
<h3>Files</h3>
<p>PHP supports various file related operations, some examples are below:</p>
<p>Opening a file is done with the <strong>fopen( );</strong> function, the first parameter is the filename, the second is the mode to open the file in:</p>
<table style="width: 70%;" border="1">
<tbody>
<tr>
<th>Mode</th>
<th>Description</th>
</tr>
<tr>
<td>r</td>
<td>Read Only, pointer at the beginning of the file</td>
</tr>
<tr>
<td>r+</td>
<td>Read and Write, pointer at the beginning of the file</td>
</tr>
<tr>
<td>w</td>
<td>Write Only, existing file will be truncated or new file will be created.</td>
</tr>
<tr>
<td>w+</td>
<td>Write and Read, existing file will be truncated or new file will be created.</td>
</tr>
<tr>
<td>a</td>
<td>Write Only, Places pointer at end of the file or new file will be created.</td>
</tr>
<tr>
<td>a+</td>
<td>Write and Read, Places pointer at end of the file or new file will be created.</td>
</tr>
<tr>
<td>x</td>
<td>Write Only, If the file already exists the <em>fopen( )</em> function will return <em>FALSE</em>, or a new file will be created.</td>
</tr>
<tr>
<td>x</td>
<td>Write and Read, If the file already exists the <em>fopen( )</em> function will return <em>FALSE</em>, or a new file will be created.</td>
</tr>
</tbody>
</table>
<p>Some examples of using the fopen( ) function are below:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	//Open file hello.txt, in the case of an error exit<br />
	$f=fopen(&#8220;hello.txt&#8221;,&#8221;r&#8221;) or exit(&#8220;Cant open file&#8221;);<br />
	//if reached the end of the file<br />
	if (feof($f))<br />
	echo &#8220;End of file&#8221;;<br />
	//Loop through file 1 char at a time, echo value<br />
	while (!feof($f))<br />
	{<br />
	$x=fgetc($f);<br />
	echo $x;<br />
	}<br />
	//Close file handle when finished<br />
	fclose($f) ;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>This function below accepts a parameter of a filename and location and returns the file data as a variable called <strong>contents</strong>:</p>
<blockquote><p>
	 function fileContents($filename){<br />
	// get contents of a file into a string<br />
	$f = fopen($filename, &#8220;r&#8221;);<br />
	$contents = fread($f, filesize($filename)) ;<br />
	fclose ($f);<br />
	return $contents;<br />
	}
</p></blockquote>
<p><a title="Functions" name="Functions"></a></p>
<h3>Functions</h3>
<p>Functions are used in the same way that procedures are used in other programming languages, they are used to aid with the re-use of code. examples of using functions can be seen below:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	function echoHelloWorld($name)<br />
	{<br />
	echo &#8220;Hello World my name is &#8221; . $name;<br />
	}<br />
	//Using the created function<br />
	echoHelloworld(&#8220;Borris&#8221;)<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>Functions can also return values as an output by the <strong>return</strong> command from within the function.</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	function addnum($num1,$num2)<br />
	{<br />
	$tot = $num1 + $num2;<br />
	return $tot;<br />
	}<br />
	//Using the function addnum<br />
	echo &#8220;1 + 2 = &#8221; . addNum(1,2);<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Require" name="Require"></a></p>
<h3>Require</h3>
<p>The require function works in the same way as a function, except that it is used to call another file which is inserted into the current page. This is useful when a <em>header</em> is being used;</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	require(&#8220;header.php&#8221;)<br />
	?&gt;<br />
	&lt;h3&gt;More text here.&lt;/h3&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Email" name="Email"></a></p>
<h3>Email</h3>
<p>PHP has built in email support by using the <strong>mail()</strong> function, it requires the usual email parameters;</p>
<table style="width: 70%;" border="1">
<tbody>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
<tr>
<td>to</td>
<td>The recipients address in the form of abc@xyz.com</td>
</tr>
<tr>
<td>subject</td>
<td>Subject of the email</td>
</tr>
<tr>
<td>message</td>
<td>Content of the email; use the <em>\n</em> command for a new line</td>
</tr>
<tr>
<td>headers</td>
<td>An optional parameter which may contain and Cc&#8217;s of Bcc&#8217;s</td>
</tr>
<tr>
<td>parameters</td>
<td>Additional parameters that the sendmail program accepts i.e. From</td>
</tr>
</tbody>
</table>
<p>An example of the code for a mail function with error checking is shown below, to send emails to multiple recipients, a comma: <em>,</em> should be used as a delimiter.</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	$to = &#8220;bob@email.com&#8221;;<br />
	$subject = &#8220;Test email&#8221;;<br />
	$message = &#8220;Hello! World \n this is a test email&#8221;;<br />
	$from = &#8220;tom@email.com&#8221;;<br />
	$headers = &#8220;From: $from&#8221;;<br />
	if (mail($to,$subject,$message,$headers))<br />
	echo &#8220;Mail Sent sucessfully&#8221;;<br />
	else<br />
	echo &#8220;Failed to Send Mail&#8221;;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>This is very basic example of using emailing, however for a more complete guide to PHP emailing please see PHP Mail article.</p></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/cookies/" title="Cookies" rel="tag">Cookies</a>, <a href="http://www.yeltuor.com/articles/tag/email/" title="Email" rel="tag">Email</a>, <a href="http://www.yeltuor.com/articles/tag/files/" title="Files" rel="tag">Files</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</a>, <a href="http://www.yeltuor.com/articles/tag/sessions/" title="Sessions" rel="tag">Sessions</a>, <a href="http://www.yeltuor.com/articles/tag/web/" title="Web" rel="tag">Web</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/php-advanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
