Send Email via SMTP from PHP Using PEAR

Sending email using PHP’s built-in mail() function is easy, but has a lot of limitations. The biggest issue is that your messages will end up in the spam folder most of the time, because unauthenticated emails sent this way are so easy to spoof that email providers distrust them, even when they are legitimate.

After weighing the pros and cons of various alternatives, I decided on sending through Gmail’s SMTP server using PEAR’s Mail package. The process isn’t difficult, but there are a lot of steps. I figured I’d write them down in case anyone else is trying to figure them out (or in case I need to do it again on another server some day).

  1. First, sign up for a Gmail account. You can use your own domain with Google Apps for Business. The service is inexpensive (free if you’re not using a custom domain) and the rate limits are generous, so this seems like an obvious way to go.
  2. Check to see if you have PEAR installed already by creating a phpinfo file:
    echo phpinfo();
    If so, you can skip ahead to step #12.
  3. To install PEAR, login to your web server via SSH and navigate to your home directory. Then, execute this command:
    Note: This step and the next few came from Dreamhost’s instructions for installing PEAR with PHP 5.6.
  4. Once it downloads, run it:
    /usr/local/php56/bin/php go-pear.phar
  5. When it asks if you’d like to edit your php.ini file, say no. (We’ll do that later.)
  6. Add this to the bottom of your .bash_profile file:
    export PHP_PEAR_PHP_BIN=/usr/local/php56/bin/php
    export PATH=${HOME}/pear/bin:/usr/local/php56/bin:${PATH}
  7. Next, add this to the bottom of your .bashrc file:
    source .bash_profile
  8. Now, edit your php.ini file, adding this:
    include_path = ".:/home/username/pear/share/pear"
    Be sure to replace username there with your username.
  9. Restart Apache, then go back to your phpinfo() page from step #2 and confirm that you see the new include_path.
  10. Now that core PEAR is installed, install the Mail package:
    pear install --onlyreqdeps Mail
  11. You’ll need to also install the Net_SMTP package:
    pear install Net_SMTP
  12. Everything should be installed now. To test, send yourself an email message. Create a PHP file with the following code:
    require_once 'Mail.php'; //this loads up PEAR Mail
    if (!class_exists('Mail')) { die('Error: The PEAR Mail class does not exist.'); }
    $host = 'ssl://';
    $username = ''; //replace this with your new Gmail address
    $password = 'your_password'; //replace this with your new Gmail account password
    $port = '465';
    $from_email = $username;
    $from = '"Your Name" <'.$from_email.'>'; //put your name in here, but keep the double quotes
    $to = '"Test Name" <>'; //put in your name and main email address to send a test to
    $subject = 'Test Message';
    $body = 'If you’re reading this, our test worked.';
    $headers = array(
    	'From' => $from,
    	'To' => $to,
    	'Subject' => $subject,
    	'Reply-To' => $from_email,
    	'MIME-Version' => 1,
    	'Content-type' => 'text/html;UTF-8'
    $params = array(
    	'host' => $host,
    	'port' => $port,
    	'auth' => true,
    	'username' => $username,
    	'password' => $password
    $smtp = Mail::factory('smtp', $params);
    echo '<p>Preparing to send the test message...</p>';
    $mail = $smtp->send($to, $headers, $body);
    if (PEAR::isError($mail)) {
    	echo '<p>Error! '.$mail->getMessage().'</p>';
    } else {
    	echo '<p>Your test message was sent successfully.</p>';
  13. After browsing to that new page, you should have a new email waiting in your inbox. If not, check the stumbling points below.
  14. One last thing: Be sure to delete your phpinfo() file. You don’t want to leave that out where someone could find it.

There were a couple stumbling points I hit. I’ll point them out in case you’re running into the same thing:

  • Problem: The script ends unexpectedly when you try to send the email with no error message.

    Solution: It’s likely that your PEAR Mail class isn’t installed correctly, so when you try to run $smtp->mail, an error occurs but isn’t being displayed. To get more info on the specific error, add this to the top of your test PHP file (so that the error will be displayed), then try again, and fix the error that’s listed:
    error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
  • Problem: You’re getting a “Validation failed for” error.

    Solution: There are probably invalid characters in your to or from email alias. Either remove the invalid characters, or enclose the alias in double quotes. (See lines 11 and 12 in step #12 above.)
  • Problem: The email is going out as plain text, not HTML.

    Solution: Add a MIME-Version and Content-type to your email headers. See lines 21 and 22 in step #12 above for an example.

You should be in business now, happily sending out mail via SMTP. But, if not, leave a comment and hopefully someone can help you out with your issue.



This article was published on December 18th, 2016 by Robert James Reese in the following categories: Email and PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.