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:
    <?php
    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:
    wget http://pear.php.net/go-pear.phar
    
    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:
    1.   <?php
    2.   require_once 'Mail.php'; //this loads up PEAR Mail
    3.   if (!class_exists('Mail')) { die('Error: The PEAR Mail class does not exist.'); }
    4.   
    5.   $host = 'ssl://smtp.gmail.com';
    6.   $username = 'example@gmail.com'; //replace this with your new Gmail address
    7.   $password = 'your_password'; //replace this with your new Gmail account password
    8.   $port = '465';
    9.   
    10.  $from_email = $username;
    11.  $from = '"Your Name" <'.$from_email.'>'; //put your name in here, but keep the double quotes
    12.  $to = '"Test Name" <test@example.com>'; //put in your name and main email address to send a test to
    13.  $subject = 'Test Message';
    14.  $body = 'If you’re reading this, our test worked.';
    15.  
    16.  $headers = array(
    17.  	'From' => $from,
    18.  	'To' => $to,
    19.  	'Subject' => $subject,
    20.  	'Reply-To' => $from_email,
    21.  	'MIME-Version' => 1,
    22.  	'Content-type' => 'text/html;UTF-8'
    23.  );
    24.  
    25.  $params = array(
    26.  	'host' => $host,
    27.  	'port' => $port,
    28.  	'auth' => true,
    29.  	'username' => $username,
    30.  	'password' => $password
    31.  );
    32.  
    33.  $smtp = Mail::factory('smtp', $params);
    34.  echo '<p>Preparing to send the test message...</p>';
    35.  
    36.  $mail = $smtp->send($to, $headers, $body);
    37.  
    38.  if (PEAR::isError($mail)) {
    39.  	echo '<p>Error! '.$mail->getMessage().'</p>';
    40.  } else {
    41.  	echo '<p>Your test message was sent successfully.</p>';
    42.  }
    43.  
    44.  ?>
    
  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.


Comments

Loading…

This post 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.