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

Related Posts Plugin for WordPress, Blogger...
  • Digital Lynx
  • http://tech.chitgoks.com tech

    cool, ill check that one out. thanks

  • http://www.siliguri.co.in Kamal

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

  • http://tech.chitgoks.com tech

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

  • http://www.smelser.net/blog/post/2010/01/01/Follow-the-chain-of-command.aspx Quinton Gesualdi

    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.

  • hamed

    where should i understand that it is safe?