Page 1 of 1

Why doesn't this email function work?

Posted: Tue Oct 11, 2005 12:42 pm
by Luke

Code: Select all

function emailer($fromname, $fromaddress, $toname, $toaddress, $subject, $message)
{
   // Copyright ? 2005 ECRIA LLC, http://www.ECRIA.com
   // Please use or modify for any purpose but leave this notice unchanged.
   $headers  = "MIME-Version: 1.0\n";
   $headers .= "Content-type: text/plain; charset=iso-8859-1\n";
   $headers .= "X-Priority: 3\n";
   $headers .= "X-MSMail-Priority: Normal\n";
   $headers .= "X-Mailer: php\n";
   $headers .= "From: \"".$fromname."\" <".$fromaddress.">\n";
   return mail($toaddress, $subject, $message, $headers);
}
I got this function from the manual on php.net... it has always worked before I use it all the time. It isn't working... what could be the problem?

Is it possible that my server isn't handling the mail() function correctly? That's the only thing I can think of.

Posted: Tue Oct 11, 2005 12:56 pm
by feyd
there's many many many reasons it may be backfiring, try using phpMailer instead

Posted: Tue Oct 11, 2005 1:13 pm
by RainbowOfChaos
I too am thinking that the likely hood is, is that you do not have a configured SMTP server running on your localhost, try specifying the "host" parameter to a free/open/public available SMTP server and seeing if it works, in something like the PEAR object to send a mail, this package is available at: http://pear.php.net/package/Mail

Example code being

Code: Select all

<?
include("Mail.php");

$recipients = "mail_to@domain.mail";

$headers["From"]    = "mail_from@domain.mail";
$headers["To"]      = "mail_to@domain.mail";
$headers["Subject"] = "Test message";

$body = "TEST MESSAGE!!!";

$params["host"] = "smtp.server";
$params["port"] = "25";
$params["auth"] = true;
$params["username"] = "user";
$params["password"] = "password";

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $params);

$mail_object->send($recipients, $headers, $body);
?>

Posted: Tue Oct 11, 2005 1:26 pm
by RobertGonzalez
Are you on a windows server? I had a problem with the same function on a windows server because of the "To" email format ( "Fname Lname" <email@domain.tld> ). Try getting rid of the everything in the "To" address except the email address and see if that works.

Posted: Tue Oct 11, 2005 1:56 pm
by Luke
What's really weird is that it works FINE on one page, and not on another. I checked EVERY variable... here's what it sends:

from: rma@whatever_dot_whatever
to: sales@whatever_dot_whatever
from name: James
to name Security Freeze
subject: Security Freeze Documents
email: Whatever

That function works FINE on one page, and not on this one... WHAT the hell??

Posted: Tue Oct 11, 2005 2:09 pm
by RainbowOfChaos
Can you paste what parameters are sent from each page? Also, please error trap the mail object and print out what it's returning.

Posted: Tue Oct 11, 2005 2:14 pm
by Luke
RainbowOfChaos wrote:Can you paste what parameters are sent from each page? Also, please error trap the mail object and print out what it's returning.
What do you mean error trap it?

Posted: Tue Oct 11, 2005 2:59 pm
by Luke
Here's what I have now:

Code: Select all

$to = $cust['customer_email'];
                $subject = "Security Freeze Documents";
                $headers = "From: sales@somewhere_dot_somewhere\r\n" .
                       'X-Mailer: PHP/' . phpversion() . "\r\n" .
                       "MIME-Version: 1.0\r\n" .
                       "Content-Type: text/html; charset=utf-8\r\n" .
                       "Content-Transfer-Encoding: 8bit\r\n\r\n";
                if(mail($to, $subject, $email, $headers)){
                    return true;
                }
It returns true... but I never get the email WHAT IS GOING ON???
I have been working with php for years and never been this frustrated.

Posted: Tue Oct 11, 2005 3:54 pm
by feyd
have you tried using phpMailer yet?

