How do I send PHP mail via SMTP?


On Jan 19, 2019, released statement that their web server had been compromised and that a tainted go-pear.phar file was discovered. If you had previously downloaded the go-pear.phar file to install PEAR, you should check to confirm its authenticity using the following article:

When you use the PHP mail function, you are sending email directly from your web server. This can cause issues if the FROM address isn’t set properly or if your email isn’t hosted with DreamHost. Sending mail via SMTP is recommended as email is sent from the mail server rather than the web server. View the PHP mail troubleshooting article for details.

There are a few options to send PHP mail via SMTP. For example:

  • Using PHPmailer
  • Using the PEAR Mail package.

This article explains how to use the PEAR option.

Configuring PEAR to send mail

At this time, is down, so the normal install commands do not function.

To install packages, you must download them manually to your server. Visit the following article for instructions.

Configuring PHP to send SMTP mail

The following example can be used to send SMTP mail using the PEAR Mail package.

You only need to update the fields which are in bold.

Finding your mail hostname

Make sure to use for the hostname if you're sending from an email hosted at DreamHost.


error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

set_include_path("." . PATH_SEPARATOR . ($UserDir = dirname($_SERVER['DOCUMENT_ROOT'])) . "/pear/php" . PATH_SEPARATOR . get_include_path());
require_once "Mail.php";

$host = "ssl://";
$username = "";
$password = "your email password";
$port = "465";
$to = "";
$email_from = "";
$email_subject = "Subject Line Here: " ;
$email_body = "whatever you like" ;
$email_address = "";

$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);

if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } ?>

If you’re using Google to host your email, you must change the $host line to the following:

$host = "ssl://";

Allowing GMAIL to send from your mail application

If you're using your GMAIL address to send via SMTP, you must first allow your application access to your GMAIL address. If you do not do this, your email will not authenticate and not send. View the following article for details:

See also

Did this article answer your questions?

Article last updated PST.