<?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>Carbonized Blog &#187; PHP</title>
	<atom:link href="http://carbonize.co.uk/wp/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://carbonize.co.uk/wp</link>
	<description>Just a bunch of stuff</description>
	<lastBuildDate>Tue, 06 Mar 2012 19:25:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Random Character Generation in PHP and JavaScript</title>
		<link>http://carbonize.co.uk/wp/2012/02/08/random-character-generation-in-php/</link>
		<comments>http://carbonize.co.uk/wp/2012/02/08/random-character-generation-in-php/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 16:29:07 +0000</pubDate>
		<dc:creator>Carbonize</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://carbonize.co.uk/wp/?p=486</guid>
		<description><![CDATA[<a href="http://carbonize.co.uk/wp/2012/02/08/random-character-generation-in-php/" title="Random Character Generation in PHP and JavaScript"></a>I wrote this function in response to someone else&#8217;s attempt on a forum I was asked to join. It basically generates a string of random letters and numbers with the letters being in both upper and lower case. It is &#8230;<p class="read-more"><a href="http://carbonize.co.uk/wp/2012/02/08/random-character-generation-in-php/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://carbonize.co.uk/wp/2012/02/08/random-character-generation-in-php/" title="Random Character Generation in PHP and JavaScript"></a><p>I wrote this function in response to someone else&#8217;s attempt on a forum I was asked to join. It basically generates a string of random letters and numbers with the letters being in both upper and lower case. It is easy to edit it to only use upper or lower case letters or even add symbols as well. I initially wrote it in PHP and then rewrote it in JavaScript as well.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> randomString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$strLen</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Create our character arrays</span>
  <span style="color: #000088;">$chrs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'z'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Z'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 
  <span style="color: #666666; font-style: italic;">// Just to make the output even more random</span>
  <span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chrs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 
  <span style="color: #666666; font-style: italic;">// Create a holder for our string</span>
  <span style="color: #000088;">$randStr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
 
  <span style="color: #666666; font-style: italic;">// Now loop through the desired number of characters for our string</span>
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$strLen</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$randStr</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$chrs</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chrs</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$randStr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Using it is simply a case of calling it and specifying how long to make the string otherwise it uses the default length of 32 characters.</p>
<p>echo randomString(12);</p>
<p>To also make it use symbols you just change the array_merge to<br />
<span id="more-486"></span><br />
<code><br />
// If we want letters, numbers and symbols<br />
$chrs = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9), array('!','£','$','%','^','&#038;','*','(',')','-','=','+','@','#','~','?'));<br />
</code></p>
<p>Now for the JavaScript version. JavaScript has neither a range() function nor an easy way to shuffle an array so the code here is a little longer.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> randomString<span style="color: #009900;">&#40;</span>len<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// Just an array of the characters we want in our random string</span>
  <span style="color: #003366; font-weight: bold;">var</span> chrs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'b'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'c'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'d'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'e'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'f'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'g'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'h'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'i'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'j'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'k'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'l'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'m'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'n'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'o'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'p'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'q'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'r'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'s'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'t'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'u'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'v'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'w'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'x'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'y'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'z'</span><span style="color: #339933;">,</span>
              <span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'B'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'C'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'D'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'E'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'F'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'G'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'H'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'I'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'J'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'K'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'L'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'M'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'N'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'O'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'P'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Q'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'R'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'S'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'T'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'U'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'V'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'W'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'X'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Y'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Z'</span><span style="color: #339933;">,</span>
              <span style="color: #3366CC;">'0'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'2'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'3'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'4'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'5'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'6'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'7'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'8'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'9'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Check that a length has been supplied and if not default to 32</span>
  len <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>isNaN<span style="color: #009900;">&#40;</span>len<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">32</span> <span style="color: #339933;">:</span> len<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// The following section shuffles the array just to further randomise the output</span>
  <span style="color: #003366; font-weight: bold;">var</span> tmp<span style="color: #339933;">,</span> current<span style="color: #339933;">,</span> top <span style="color: #339933;">=</span> chrs.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> 
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>top<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>top<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span> 
      current <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>top <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
      tmp <span style="color: #339933;">=</span> chrs<span style="color: #009900;">&#91;</span>current<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
      chrs<span style="color: #009900;">&#91;</span>current<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> chrs<span style="color: #009900;">&#91;</span>top<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
      chrs<span style="color: #009900;">&#91;</span>top<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> tmp<span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Just a holder for our random string</span>
  <span style="color: #003366; font-weight: bold;">var</span> randomStr <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Loop through the required number of characters grabbing one at random from the array each time</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>len<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    randomStr <span style="color: #339933;">=</span> randomStr <span style="color: #339933;">+</span> chrs<span style="color: #009900;">&#91;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>chrs.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Return our random string</span>
  <span style="color: #000066; font-weight: bold;">return</span> randomStr<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://carbonize.co.uk/wp/2012/02/08/random-character-generation-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending email via SMTP using PHP</title>
		<link>http://carbonize.co.uk/wp/2011/10/24/sending-email-via-smtp-using-php/</link>
		<comments>http://carbonize.co.uk/wp/2011/10/24/sending-email-via-smtp-using-php/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 11:08:51 +0000</pubDate>
		<dc:creator>Carbonize</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://carbonize.co.uk/wp/?p=472</guid>
		<description><![CDATA[<a href="http://carbonize.co.uk/wp/2011/10/24/sending-email-via-smtp-using-php/" title="Sending email via SMTP using PHP"></a>A couple of my users contacted me to say that their host had disabled sendmail and required any scripts they use to now use SMTP to send emails. This resulted in me quickly reading all I could about SMTP and &#8230;<p class="read-more"><a href="http://carbonize.co.uk/wp/2011/10/24/sending-email-via-smtp-using-php/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://carbonize.co.uk/wp/2011/10/24/sending-email-via-smtp-using-php/" title="Sending email via SMTP using PHP"></a><p>A couple of my users contacted me to say that their host had disabled sendmail and required any scripts they use to now use SMTP to send emails. This resulted in me quickly reading all I could about SMTP and the result is this slightly rough script I am sharing. It&#8217;s pretty self explanatory. Put your SMTP server details in the $mailCfg array. Next simply call the smtpMail function which uses the same variables as the standard PHP mail() function but with two additional variables. The additional variables are the email address we are sending from and $mailCfg. I could of put $mailCfg as a global but this way you can include the script in a different script and store the required information where ever you want. The from address is important as most SMTP servers will reject the message if it&#8217;s not included.</p>
<p>Just remember the code is far from perfect and was created to do a simple job.</p>
<p>It also has one nice extra function you might find useful&#8230; <span id="more-472"></span></p>
<p>The directMail function will look up the recipients SMTP server and try to deliver the email directly to it thereby bypassing the need for you to have access to an SMTP server to send through. Just remember your host may frown upon the use of this function.</p>
<p>[sourcecode language='php']<?php<br />
/*<br />
 * SMTP Email Sending<br />
 * By Stewart Souter<br />
 * Date Created: Thurs, 11 August 2011 17:15:37 GMT<br />
 * Last Updated: Fri, 19 August 2011 10:54:35 GMT<br />
 * email: webmaster@carbonize.co.uk<br />
 *<br />
 * By using this script you are agreeing to leave this<br />
 * comment and agreement in place and untouched. If you<br />
 * use any part of this code you must make it clear where<br />
 * it came from and give credit where it is due.<br />
 */</p>
<p>$mailCfg['Server']    = '';    // Servername<br />
$mailCfg['User']      = '';    // SMTP username if needed<br />
$mailCfg['Pass']      = '';    // SMTP Password if needed<br />
$mailCfg['Port']      = 25;    // SMTP server port. 25 is the usual and 465 if using SSL<br />
$mailCfg['popServer'] = '';    // Name of the pop server. Leave empty if POP Auth not required<br />
$mailCfg['popPort']   = 110;   // Port for the pop server. 110 is the usual and 995 if using SSL<br />
$mailCfg['SSL']       = 0;     // Does your SMTP server need you to use SSL or TLS? 0 = no, 1 = SSL, 2 = TLS</p>
<p>// This function delivers the email directly to the recipients mail server so bypassing the need for your own<br />
function directMail($mailTo, $mailSubject, $mailMsg, $mailHeaders = '', $mailFrom = '', $mailCfg)<br />
{<br />
  if(empty($mailFrom))<br />
  {<br />
    return false; // No from address == no sending<br />
  }<br />
  $mailParts = explode('@', $mailTo);  // Seperate the parts of the email address<br />
  @getmxrr($mailParts[1], $mxHosts, $mxWeight); // Get the MX records for the emails domain<br />
  for($i=0;$i<count($mxHosts);$i++) // Put the records and weights into an array<br />
  {<br />
      $mxServers[$mxHosts[$i]] = $mxWeight[$i];<br />
  }<br />
  asort($mxServers); // Sort the array so they are in weighted order<br />
  foreach($mxServers as $key => $value)<br />
  {<br />
    $mailCfg['Server'] = $key; // Set the SMTP server to the current MX record<br />
    if(smtpMail($mailTo, $mailSubject, $mailMsg, $mailHeaders, $mailFrom, $mailCfg)) // Send the email using the MX server<br />
    {<br />
      return true;  // The email was successfully sent<br />
    }<br />
  }<br />
  return false;  // Houston we have a problem<br />
}</p>
<p>// This function connects to the SMTP server and does the AUTH if needed. Can also do a POP login if server requires that.<br />
function smtpMail($mailTo, $mailSubject, $mailMsg, $mailHeaders = &#8221;, $mailFrom = &#8221;, $mailCfg )<br />
{<br />
  if(empty($mailFrom))<br />
  {<br />
    return false; // No from address == no sending<br />
  }<br />
  $timeout = &#8217;30&#8242;; // How long to keep trying to connect<br />
  $localhost = &#8216;localhost&#8217;; // How to identify ourselves<br />
  $logArray = array(); // For storing the replies</p>
<p>  /* * * * POP Login if required * * */ </p>
<p>  if(!empty($mailCfg['popServer'])) // Can&#8217;t really do POP Auth without a server<br />
  {<br />
    $ssl = ($mailCfg['SSL'] != 0) ? (($mailCfg['SSL'] == 1) ? &#8216;ssl://&#8217; : &#8216;tls://&#8217;) : &#8221;; // If SSL or TLS add it<br />
    $popConnect = @fsockopen($ssl.$mailCfg['popServer'], $mailCfg['popPort'], $errno, $errstr, $timeout); // Connect<br />
    if(!$popConnect) // If we fail to connect&#8230;<br />
    {<br />
      $logArray['POPconnect'] = $errstr . &#8216;(&#8216; . $errno . &#8216;)&#8217;; // Log the given reason&#8230;<br />
      logMailError($logArray); // And output to the log file.<br />
      return false;<br />
    }<br />
    else<br />
    {<br />
      $logArray['POPconnect'] = @fgets($popConnect, 515)); // POP servers only return single line replies. Or should.<br />
      if(!mailPackets(&#8216;AUTH LOGIN&#8217;, $popConnect, &#8216;SMTPauth&#8217;)) //Request Auth Login<br />
      {<br />
        return false;<br />
      }<br />
      if(!mailPackets(&#8216;USER &#8216; . $smtpUser, $popConnect, &#8216;POPuser&#8217;)) // Send username. POP is plaintext<br />
      {<br />
        return false;<br />
      }<br />
      if(!mailPackets(&#8216;PASS &#8216; . $smtpPass, $popConnect, &#8216;POPpass&#8217;)) // Send password, again in plaintext<br />
      {<br />
        return false;<br />
      }<br />
      if(!mailPackets(&#8216;QUIT&#8217;, $popConnect, &#8216;POPquit&#8217;)) // Say bye to the server<br />
      {<br />
        return false;<br />
      }<br />
      fclose($popConnect); // Close connection<br />
    }<br />
  }</p>
<p>  /* * * * End of POP Login * * * * */</p>
<p>  /* * * * Start of SMTP stuff * * * */</p>
<p>  $ssl = ($mailCfg['SSL'] != 0) ? (($mailCfg['SSL'] == 1) ? &#8216;ssl://&#8217; : &#8216;tls://&#8217;) : &#8221;; // Set the encryption if needed<br />
  $smtpConnect = @fsockopen($ssl.$mailCfg['Server'], $mailCfg['Port'], $errno, $errstr, $timeout); // Connect<br />
  if(!$smtpConnect) // If we fail to connect&#8230;<br />
  {<br />
    $logArray['SMTPconnect'] = $errstr . &#8216;(&#8216; . $errno . &#8216;)&#8217;; // Add the reason to the log&#8230;<br />
    logMailError($logArray); // Then output the log<br />
    return false;<br />
  }<br />
  else<br />
  {<br />
    $cnectKey = 0; // A counter for when we receive multiple lines in reply<br />
    do<br />
    {<br />
      $smtpResponse = @fgets($smtpConnect, 515); // Get the reply<br />
      $cnectKey++; // Increment the counter<br />
      $logArray['SMTPconnect' . $cnectKey] = $smtpResponse; // Log the response<br />
      $responseCode = substr($smtpResponse, 0, 3); // Grab the response code from start of the response<br />
      // If we get an error terminate the connection and log the results so far<br />
      if($responseCode >= 400)<br />
      {<br />
        logMailError($logArray, $smtpConnect);<br />
        return false;<br />
      }<br />
    }<br />
    while((strlen($smtpResponse) > 3) &#038;&#038; (strpos($smtpResponse, &#8216; &#8216;) != 3)); // Loop until we get told it&#8217;s the last line<br />
      $ehlo = mailPackets(&#8216;EHLO &#8216; . $localhost, $smtpConnect, $logArray, &#8216;SMTPehlo&#8217;); // Let&#8217;s try using EHLO first<br />
      if($ehlo != 250) // Server said it didn&#8217;t like EHLO so drop back to HELO<br />
      {<br />
        if(!mailPackets(&#8216;HELO &#8216; . $localhost, $smtpConnect, $logArray, &#8216;SMTPhelo&#8217;)) // Send HELO. No EHLO means server doesn&#8217;t support AUTH<br />
        {<br />
          return false;<br />
        }<br />
      }<br />
      if(!empty($mailCfg['User']) &#038;&#038; ($ehlo == 250)) // We have a username and server supports EHLO so send login credentials<br />
      {<br />
        if(!mailPackets(&#8216;AUTH LOGIN&#8217;, $smtpConnect, $logArray, &#8216;SMTPauth&#8217;)) // Request Auth Login<br />
        {<br />
          return false;<br />
        }<br />
        if(!mailPackets(base64_encode($mailCfg['User']), $smtpConnect, $logArray, &#8216;SMTPuser&#8217;)) // Send username<br />
        {<br />
          return false;<br />
        }<br />
        if(!mailPackets(base64_encode($mailCfg['Pass']), $smtpConnect, $logArray, &#8216;SMTPpass&#8217;)) // Send password<br />
        {<br />
          return false;<br />
        }<br />
      }<br />
      if(!mailPackets(&#8216;MAIL FROM:<' . $mailFrom . '>&#8216;, $smtpConnect, $logArray, &#8216;SMTPfrom&#8217;)) // Email From<br />
      {<br />
        return false;<br />
      }<br />
      if(!mailPackets(&#8216;RCPT TO:<' . $mailTo . '>&#8216;, $smtpConnect, $logArray, &#8216;SMTPrcpt&#8217;)) // Email To<br />
      {<br />
        return false;<br />
      }<br />
      if(!mailPackets(&#8216;DATA&#8217;, $smtpConnect, $logArray, &#8216;SMTPmsg&#8217;)) // We are about to send the message<br />
      {<br />
        return false;<br />
      }<br />
      // First lets make sure both the message and additional headers do not contain anythign that might be seen as end of message marker<br />
      $mailMsg = preg_replace(array(&#8220;/(?<!\r)\n/", "/\r(?!\n)/", "/\r\n\./"), array("\r\n", "\r\n", "\r\n.."), $mailMsg);<br />
      $mailHeaders = (!empty($mailHeaders)) ? "\r\n" . preg_replace(array("/(?<!\r)\n/", "/\r(?!\n)/", "/\r\n\./"), array("\r\n", "\r\n", "\r\n.."), $mailHeaders) : '';<br />
      // Create the default headers, attach any additonal headers<br />
      $mailHeaders = "To: <".$mailCfg['To'].">\r\nFrom: <".$mailCfg['From'].">\r\nSubject: &#8220;.$mailCfg['Subject'].&#8221;\r\nDate: &#8221; . gmdate(&#8216;D, d M Y H:i:s&#8217;) . &#8221; -0000&#8243;.$mailHeaders;<br />
      if(!mailPackets($mailHeaders.&#8221;\r\n\r\n&#8221;.$mailMsg.&#8221;\r\n.&#8221;, $smtpConnect, $logArray, &#8216;SMTPbody&#8217;)) // The message<br />
      {<br />
        return false;<br />
      }<br />
      mailPackets(&#8216;QUIT&#8217;, $smtpConnect, $logArray, &#8216;SMTPquit&#8217;); // Say Bye to SMTP server<br />
      fclose($smtpConnect); // Be nice and close the connection<br />
      return true; // Return the fact we sent the message<br />
  }<br />
}</p>
<p>// This function sends the actual packets then logs the reponses and parses the reponse code<br />
function mailPackets($sendStr,$mailConnect,&#038;$logArray,$logName = &#8221;)<br />
{<br />
  $newLine = &#8220;\r\n&#8221;; // LEAVE THIS ALONE<br />
  $keyCount = 0;  // Just an incremental counter for when we get more than a single line response<br />
  @fputs($mailConnect,$sendStr . $newLine); // Send the packet<br />
  do // Start grabbing the responses until we either get a terminal error or told we are at the end<br />
  {<br />
    $mailResponse = @fgets($mailConnect, 515); // Receive the response<br />
    $keyCount++; // Incrememnt the key count<br />
    $logArray[$logName . $keyCount] = $mailResponse; // Put the response in to the log array<br />
    $responseCode = substr($smtpResponse, 0, 3); // Grab the response code from start of the response<br />
    // Check for error codes except on ehlo, auth, and user details as they are not always fatal<br />
    if((($logName != &#8216;SMTPauth&#8217;) &#038;&#038; ($logName != &#8216;SMTPuser&#8217;) &#038;&#038; ($logName != &#8216;SMTPehlo&#8217;) &#038;&#038; ($logName != &#8216;SMTPpass&#8217;)) &#038;&#038; ($responseCode >= 400))<br />
    {<br />
       logMailError($logArray,$mailConnect);<br />
       return false;<br />
    }<br />
    elseif((substr($responseCode, 0, 1) == 4) || ($responseCode >= 521) &#038;&#038; ($logName != &#8216;SMTPehlo&#8217;))<br />
    {<br />
       logMailError($logArray,$mailConnect);<br />
       return false;<br />
    }<br />
  }<br />
  while((strlen($mailResponse) > 3) &#038;&#038; (strpos($mailResponse, &#8216; &#8216;) != 3)); // Loop until we get the end response<br />
  return $responseCode; // Return the response code<br />
}</p>
<p>function logMailError(&#038;$logArray, $mailServer = false)<br />
{<br />
  if($mailServer)<br />
  {<br />
    fclose($mailServer); // Be nice and close the connection<br />
  }<br />
  $fd = @fopen (&#8216;smtplog.txt&#8217;, &#8216;a&#8217;); // open the log file<br />
  $mailResults = print_r($logArray, true); // Create a nice printable version of logArray<br />
  @fwrite($fd,$mailResults); // Write the log<br />
  @fclose ($fd); // Close the file<br />
}</p>
<p>?>[/sourcecode]</p>
]]></content:encoded>
			<wfw:commentRss>http://carbonize.co.uk/wp/2011/10/24/sending-email-via-smtp-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gzipping your pages to save bandwidth</title>
		<link>http://carbonize.co.uk/wp/2009/02/13/gzipping-your-pages-to-save-bandwidth/</link>
		<comments>http://carbonize.co.uk/wp/2009/02/13/gzipping-your-pages-to-save-bandwidth/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 00:26:11 +0000</pubDate>
		<dc:creator>Carbonize</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.carbonize.co.uk/Blog/archives/54-guid.html</guid>
		<description><![CDATA[<a href="http://carbonize.co.uk/wp/2009/02/13/gzipping-your-pages-to-save-bandwidth/" title="Gzipping your pages to save bandwidth"></a>Ok I&#8217;ve been playing with Javascript frameworks like Jquery and Prototype. Now by default Jquery is 54KB and Prototype is 130KB (at the time of writing this). As you can see these are not small files. Now this is where &#8230;<p class="read-more"><a href="http://carbonize.co.uk/wp/2009/02/13/gzipping-your-pages-to-save-bandwidth/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://carbonize.co.uk/wp/2009/02/13/gzipping-your-pages-to-save-bandwidth/" title="Gzipping your pages to save bandwidth"></a><p>Ok I&#8217;ve been playing with Javascript frameworks like Jquery and Prototype. Now by default Jquery is 54KB and Prototype is 130KB (at the time of writing this). As you can see these are not small files. Now this is where gzip comes in. An easy way to describe gzip is that your server zips up the file before sending it to the web browser and the web browser then unzips it. Anyway by gzipping these two files using some PHP I have got Jquery down to 19KB and prototype down to 30KB!!!!</p>
<p>So we have Jquery:<br />
Original Size: 54 KB<br />
Gzipped Size: 19 KB<br />
Data Savings: 64.81%</p>
<p>and prototype:<br />
Original Size: 131 KB<br />
Gzipped Size: 30 KB<br />
Data Savings: 77.1%</p>
<p><span style="font-size:1">All testing done using <a  class="bb-url" href="http://whatsmyip.org/mod_gzip_test/" target="_blank">mod_zip test</a>.</span></p>
<p>Anyway on to the code.<br />
<span id="more-5"></span><br />
Ok to send something gzipped we need to use PHP&#8217;s <a  class="bb-url" href="http://php.net/ob_start" target="_blank">buffer</a> and so one of the first things we need to do is turn it on.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ob_gzhandler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We would then just put our Javascript (or normal HTML) after the ?&gt;</p>
<p>The ob_gzhandler is a function in PHP that checks if a browser supports gzip compression or not and if it doesn&#8217;t then PHP will not gzip the buffer.</p>
<p>Finally we tell PHP that we are done with the buffer and we want it to <a  class="bb-url" href="http://php.net/ob_end_flush" target="_blank">flush</a> the buffer. Flush just means it sends the contents of the buffer to the browser and then closes the buffering.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">ob_end_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And that&#8217;s it. So in a nutshell.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ob_gzhandler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!-- Your Javascript or HTML here (or even PHP code) --&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">ob_end_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://carbonize.co.uk/wp/2009/02/13/gzipping-your-pages-to-save-bandwidth/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using The PHP mail Function</title>
		<link>http://carbonize.co.uk/wp/2008/01/29/using-the-php-mail-function/</link>
		<comments>http://carbonize.co.uk/wp/2008/01/29/using-the-php-mail-function/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 13:48:42 +0000</pubDate>
		<dc:creator>Carbonize</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.carbonize.co.uk/Blog/archives/35-guid.html</guid>
		<description><![CDATA[<a href="http://carbonize.co.uk/wp/2008/01/29/using-the-php-mail-function/" title="Using The PHP mail Function"></a>Ok been a while since I posted anything so thought I would post my tutorial on using the PHP mail() function. I have basically copied my tutorial word for word from the Lazarus forum. This is a brief (or not) &#8230;<p class="read-more"><a href="http://carbonize.co.uk/wp/2008/01/29/using-the-php-mail-function/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://carbonize.co.uk/wp/2008/01/29/using-the-php-mail-function/" title="Using The PHP mail Function"></a><p>Ok been a while since I posted anything so thought I would post my tutorial on using the <strong>PHP mail()</strong> function. I have basically copied my tutorial word for word from <a  href="http://carbonize.co.uk/Lazarus/Forum/index.php?topic=1444.0">the Lazarus forum</a>.</p>
<p>This is a brief (or not) tutorial on using the <strong>PHP mail()</strong> function to send emails from your scripts.</p>
<p>bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )</p>
<p>Let&#8217;s break that down. The bool means that the <strong>PHP mail</strong> function returns either <strong>true </strong>or <strong>false</strong> depending on if it was successful or not. The rest is pretty self explanatory. string $to is where we put the email address we want to send to. string $subject is where we put the emails subject and string $message is where we put our message. The string part just means that this variable will be treated as a string when used. We will cover $additional_headers a bit later and won&#8217;t even touch on $additional_parameters.</p>
<p>Ok so we want to send an email to <strong>Bill Gates</strong> with the subject <strong>Windows Linux</strong> and our message is <strong>When is it coming out?</strong>. The function we would use looks like this</p>
<div class="bb-code">&lt;?php<br />
mail(&#8216;bill.gates@microsoft.com&#8217;, &#8216;Windows Linux&#8217;, &#8216;When is it coming out?&#8217;);<br />
?&gt;</div>
<p><span id="more-24"></span><br />
This would suit our needs but there are some things you need to know. Firstly this email will be sent as plain text so any HTML tags in the message would be displayed in the message. Second this email would have our servers address as the from address. Before I continue I would like to just briefly touch on the point that you can send to multiple address by putting them in the to section separated by a comma like &#8216;bill.gates@microsoft.com,some@address.com&#8217;. So now let us make it appear to come from our email address <strong>email@address.com</strong>.</p>
<div class="bb-code">&lt;?php<br />
mail(&#8216;bill.gates@microsoft.com&#8217;, &#8216;Windows Linux&#8217;, &#8216;When is it coming out?&#8217;, &#8216;From: email@address.com&#8217;);<br />
?&gt;</div>
<p>As you can see we added a header to our email. The header is From and says what address the email was sent from. We could also use the format From: &quot;Our Name&quot; &lt;email@address.com&gt; so that it also has our name. This is fine and would suit most of your email sending needs but what if you want to send HTML? Then we need some more headers. They are MIME-Version and Content-type. So now our function looks like:</p>
<div class="bb-code">&lt;?php<br />
mail(&#8216;bill.gates@microsoft.com&#8217;, &#8216;Windows Linux&#8217;, &#8216;<b>When</b> is it coming out?&#8217;, &quot;From: email@address.com\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1&quot;);<br />
?&gt;</div>
<p>You will see I switched from using single quotes to double for the headers. This is because we need to separate the different headers with a new line marker (\n) and this does not work when using single quotes. I won&#8217;t say a lot about what the MIME type means. You can read up on that <a  href="http://en.wikipedia.org/wiki/MIME">HERE</a>. The Content-type says that the email is sent as text but is to be treated as HTML. Part of the Content-type is charset. This tells the email reader what character set to use when displaying the email. You will only have to change this if you are sending your email in a language other than English. Now when our recipient reads the email the word <strong>When</strong> will be bold.</p>
<p>You can also use the headers section to send carbon copies and blind carbon copies of the email to people by using CC: emailaddress and BCC: emailaddress respectively. You still need a main email address to send to though for these to work.</p>
<p>Now some might argue that you should use <strong>\r\n</strong> for separating the headers but in my experience just <strong>\n</strong> on it&#8217;s own is fine and will be understood by all email clients where as <strong>\r\n</strong> may cause problems.</p>
<p>Just a few notes.</p>
<p>If you are ending an email in a language other than English you may need to set the character set even for plain text. This is done the same as above but just replace the text/html with text/plain.</p>
<p>You can use variables for the relevant parts of the email. For example.</p>
<div class="bb-code">&lt;?php<br />
$recipient = &#8216;bill.gates@microsoft.com&#8217;;<br />
$subject = &#8216;Windows Linux&#8217;;<br />
$message = &#8216;When is it coming out?&#8217;;<br />
$headers = &#8216;From: email@address.com&#8217;;</p>
<p>mail($recipient, $subject, $message, $headers);<br />
?&gt;</p></div>
<p>As I also said earlier the mail function returns <strong>TRUE</strong> or <strong>FALSE</strong> depending on if it is successful in sending or not. So we can add some error checking like</p>
<div class="bb-code">&lt;?php<br />
if (@mail(&#8216;bill.gates@microsoft.com&#8217;, &#8216;Windows Linux&#8217;, &#8216;When is it coming out?&#8217;, &#8216;From: email@address.com&#8217;))<br />
{<br />
   echo &#8216;Your mail was successfully sent&#8217;;<br />
}<br />
else<br />
{<br />
   echo &#8216;There was a problem and the email did not get sent&#8217;;<br />
}<br />
?&gt;</div>
<p>You may wonder what the @ is for in front of the mail. The @ before a function tells PHP not to display any error messages that may result from running that function. It just keeps things tidy as we are error checking anyway.</p>
<p>And that&#8217;s about it for the basic usage of the PHP mail() function. Next time I will explain how to send emails that are both HTML and plain text so that our recipient can read it regardless of their email reader. I will also explain about adding attachments to your emails and displaying attached images inside the body of the email.</p>
]]></content:encoded>
			<wfw:commentRss>http://carbonize.co.uk/wp/2008/01/29/using-the-php-mail-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

