Send Email From a PHP Script Using SMTP Authentication

Email
alengo/Getty Images

Sending an email from a PHP script is simple, fast and easy...if it works!

PHP mail() and SMTP Authentication

Part of what makes the PHP mail() function is so simple is its lack of flexibility. It's frustrating that stock PHP mail() does not usually allow you to use the SMTP server of your choice, and it does not support SMTP authentication — required by many mail servers today — at all.

PEAR Mail for SMTP Authentication and SSL Connection

Fortunately, overcoming PHP's built-in shortcomings is not difficult.

For most email uses, the free PEAR Mail package offers all the power and flexibility needed, and it authenticates with your desired outgoing mail server. For enhanced security, encrypted SSL connections are supported for sending mail using PEAR Mail as well.

Send Email from a PHP Script Using SMTP Authentication

To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email:

  • Make sure the PEAR Mail package is installed.
    • Typically, in particular with PHP 4 or later, this will have already been done for you. Just give it a try.
  • Adapt the example below for your needs. Make sure you change the following variables at least:
    • from: the email address from which you want the message to be sent.
    • to: the recipient's email address and name.
    • host: your outgoing SMTP server name.
    • username: the SMTP user name (typically the same as the username used to retrieve mail).
    • password: the password for SMTP authentication.

    Sending Mail from PHP Using SMTP Authentication - Example

    <?php
     require_once "Mail.php";
     
     $from = "Sandra Sender <sender@example.com>";
     $to = "Ramona Recipient <recipient@example.com>";
     $subject = "Hi!";
     $body = "Hi,\n\nHow are you?";
     
     $host = "mail.example.com";
     $username = "smtp_username";
     $password = "smtp_password";
     
     $headers = array ('From' => $from,
       'To' => $to,
       'Subject' => $subject);
     $smtp = Mail::factory('smtp',
       array ('host' => $host,
         'auth' => true,
         'username' => $username,
         'password' => $password));
     
     $mail = $smtp->send($to, $headers, $body);
     
     if (PEAR::isError($mail)) {
       echo("<p>" . $mail->getMessage() . "</p>");
      } else {
       echo("<p>Message successfully sent!</p>");
      }
     ?>

    Sending Mail from PHP Using SMTP Authentication and SSL Encryption - Example

    <?php
     require_once "Mail.php";
     
     $from = "Sandra Sender <sender@example.com>";
     $to = "Ramona Recipient <recipient@example.com>";
     $subject = "Hi!";
     $body = "Hi,\n\nHow are you?";
     
     $host = "ssl://mail.example.com";
     $port = "465";
     $username = "smtp_username";
     $password = "smtp_password";
     
     $headers = array ('From' => $from,
       'To' => $to,
       'Subject' => $subject);
     $smtp = Mail::factory('smtp',
       array ('host' => $host,
         'port' => $port,
         'auth' => true,
         'username' => $username,
         'password' => $password));
     
     $mail = $smtp->send($to, $headers, $body);
     
     if (PEAR::isError($mail)) {
       echo("<p>" . $mail->getMessage() . "</p>");
      } else {
       echo("<p>Message successfully sent!</p>");
      }
     ?>