Learn to Send Email From a PHP Script Using Simple SMTP Authentication

How to connect to an outgoing SMTP server from a PHP script

Blue email logo
alengo/Getty Images

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

Part of what makes the PHP mail() function so simple is its lack of flexibility, but one problem with that is that the stock PHP mail() doesn't usually let you use the SMTP server of your choice, and it doesn't support SMTP authentication.

Fortunately, overcoming PHP's built-in shortcomings is not difficult. For most email users, 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.

How to Send Email From a PHP Script With SMTP Authentication

To start, install the PEAR Mail package. Typically, this will have already been done for you with PHP 4 and later, but if you're not sure if you already have it, go ahead and install it.

Copy this code:

<?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>");
 }
?>

Locate all the bold text in our example and change those areas of the script to whatever is relevant to you. Those are the only areas that you must change in order for the PHP script to work, but also be sure to adjust the subject and body text, too.

  • 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 username (typically the same as the username used to retrieve mail)
  • password: The password for SMTP authentication

Note: The above example is of a PHP script that sends an email with SMTP authentication but without SSL encryption. If you want encryption as well, use this script instead, again, swapping the bold text with your information.

<?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>");
 }
?>
Was this page helpful?