Posted: Tue Oct 11, 2005 6:21 pm
by RobertGonzalez
What value are you using for $email in the below code?
One armed space goat wrote:Here's what I have now:

Code: Select all

$to = $cust['customer_email'];
                $subject = "Security Freeze Documents";
                $headers = "From: sales@somewhere_dot_somewhere\r\n" .
                       'X-Mailer: PHP/' . phpversion() . "\r\n" .
                       "MIME-Version: 1.0\r\n" .
                       "Content-Type: text/html; charset=utf-8\r\n" .
                       "Content-Transfer-Encoding: 8bit\r\n\r\n";
                if(mail($to, $subject, $email, $headers)){
                    return true;
                }
It returns true... but I never get the email WHAT IS GOING ON???
I have been working with php for years and never been this frustrated.

Posted: Tue Oct 11, 2005 6:32 pm
by fdesensi
If you are can't figure out the answer to your problem, I had to made an SMTP script by hand (don't ask) and it works just fine for sending mail here it is.

Code: Select all

<?php
/**************************************************************************************
*
* Title:  	SMTP Library 
* Version: 	1.1
* Author: 	Francesco DeSensi
*
* Version Notes:
*	See file /docs/history.txt for further information
*		
**************************************************************************************/

class SMTP
{

	var $SMTP_PORT; 			// the default SMTP PORT
    var $CRLF;   				// CRLF pair

    var $smtp_conn;      		// the socket to the server
    var $error;          		// error if any on the last call
    var $helo_rply;      		// the reply the server sent to us for HELO

    var $do_debug;       		// the level of debug to perform
	var $usr_header;			// allow user to specify header
	var $confirm;		 		// confirm a mailing has been sent
	
	var $host;		 			// host to be used to send
	var $port;					// smtp port
	var $timeout;				// set timeout for the connection
	var $user;					// set the user for the server
	var $password;				// set the user password for the server
	
	
	
	
	/****************************************************************
	* SMTP()
	*
	* Return:
	* 	N/A
	*
	* Desc: 
	*	This is the constructor
	****************************************************************/
	function SMTP()
	{
		//connection vars
		$this->smtp_conn 	= 0;
		$this->helo_rply 	= null;
		$this->SMTP_PORT 	= 25; 		
    	$this->CRLF 		= "\r\n";   
		
		//debug information
		$this->do_debug	 	= 0;
		$this->confirm	 	= 0;
        $this->error	 	= null;
		
		//connection information
		$this->host			= "";
		$this->port			= 25;
		$this->timeout		= 30;
		$this->user			= "";
		$this->password		= "";
    }
	
	
	
	/****************************************************************
	* SendMail()
	*
	* Return:
    * 	N/A 
	*
	* Desc: 
	*	Pull everthing together and send an email
	****************************************************************/
	function SendMail()
	{
		//Connect
		$this->Connect( $this->host,$this->port,$this->timeout); 
		$this->Hello($this->host);
		$this->Authenticate($this->user, $this->password);
		
		$args 		= func_get_args();
        $arg_count 	= count($args);
		
		//user has provided the data
		if( $arg_count == 3)
		{
			$to 	= $args[0];
			$from 	= $args[1];
			$data 	= $args[2];
			
			//Mailing
			$this->MailFrom($from);
			$this->MailTo($to);
			$this->Data($data);
		}
		 elseif( $arg_count == 4 )
		{
			$to 		= $args[0];
			$from 		= $args[1];
			$subject	= $args[2];
			$message	= $args[3];
			
			//Mailing
			$this->MailFrom($from);
			$this->MailTo($to);
			
			//Use the default header
			$data = "To: $to\nFrom: $from\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1\nSubject: $subject\n\n" . $message;
			$this->Data($data);
		}
		 else
		{
			if($this->do_debug >= 1 )
			{
				print("<br><br><strong>You have supplied an invalid number of arguments for SendMail()</strong><br>" .
					  "If you are providing your own data values use this format: SendMail([to],[from],[data])<br>" .
					  "If you are are using the standard usage use this format: SendMail([to],[from],[subject],[message])<br><br>");
			}
			
			$SendError = true;
		}
		
		//Cleanup
		$this->Quit();
		$this->Close();
		
		//If confirm is set to 1 then print out a confirmation if it was
		//sent or not
		if($this->confirm == 1)
		{
			if( $SendError != true )
			{
				print("An email has been sent to: $to<br>");
			}
			 else
			{
				print("The email was not sent as there are errors. If you do not see them set $do_debug=1");
			}
		}
	}






