Send PHP mail via SMTP


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.

When should you use this option?

You should only use this option if you're creating a custom built mail form. If you're using software such as WordPress, you should use the built-in tools or plugins to send via SMTP instead.

Configuring PEAR to send mail

The following steps walk you through how to enable this:

  1. Visit the PEAR article to install PEAR on your web server.
  2. Install the necessary PEAR Mail packages. Visit PEAR Troubleshooting for instructions. For example, you'll need the Mail and Net_SMTP packages.
    [server]$ pear install --alldeps Mail
  3. Create a PHP file that uses PEAR to send mail via SMTP.

Configuring PHP to send SMTP mail

The following example can be used to send SMTP mail using the PEAR Mail package. Create a file named something like phpmail.php with the following code.

You only need to update the fields which are highlighted.

The Mail.php file is located in the /home/username/pear/share/pear directory. It was created when you installed the Mail package and is being referenced from the code you placed in your phprc file from when you installed Pear.


error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

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

DreamHost email

If you’re using DreamHost's email service, the $host value is the same as shown in the code above:

$host = "ssl://";


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.

Still not finding what you're looking for?