Sending HTML Mail via SMTP with PHP Pear Mail

Image for Sending HTML Mail via SMTP with PHP Pear Mail from

By Andrew McCombe
May 30, 2012

Pear Mail Mime Example

Here's how to send html email via remote SMTP using PHP and Pear Mail / Mime.

Installing Pear Mail

To install the classes you should enter (as a superuser):

pear upgrade
pear install Mail
pear install Mail_Mime
pear install Net_SMTP

Pear Mail Mime Example

The following code shows how to send an HTML mail using Pear classes over SMTP.

 * send_email
 * Sends mail via SMTP
 * uses Pear::Mail
 * @author Andrew McCombe &lt;[email protected]>
 * @param string $to Email address of the recipient in 'Name &lt;email>' format
 * @param string $from Email address of sender
 * @param string $subject Subject of email
 * @param string $body Content of email
 * @return boolean if false, error message written to error_log
function send_email($to, $from, $subject, $body) {
        require_once "Mail.php";
        require_once "Mail/mime.php";    

        $host = "";

        $headers = array (
                'From' => $from,
                'To' => $to,
                'Subject' => $subject

        $mime = new Mail_mime();

        $body = $mime->get();
        $headers = $mime->headers($headers);

        $smtp = Mail::factory('smtp', array ('host' => $host));
        $mail = $smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
                return false;
        } else {
                return true; 

$body = '

  Test Mail

<p style="color: red">
  This is a test

send_email('John Doe <John>', 'Bob Smith <bob>', 'Test HTML message', $body);