	/**********************************************************************************
	*
	* CONNECTION FUNCTIONS
	*
	* Functions to for connecting to and sending smtp servers
	*
	* Listing:
	* 	- SMTP 	  
	* 	- Connect
	*
	* Note:
	* 	SMTP CODE SUCCESS: 220
    * 	SMTP CODE FAILURE: 421
	**********************************************************************************/


	/****************************************************************
	* Connect ( $host, $port, $tval=30 )
	*
	* Return:
    * 	boolean
	*
	* Desc: 
	*	Connec to the specified host
	****************************************************************/
	function Connect( $host,$port=0,$tval=30 ) 
	{
        // Esnure there is no error handling confusion
        $this->error = null;

        // Make sure we are not already connected
        if($this->connected()) 
		{
            
            $this->error =
                array("error" => "Already connected to a server");
            return false;
		}

        // If the port is empty then set it to the default
		if(empty($port)) 
		{
            $port = $this->SMTP_PORT;
        }

        // Connect to the smtp server
        $this->smtp_conn = fsockopen($host, $port, $errno, $errstr, $tval); 
		  
        // Verify we connected properly, if not set up errors
        if(empty($this->smtp_conn)) 
		{
            $this->error = array("error"  => "Failed to connect to server",
                                 "errno"  => $errno,
                                 "errstr" => $errstr);
            if($this->do_debug >= 1) 
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": $errstr ($errno)" . $this->CRLF;
            }
			
            return false;
        }

        // The SMTP server may take a little longer to respond
        // so we will give it a longer timeout for the first try
        // NOTE: Windows still does not have support for this timeout 
		//       function
        if(substr(PHP_OS, 0, 3) != "WIN") 
		{
           socket_set_timeout($this->smtp_conn, 1, 0);
		}

        // get any announcement stuff
        $announce = $this->get_lines();

        if($this->do_debug >= 2) 
		{
            echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
        }

