postimg
Apr 2009 15

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

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
 
");
}
?>

2 comments

  1. Garrett says:

    Worked like a charm. Thanks!

  2. Aneeq says:

    To send HTML mail in PHP, you need to use some additional headers. Below is the code to send HTML mail in PHP.

    $to = “recipient@domain.com“;
    $subject= “Subject of email”;
    $message= “This is HTML mail.”;
    $fromName = “Name of the sender”;
    $fromEmail = “sender@domain.com“;

    // To send HTML mail, the Content-type header must be set
    $headers = “MIME-Version: 1.0? . “\r\n”;
    $headers .= “Content-type: text/html; charset=iso-8859-1? . “\r\n”;

    // Additional headers
    $headers .=”From: $fromName ”.”\r\n”;

    //Mail function
    mail($to, $subject, $message, $headers);

    Source:
    http://http://phphelp.co/2012/05/03/how-to-send-html-mail-in-php//

    OR

    http://http://addr.pk/a1c4

Leave a comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes