Skip to content

Sending HTML Mail via SMTP with PHP Pear Mail

by Andrew McCombe on May 30th, 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.

<?php
/**
 * send_email
 * Sends mail via SMTP
 * uses Pear::Mail
 * @author Andrew McCombe <andrew@iweb.co.uk>
 * 
 * @param string $to Email address of the recipient in 'Name <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 = "smtp.yourhost.com";
 
        $headers = array (
                'From' => $from,
                'To' => $to,
                'Subject' => $subject
        );
 
        $mime = new Mail_mime();
        $mime->setHTMLBody($body);
 
        $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 = '<h1>Test Mail</h1><p style="color: red">This is a test</p>';
 
send_email('John Doe <john@doe.com>', 'Bob Smith <bob@aol.com>', 'Test HTML message', $body);

From → PHP

5 Comments
  1. karuppu permalink

    Thank you very much it’s worked for me.

  2. Michael Heliker permalink

    As a side note, I had to use “@” to avoid errors in php 5.3.x to get this to work for me. Without that, I got a LONG list of errors that would prevent the email from being sent.

    @require_once “Mail.php”;
    @require_once “Mail/mime.php”;
    function send_email($to, $from, $subject, $body) {

    $host = “smtp.domain.com”; // changed in my file

    @$headers = array (
    ‘From’ => $from,
    ‘To’ => $to,
    ‘Subject’ => $subject
    );

    @$mime = new Mail_mime();
    @$mime->setHTMLBody($body);

    $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;
    }
    }

  3. function call now changed to send_email(). Thanks Scott.

  4. Just a note to save some people trouble (or to get the poster to fix). The defined function is “send_email” the used function is “send_mail”. One of those needs a name change to match the other or the whole thing will fatal error out.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

euperia.com / Website Engineer located at , Midlands, UK . Reviewed by Andrew McCombe rated: 1 / 5