Free SMTP PHP Mail Script

Free Script for Sending Mail using SMTP instead of mail()


If your webserver does hasn't got sendmail installed you cannot use the mail() PHP function to send mail using scripts. An alternative needs to be found and the solution is sending email vis SMTP server. The beauty of this means you can use any SMTP server you have permission to connect to to send your mail.
We have designed our own SMTP function that can be added to any form with ease and is available to download here for Free!

Download the FREE SMTP Script Now




FREE CAPTCHA System Requirements




Documentation

The download package consists of 2 files. 1)The mail script, 2)The mail function, 3


The Form Submission Page

The Code:

<?php
    $from="sales@xenserv.co.uk";
  $namefrom="Sales Department";
  $to = "receivers@email.com";
  $nameto = "To Name";
  $subject = "Email Subject";
  $message = "Email Message goes here.....";
  
  smtp($from, $namefrom, $to, $nameto, $subject, $message,);

?>

This is the mail script parameters used to construct the email ready to be sent. We then pass these parameters to the smtp function below

The SMTP function

    <?php
    function smtp ($from, $namefrom, $to, $nameto, $subject, $message) {
        $smtpServer = "192.168.xxx.xxx";   //ip address of the mail server or hostname
        $port = "25";					 // should be 25 by default, 
        $timeout = "45";				 // typical timeout. try 45 for slow servers
        $username = "sales@xenserv.co.uk"; // the login for your smtp
        $password = "myPA$$";			// the password for your smtp
        $localhost = "127.0.0.1";	   // Defined IP for the web server.  
        $newLine = "\r\n";			 // aka, carrage return line feed. var just for newlines in MS
        $secure = 0;				  // change to 1 if your server is running under SSL
        //connect to the host and port
        $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
        $smtpResponse = fgets($smtpConnect, 4096);
                
        //you have to say HELO again after TLS is started
           fputs($smtpConnect, "HELO $localhost". $newLine);
           $smtpResponse = fgets($smtpConnect, 4096);
           $logArray['heloresponse2'] = "$smtpResponse";
        //request for auth login
        fputs($smtpConnect,"AUTH LOGIN" . $newLine);
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['authrequest'] = "$smtpResponse";
        
        //send the username
        fputs($smtpConnect, base64_encode($username) . $newLine);
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['authusername'] = "$smtpResponse";
        
        //send the password
        fputs($smtpConnect, base64_encode($password) . $newLine);
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['authpassword'] = "$smtpResponse";
        
        //email from
        fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['mailfromresponse'] = "$smtpResponse";
        
        //email to
        fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['mailtoresponse'] = "$smtpResponse";
        
        //the email
        fputs($smtpConnect, "DATA" . $newLine);
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['data1response'] = "$smtpResponse";
        
        //construct headers
        $headers = "MIME-Version: 1.0" . $newLine;
        $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
        $headers .= "To: $nameto <$to>" . $newLine;
        $headers .= "From: $namefrom <$from>" . $newLine;
        
        //observe the . after the newline, it signals the end of message
        fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['data2response'] = "$smtpResponse";
        
        // say goodbye
        fputs($smtpConnect,"QUIT" . $newLine);
        $smtpResponse = fgets($smtpConnect, 4096);
        $logArray['quitresponse'] = "$smtpResponse";
        $logArray['quitcode'] = substr($smtpResponse,0,3);
        fclose($smtpConnect);
        //a return value of 221 in $retVal["quitcode"] is a success
        return($logArray);
        }
?>
    
    

This is the smtp mail function to process the parameters and send the mail. You can also use this function to send attachments, but modifications are required.