Back to the module list

Send mails by SMTP protocol

The SMTP protocol allow the PHP to send emails. It supports the encryption with the server, in the connection (STARTTLS), authentication. To create valid emails, use the mail class. Then use

echo $smtp->send ("sender@example.com",
                  "recipient@example.com",
                  $mail->getMail ());

If there is an error, an exception is raised

Light example

Use the local server and STARTTLS if the server support it. On port 25 by default.

$smtp = new smtp ();
$smtp->connect ();
$rc = $smtp->send ("sender@example.com", "recipient@example.com",
                   "From: Sender <sender@example.com>\r\n".
                   "To: Recipient <recipient@example.com>\r\n".
                   "Subject: Test\r\n".
                   "Message-ID: ".md5 (rand ().time ()). "\r\n".
                   "Date: ".date ("r")."\r\n".
                   "\r\n".
                   "Message\r\n");
$smtp->disconnect ();

The result of $smtp->send is the last valid message from the server.
Example : 250 2.0.0 Ok: queued as DF735C3BBA

With SSL support

Use the port 465 by default. The server must be defined as the certificate is

validated, or an exception will be raised.
   $smtp = new smtp ();
   $smtp->server = "smtp.example.com";
   $smtp->ssl = true;
   $smtp->connect ();
   $rc = $smtp->send ("sender@example.com", "recipient@example.com",
                      "From: Sender <sender@example.com>\r\n".
                      "To: Recipient <recipient@example.com>\r\n".
                      "Subject: Test\r\n".
                      "Message-ID: ".md5 (rand ().time ()). "\r\n".
                      "Date: ".date ("r")."\r\n".
                      "\r\n".
                      "Message\r\n");
   $smtp->reset ();
   $smtp->disconnect ();

With SSL support and authentication

Use the port 465 by default. The server must be defined as the certificate is

validated, or an exception will be raised.
   $smtp = new smtp ();
   $smtp->server = "smtp.example.com";
   $smtp->ssl = true;
   $smtp->user = "user@domain.tld";
   $smtp->password = "*********";
   $smtp->connect ();
   $rc = $smtp->send ("sender@example.com", "recipient@example.com",
                      "From: Sender <sender@example.com>\r\n".
                      "To: Recipient <recipient@example.com>\r\n".
                      "Subject: Test\r\n".
                      "Message-ID: ".md5 (rand ().time ()). "\r\n".
                      "Date: ".date ("r")."\r\n".
                      "\r\n".
                      "Message\r\n");
   $smtp->reset ();
   $smtp->disconnect ();

The class definition

Class \smtp

Namespace \

Description

 Allow to send mails by smtp

Properties

public $authmethods=array (plain,login);
 The authentication methods in an array. Allowed : plain, login
public $debug=0;
 Debug mode
public $debugFile="/tmp/debugSMTP";
 The debug file used to store all the communication between the client and
 the server. The file contains the passwords if used !
public $password;
 The authentication password allow to send SMTP mails
public $port=false;
 The SMTP port to use (if false, use 25 if no SSL or 465 if SSL,
 if user defined, use the user value)
public $server="127.0.0.1";
 The SMTP server name or IP
public $ssl=false;
 The SMTPS support by tunnelling the session in SSL transport
public $sslCheck=true;
 Check the certification chain in SSL mode
public $starttls="may";
 Activate STARTTLS if needed. Allowed values : none, may, encrypt
public $starttlsCheck=false;
 Check the certificate in STARTTLS
public $timeout=10;
 The Timeout between the answer of the SMTP server. If the server don't
 answer in this time, an exception is raised
public $user;
 The authentication user allow to send SMTP mails

Methods

public function connect ()
 Connect to the SMTP server

public function disconnect ()
 Disconnect from the SMTP server

public function reset ()
 Reset the session to start a new mail in the same SMTP connection

public function send ($from, $to, $completeMail)
 Send the mail to the users
 @param string $from The email address used for the from enveloppe
 @param string|array $to the recipient of the email. Not displayed in the
        email content
 @param string $completeMail The content of the email
 @return the message from the server (where is stored the message queue
 identifier