Sending HTML Mail via SMTP with PHP Pear Mail

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 Mail Mime Example

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

  • Inderjeet singh

    thanks dude, You are life saver

  • karuppu

    Thank you very much it’s worked for me.

  • Michael Heliker

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

    • Just out of inerest, what errors do you get? Can you post them?

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

  • 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.