PHP Classes

Simple External SMTP: Send e-mail messages via SMTP with authentication

Recommend this page to a friend!
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 70%Total: 1,117 All time: 3,365 This week: 110Up
Version License PHP version Categories
secure-class-email 1.0.0BSD License5.1Email, Networking, PHP 5


This class can send e-mail messages via SMTP with authentication.

It can compose an e-mail message with a given sender, recipient, subject and message body in either text or HTML.

The class can send the message connecting to a given SMTP server or by send using the PHP mail function..

SMTP server delivery supports authentication using different methods and secure connections using encryption with the STARTTLS command or using SSL and TLS.

Picture of Sceptre
Name: Sceptre <contact>
Classes: 2 packages by
Country: Germany Germany
Age: ???
All time rank: 1865116 in Germany Germany
Week rank: 441 Up14 in Germany Germany Up

Details This is a PHP 5 class to send e-mails from web or command line scripts (CLI) via an external SMTP server like It has been designed to be a replacement for the built in PHP mail() function by accepting the same input for sending e-mails. This makes it possible to implement the class with only minor modifications to your existing code. The class uses UTF-8 + base64 encoding to ensure that non-ASCII characters are displayed properly. It also supports encryption via STARTTLS, tls:// or ssl:// and CRAM-MD5, PLAIN or LOGIN authentication. This class is designed for business use where you want all outgoing e-mails to pass through the e-mail servers of your company. The class opens a tcp socket to the smtp server and will communicate with it just like a simple e-mail client would. I have tested this class on multiple Linux systems with the PHP5 packages provided by the distributions and on Windows XP/7/2008 with the latest XAMPP release. The class appears to be working on any of them so I am releasing it to the public. Please let me know if you are using the class in your project and please don't forget to send me any improvement ideas or bug fixes. Here is some sample code for the class. <?PHP require_once 'SendEmail.php'; $e = new SendEmail(); //First value is the URL of your server, the second the port number $e->set_server( 'smtp.localhost.local', 587); //First value is your username, then your password $e->set_auth('user@localhost.local', 'YourPasswordHere'); //Set the "From" setting for your e-mail. The Name will be base64 encoded $e->set_sender( 'Your Name here Ää, Öö, Üü', 'support@localhost.local' ); //for one recipient $send_to = 'foo@localhost.local'; //you may also specify multiple recipients by creating an array like this: $send_to = array('foo1@localhost.local', 'foo2@localhost.local', 'foo3@localhost.local'); $subject = 'This is a test subject Ää, Öö, Üü'; $body = "This is the test body of the message\r\nIt may contain special characters: Ää, Öö, Üü\r\n"; if( $e->mail($send_to, $subject, $body) == true ) { //message was received by the smtp server //['last'] tends to contain the queue id so I like to save that string in the database echo 'last: '.htmlspecialchars($e->srv_ret['last']).''; }else{ //something went wrong echo 'all: '.nl2br(htmlspecialchars($e->srv_ret['all'])).''; echo 'full:'.nl2br(htmlspecialchars($e->srv_ret['full'])).''; } ?> Once you call $e->mail() you can use the $e->srv_ret[] array to get some debugging information. * ['last'] contains the last message from the smtp server or this class. * ['all'] contains everything the server sent to this class. * ['full'] contains everything the class sent to the server and the response The full log is useful if you are having trouble making a connection because you will know at which point the error occurs. The output will vary depending on the e-mail server configuration or version but it tends to be: last: 250 2.0.0 Ok: queued as 167D3C2031E1 all: 220 ESMTP l26sm7962357fad.17 at your service, [] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250 ENHANCEDSTATUSCODES 220 2.0.0 Ready to start TLS .... and so on full: notice: will attempt to switch to a tls secured connection after EHLO notice: connected to server recieved: 220 ESMTP l26sm7983346fad.17 notice: checking expected value 220 against the server response of 220 -- PASSED! sent: EHLO your.rdns.hostname received: at your service, [] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250 ENHANCEDSTATUSCODES notice: checking expected value 250 against the server response of 250 -- PASSED! sent: STARTTLS received: 220 2.0.0 Ready to start TLS notice: checking expected value 220 against the server response of 220 -- PASSED! notice: tls is now enabled notice: sending EHLO again but this time encrypted ... and so on As you can see it is simple to send a message but it provides you with debug information when stuff is not working as expected. There are a few more methods explained in the included readme but the default settings should work on most servers. Please keep in mind that the communication with an external SMTP server will take a few seconds so it is a good idea to queue messages before sending them. I do this with a cronjob or a Windows Task executing "php -f queue_script.php" every few minutes and it works great!

  Files folder image Files  
File Role Description
Accessible without login Plain text file README.txt Doc. basic documentation
Accessible without login Plain text file SendEmail-example.php Example Example script
Plain text file SendEmail.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:3,365
This week:110Up
User Ratings User Comments (1)
 All time