        return true;
    }
	
	
	
	
	/****************************************************************
	* Authenticate( $username, $password )
	*
	* Return:
    * 	boolean 
	*
	* Desc: 
	*	Connec to the specified host
	****************************************************************/
	function Authenticate($username, $password) 
	{
        // Start authentication
        fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);

        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($code != 334)
		{
            $this->error =
                	array("error" => "AUTH not accepted from server",
                      "smtp_code" => $code,
                      "smtp_msg" => substr($rply,4));
					  
            if($this->do_debug >= 1)
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }
			
            return false;
        }

        // Send encoded username
        fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);

        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($code != 334) 
		{
            $this->error =
                   array("error" => "Username not accepted from server",
                      	 "smtp_code" => $code,
                      	 "smtp_msg" => substr($rply,4));
					  
            if($this->do_debug >= 1) 
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }

            return false;
        }

        // Send encoded password
        fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);

        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($code != 235) 
		{
            $this->error =
                	array("error" => "Password not accepted from server",
                      "smtp_code" => $code,
                      "smtp_msg" => substr($rply,4));
					  
            if($this->do_debug >= 1) 
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }
            
			return false;
        }

        return true;
    }
	
	
	
	/****************************************************************
	* Connected()
	*
	* Return:
    * 	boolean 
	*
	* Desc: 
	*	Determine if we a truly connected to the server. If not
	*	close the connection and clean up.
	****************************************************************/
	function Connected() 
	{
        if(!empty($this->smtp_conn)) 
		{
            $sock_status = socket_get_status($this->smtp_conn);
			
            if($sock_status["eof"]) 
			{
                // Socket is valid but we aren't connected anymore
                if($this->do_debug >= 1) 
				{
                    echo "SMTP -> NOTICE:" . $this->CRLF .
                         "EOF caught while checking if connected";
                }
				
                $this->Close();
		        return false;
            }
            return true;
        }
        return false;
    }
	
	
	
	/****************************************************************
	* Close()
	*
	* Return:
    * 	N/A 
	*
	* Desc: 
	*	This will clean up the connection
	****************************************************************/
	function Close() 
	{
        // Ensure there is no variable confusion
		$this->error = null; 
        $this->helo_rply = null;
		
        if(!empty($this->smtp_conn)) 
		{
            // close the connection and cleanup
            fclose($this->smtp_conn);
            $this->smtp_conn = 0;
        }
    }
	
	
	
	
	/**********************************************************************************
	*
	* MAIL FUNCTIONS
	*
	* Functions to for connecting to and sending smtp servers
	*
	* Listing:
	* 	- DATA	  
	* 	- Reciepent
	*
	* Note:
	* Implements rfc 821: DATA <CRLF>
    *
    * SMTP CODE INTERMEDIATE: 354
    *     [data]
    *     <CRLF>.<CRLF>
    *     SMTP CODE SUCCESS: 250
    *     SMTP CODE FAILURE: 552,554,451,452
    * 	  SMTP CODE FAILURE: 451,554
    *	  SMTP CODE ERROR  : 500,501,503,421
	*
	**********************************************************************************/
	
	/****************************************************************
	* Hello()
	*
	* Return:
    * 	N/A 
	*
	* Desc: 
	*	Identify our client to the SMTP server
	****************************************************************/
	function Hello($host="") 
	{
        $this->error = null;

        if(!$this->connected()) 
		{
            $this->error = array(
                    "error" => "Called Hello() without being connected");
					
            return false;
        }

        // if a hostname for the HELO wasn't specified determine
        // a suitable one to send
        if(empty($host)) 
		{
            // we need to determine some sort of appopiate default
            // to send to the server
            $host = "localhost";
        }

        fputs($this->smtp_conn,"HELO " . $host . $this->CRLF);

        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($this->do_debug >= 2) 
		{
            echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
        }

        if($code != 250) 
		{
            $this->error =
                array("error" => "HELO not accepted from server",
                      "smtp_code" => $code,
                      "smtp_msg" => substr($rply,4));
					  
            if($this->do_debug >= 1) 
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }
			
            return false;
        }

        $this->helo_rply = $rply;

        return true;
    }




	/****************************************************************
	* Data()
	*
	* Return:
    * 	N/A 
	*
	* Desc: 
	*	Accepts data and will clean up various elements to ensure
	*	proper sending.
	****************************************************************/
	function Data($msg_data) 
	{
		// Ensure there is no confusion
        $this->error = null;

        if(!$this->connected()) 
		{
            $this->error = array(
                    "error" => "Called Data() without being connected");
            return false;
        }

        fputs($this->smtp_conn,"DATA" . $this->CRLF);

        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($this->do_debug >= 2) 
		{
            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
        }

        if($code != 354) 
		{
            $this->error =
                array("error" => "DATA command not accepted from server",
                      "smtp_code" => $code,
                      "smtp_msg" => substr($rply,4));
					  
            if($this->do_debug >= 1) 
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }
            
			return false;
        }

        // normalize the line breaks so we know the explode works
        $msg_data = str_replace("\r\n","\n",$msg_data);
        $msg_data = str_replace("\r","\n",$msg_data);
        $lines = explode("\n",$msg_data);

        // Assuming rfc 822 definitions of msg headers
        // and if the first field of the first line (':' sperated)
        // does not contain a space then it _should_ be a header
        // and we can process all lines before a blank "" line as
        // headers.
        $field = substr($lines[0],0,strpos($lines[0],":"));
        $in_headers = false;
		
        if(!empty($field) && !strstr($field," ")) 
		{
            $in_headers = true;
        }

        $max_line_length = 998; // used below; set here for ease in change

        while(list(,$line) = @each($lines)) 
		{
            $lines_out = null;
			
            if($line == "" && $in_headers) 
			{
                $in_headers = false;
            }
			
            // ok we need to break this line up into several
            // smaller lines
            while(strlen($line) > $max_line_length) 
			{
                $pos = strrpos(substr($line,0,$max_line_length)," ");
                $lines_out[] = substr($line,0,$pos);
                $line = substr($line,$pos + 1);
                
				// if we are processing headers we need to
                // add a LWSP-char to the front of the new line
                // rfc 822 on long msg headers
                if($in_headers) 
				{
                    $line = "\t" . $line;
                }
            }
            
			$lines_out[] = $line;

            // now send the lines to the server
            while(list(,$line_out) = @each($lines_out)) 
			{
                if(strlen($line_out) > 0)
                {
                    if(substr($line_out, 0, 1) == ".") 
					{
                        $line_out = "." . $line_out;
                    }
                }
				
                fputs($this->smtp_conn,$line_out . $this->CRLF);
            }
        }

        // ok all the message data has been sent so lets get this
        // over with aleady
        fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);

        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($this->do_debug >= 2) 
		{
            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
        }

        if($code != 250) 
		{
            $this->error =
                array("error" => "DATA not accepted from server",
                      "smtp_code" => $code,
                      "smtp_msg" => substr($rply,4));
					  
            if($this->do_debug >= 1)
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }
			
            return false;
        }
		
        return true;
    }
	
	
	
	
	/****************************************************************
	* MailFrom()
	*
	* Return:
    * 	N/A 
	*
	* Desc: 
	*	Specify the MAIL FROM: clause
	****************************************************************/
	function MailFrom($from,$to,$message) 
	{
        $this->error = null; 

        if(!$this->connected()) 
		{
            $this->error = array(
                    "error" => "Called Mail() without being connected");
            return false;
        }

		$strDate = date();
		
        fputs($this->smtp_conn,"MAIL From: postmaster@domain.com" . $this->CRLF);
		
        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($this->do_debug >= 2) 
		{
            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
        }

        if($code != 250) {
            $this->error =
                array("error" => "MAIL not accepted from server",
                      "smtp_code" => $code,
                      "smtp_msg" => substr($rply,4));
            if($this->do_debug >= 1) 
			{
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }
			
            return false;
        }
		
        return true;
    }
	
	
	
	/****************************************************************
	* MailTo()
	*
	* Return:
    * 	N/A 
	*
	* Desc: 
	*	Specify the RCPT TO: Clause
	****************************************************************/
	function MailTo($to) 
	{
        $this->error = null; 

        if(!$this->connected()) {
            $this->error = array(
                    "error" => "Called Recipient() without being connected");
            return false;
        }

        fputs($this->smtp_conn,"RCPT TO:" . $to . $this->CRLF);

        $rply = $this->get_lines();
        $code = substr($rply,0,3);

        if($this->do_debug >= 2) {
            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
        }

        if($code != 250 && $code != 251) {
            $this->error =
                array("error" => "RCPT not accepted from server",
                      "smtp_code" => $code,
                      "smtp_msg" => substr($rply,4));
            if($this->do_debug >= 1) {
                echo "SMTP -> ERROR: " . $this->error["error"] .
                         ": " . $rply . $this->CRLF;
            }
            return false;
        }
        return true;
    }
	
	
	
	
	
	/****************************************************************
	* Quit()
	*
	* Return:
    * 	N/A 
	*
	* Desc: 
	*	Send termniation to the server
	****************************************************************/
	function Quit($close_on_error=true) 
	{
        $this->error = null;

        if(!$this->connected()) {
            $this->error = array(
                    "error" => "Called Quit() without being connected");
            return false;
        }

        // send the quit command to the server
        fputs($this->smtp_conn,"quit" . $this->CRLF);

        // get any good-bye messages
        $byemsg = $this->get_lines();

        if($this->do_debug >= 2) {
            echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
        }

        $rval = true;
        $e = null;

        $code = substr($byemsg,0,3);
        if($code != 221) {
            // use e as a tmp var cause Close will overwrite $this->error
            $e = array("error" => "SMTP server rejected quit command",
                       "smtp_code" => $code,
                       "smtp_rply" => substr($byemsg,4));
            $rval = false;
            if($this->do_debug >= 1) {
                echo "SMTP -> ERROR: " . $e["error"] . ": " .
                         $byemsg . $this->CRLF;
            }
        }

        if(empty($e) || $close_on_error) {
            $this->Close();
        }

        return $rval;
    }
	
	
	
	
	
	/**********************************************************************************
	*
	* UTILITY FUNCTIONS
	*
	* Functions used by the SMTP functions to more easily handle
	* information.
	*
	* Listing:
	* 	- get_lines
	*
	**********************************************************************************/

	/****************************************************************
	* get_lines()
	*
	* Return:
    * 	$data - value gathered from socket using fgets(...)
	*
	* Desc: 
	*	Gather responses form the server using smtp_conn
	****************************************************************/
	function get_lines() 
	{
        $data = "";
		
        while($str = fgets($this->smtp_conn,515)) 
		{
            if($this->do_debug >= 4) 
			{
                print("SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . "<br>");
                print("SMTP -> get_lines(): \$str is \"$str\"" 	. $this->CRLF . "<br>");
            }
			
            $data .= $str;
			
            if($this->do_debug >= 4) 
			{
                print("SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . "<br>");
            }
			
            // if the 4th character is a space then we are done reading
            // so just break the loop
            if(substr($str,3,1) == " ") 
			{ 
				break; 
			}
        }
        return $data;
    }
		
}
?>
Make sure to change:

Code: Select all

fputs($this->smtp_conn,"MAIL From: postmaster@domain.com" . $this->CRLF);
and

Code: Select all

//connection information
		$this->host			= "";
		$this->port			= 25;
		$this->timeout		= 30;
		$this->user			= "";
		$this->password		= "";

Posted: Tue Oct 11, 2005 11:06 pm
by RobertGonzalez
One other thing... have you validated your vars to make sure they are passing what you think they are passing. Just a thought, but try echoing the vars out before or after the mail function to see what the mailer is getting passed into it. Another thing to try is putting an off network email address in each of the addressee fields (To, CC, BCC) to see if you get them in all three. When I was having mail problems I tried sending to my domain email, my yahoo account and my gmail account just to see how each mail server received the message.

These are just some more ideas. Without seeing the entire script or trying it out on your server I am at a loss for ideas.
One armed space goat wrote:Here's what I have now:

Code: Select all

$to = $cust['customer_email'];
                $subject = "Security Freeze Documents";
                $headers = "From: sales@somewhere_dot_somewhere\r\n" .
                       'X-Mailer: PHP/' . phpversion() . "\r\n" .
                       "MIME-Version: 1.0\r\n" .
                       "Content-Type: text/html; charset=utf-8\r\n" .
                       "Content-Transfer-Encoding: 8bit\r\n\r\n";
                if(mail($to, $subject, $email, $headers)){
                    return true;
                }
It returns true... but I never get the email WHAT IS GOING ON???
I have been working with php for years and never been this frustrated.