PHP How To Send HTML Email with SMTP Authentication

Here’s a code snippet on how to send HTML email with SMTP authentication using PHP. You would have to download a library called PHPMailer as the mail() function of PHP does not support SMTP authentication. There is also other class that must be in the same location as class.phpmailer.php in order for the mail sending to work, smtp.phpmailer.php.

require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Username = "username"; // your SMTP username
$mail->Password = "password"; // your SMTP password
$mail->Host = "smtp.host.url"; // SMTP server
$mail->From = $from;
$mail->AddAddress($to);

$mail->Subject = $subject;
$mail->Body = $body;

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails

5 Responses to “PHP How To Send HTML Email with SMTP Authentication”

  1. 1
    Digital Lynx Says:

    You might also want to try the mMail library found here:

    http://www.virtualthinking.com/loadhtml.php?where=scripts&what=art_show.php&db_target=00000000023

  2. 2
    tech Says:

    cool, ill check that one out. thanks

  3. 3
    Kamal Says:

    But where will I get the “class.phpmailer.php”

  4. 4
    tech Says:

    you can google it kamal. there should easily be lots of results for it.

  5. 5
    Quinton Gesualdi Says:

    Very good blog post, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.

Leave a Reply

Spam protection by WP Captcha-Free