Noticed there was a few questions on here about sending mail using the mail() function and I had a few problems when I used it a while back, mainly its overly slow but also it doesn't give much control.
So I wrote my own mail function, its much easier with cURL but I didn't have that available on the server I was working on so I used fSockopen which is more or less standard on all hosting packages.

I welcome anyone who has suggestions to improve it and reuse if you like.

FUNCTION:
PHP Code:
function authSendEmail($from$fromname$to$toname$subject$message)
{
  
//SMTP + SERVER DETAILS
  /* * * * CONFIGURATION START * * * */
  
$smtpServer "****.atlas.pipex.net";
  
$port "25";
  
$timeout "2";
  
$username "no-reply@*******.co.uk";
  
$password "*********";
  
$identify "localhost"// This is used at the start of the connection to identify yourself but value isn't checked
  
$newLine "\r\n";
  
/* * * * CONFIGURATION END * * * * */
  
  //Connect to the host on the specified port
  
$smtpConnect = @fsockopen($smtpServer$port$errno$errstr$timeout);
  
  if(empty(
$smtpConnect)) {
    
$logArray['connection'] = "Failed to connect";
    
$logArray['SUCCESS'] = "FALSE";
    return 
$logArray;
  }
  
  
stream_set_timeout($smtpConnect$timeout);
  
  
$smtpResponse fgets($smtpConnect515);
  
$info stream_get_meta_data($smtpConnect);
  if (
$info['timed_out']) {
    
$logArray['connection'] = "Timeout: $smtpResponse";
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
$logArray['connection'] = "Connected: $smtpResponse";
  
  
//--- Say hello to the server --------
  
fputs($smtpConnect,"EHLO ".$identify $newLine);
  
$logArray['ehlo'] = fgets($smtpConnect515);
  
$info stream_get_meta_data($smtpConnect);
  
  
$i=0;
  while (!
$info['timed_out']) {
    
$logArray['ehlo'] .= fgets($smtpConnect515);
    
$info stream_get_meta_data($smtpConnect);
    
$i++;
  }
  if (
$i=0) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- Request Auth Login --------
  
fputs($smtpConnect,"AUTH LOGIN" $newLine);
  
$logArray['authrequest'] = fgets($smtpConnect515);
  if (
false === strpos($logArray['authrequest'],"334")) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- Send username --------
  
fputs($smtpConnectbase64_encode($username) . $newLine);
  
$logArray['authusername'] = fgets($smtpConnect515);
  if (
false === strpos($logArray['authusername'],"334")) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- Send password --------
  
fputs($smtpConnectbase64_encode($password) . $newLine);
  
$logArray['authpassword'] = fgets($smtpConnect515);
  if (
false === strpos($logArray['authpassword'],"235")) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- Email From --------
  
fputs($smtpConnect"MAIL FROM: $from$newLine);
  
$logArray['mailfrom'] = fgets($smtpConnect515);
  if (
false === strpos($logArray['mailfrom'],"250")) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- Email To --------
  
fputs($smtpConnect"RCPT TO: $to$newLine);
  
$logArray['mailto'] = fgets($smtpConnect515);
  if (
false === strpos($logArray['mailto'],"250")) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- The Email --------
  
fputs($smtpConnect"DATA" $newLine);
  
$logArray['data1'] = fgets($smtpConnect515);
  if (
false === strpos($logArray['data1'],"354")) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- Construct Headers --------
  
$headers "MIME-Version: 1.0" $newLine;
  
$headers .= "Content-type: text/html; charset=iso-8859-1" $newLine;
  
$headers .= "To: ".$toname." <".$to.">"$newLine;
  
$headers .= "From: ".$fromname." <".$from.">" $newLine;
  
$headers .= "Subject: ".$subject.$newLine;
  
  
$message str_replace("\n.""\n..",$message);
  
  
  
fputs($smtpConnect$headers.$newLine.$message."\n.\n");
  
$logArray['data2'] = fgets($smtpConnect515);
  if (
false === strpos($logArray['data2'],"250")) {
    
$logArray['SUCCESS'] = "FALSE";
    
fclose($smtpConnect);
    return 
$logArray;
  }
  
  
//--- Say Bye to SMTP --------
  
fputs($smtpConnect,"QUIT" $newLine);
  
$logArray['quit'] = fgets($smtpConnect515);
  
  
//fclose($smtpConnect);
  
$logArray['SUCCESS'] = "TRUE";
  
  return 
$logArray;


USAGE:
PHP Code:
function SendEmail_Error($MSG)
{
  
// This is sent to the Operator when an error occours.
  
$subject "An Error has occurred on Eden Photography";
  
$message "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  
$message.= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
  
$message.= "<head><style type=\"text/css\">\n";
  
$message.= ".ErrorMSG{font-family: Courier New, sans-serif; font-size:12pt; color:#222222;}\n";
  
$message.= ".ErrorSQL{font-family: Courier New, sans-serif; font-size:12pt; color:#222252; width:100%; border-style:solid; border-color:#222252; border-width:1px; background-color:#CCBBBB; padding: 10px 0px;}\n";
  
$message.= ".ErrorText{font-family: Tahoma, Arial, sans-serif; font-size:10pt; color:#222222;}";
  
$message.= ".ErrorCode{font-family: Courier New, sans-serif; font-size:12pt; color:#222252; width:100%; border-style:solid; border-color:#222252; border-width:1px; background-color:#BBCCBB; padding: 10px 0px;}";
  
$message.= ".ErrorTitle{font-family: Tahoma, Arial, sans-serif; font-size:11pt; color:#222222; text-decoration:underline; margin-bottom:10px;}";
  
$message.= "</style>";
  
$message.= "</head><body>";
  
$message.= "The error was as follows:<br/>\n<br/>\n".$MSG."\n</body></html>"
  
  
//echo "<div class=\"BrowseBox\">To: ".CONST_EMAIL_ERROR."<br/>\n<strong>".$subject."</strong><br/>\n".$message."<br/><br/></div>";
  
return authSendEmail(CONST_EMAIL_FROM_ADDRE,CONST_EMAIL_FROM_NAME,CONST_EMAIL_ERROR,"Error Handler"$subject$message);

OUTPUT:
PHP Code:
Array
(
    [
connection] => Connected220 ****.atlas.pipex.net ESMTP Exim 4.71 "ATLAS SMTP Service" Wed10 Aug 2011 23:31:01 +0100

    
[ehlo] => 250-****.atlas.pipex.net Hello #####.atlas.pipex.net [10.15.10.49]
250-SIZE 31457280
250
-PIPELINING
250
-AUTH PLAIN LOGIN
250 HELP

    
[authrequest] => 334 VXNlcm5hbWU6

    
[authusername] => 334 UGFzc3dvcmQ6

    
[authpassword] => 235 Authentication succeeded

    
[mailfrom] => 250 OK

    
[mailto] => 250 Accepted

    
[data1] => 354 Enter messageending with "." on a line by itself

    
[data2] => 250 OK id=1QrHIl-00031o-Pe

    
[quit] => 221 ****.atlas.pipex.net closing connection

    
[SUCCESS] => TRUE