postimg
Apr 2009 15

Send An HTML & Plain Text Email Using An SMTP Server With PHP & PEAR1

Posted In PHP By Tony Virelli

You have to have PEAR installed. You can get more information at http://pear.php.net.

<?php
include('Mail.php');
include('Mail/mime.php');
 
$text = 'Hello,\nHow are you doing?';
$html = 'Hello,
How are you doing?';
 
$smtpinfo["host"] = "smtp.domain.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "SMTP USERNAME";
$smtpinfo["password"] = "SMTP PASSWORD";
 
$recipients = "recipient@domain.com";
$headers["From"] = "From Name ";
$headers["To"] = "recipient@domain.com";
$headers["Subject"] = "Email Subject";
 
$mime = new Mail_mime();
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
 
$body = $mime->get();
$headers = $mime->headers($headers);
 
$mail =& Mail::factory('smtp', $smtpinfo);
$mail->send($recipients, $headers, $body);
 
if(PEAR::isError($mail)){
echo("
 
There was an error: " . $mail->getMessage() . "
 
");
}else {
echo("
 
Message Sent To $to
 
");
}
?>

1 comment

  1. Garrett says:

    Worked like a charm. Thanks!

Leave a comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes