Using the PHPMailer library to send html emails using a SMTP server

December 15th, 2007 by jason

This is a beginner tutorial on how to create a script to send out html emails using the PHPMailer library. This will be a very simple script, we will edit the mail in the actually code and run that PHP page to send out the mail. At a later stage I will include a tutorial on how to implement a WYSIWYG editor such as FCKEditor. Firstly, what is the PHPMailer library? PHPMailer is a fully featured email transfer class for PHP. It has the following features: send emails with multiple TOs, CCs, BCCs and REPLY-TOs; SMTP support; alternative/plain text sending; word wrap; debugging features; attachment and imbedded image support.

Ok, now we need the PHPMailer library, it is free so don’t worry! You can get it from here: http://phpmailer.codeworxtech.com/ just click on the download tab at the top of the screen. Extract the library, and upload onto your server. Next the simple code we are going to use to send out the mail:
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();$mail->Host = "smtp1.domain.com”;
$mail->SMTPAuth = true;
$mail->Username = "username";$mail->Password = "password";
$mail->From = "from@domain.com";
$mail->FromName = "Domain.com";
$mail->AddAddress("sendto@email.com");
$mail->AddReplyTo("info@domain.com");
$mail->WordWrap = 50;$mail->IsHTML(true);
$mail->Subject = "Your subject";
$mail->Body    = " This is a test";
$mail->AltBody = "plain text message";
if(!$mail->Send()){   echo "Message could not be sent.  ";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;}
echo "Message has been sent";?>

  Ok lets customize this code for your needs. Where you see $mail->Host = “smtp1.domain.com”; put in your SMTP server, and below it where you see “username” and “password” enter your SMTP server username and password.Then where you see $mail->From = “from@domain.com”; put in your email address that you want to send from. To add addresses that you are going to send the mail to put them in the following bit of code $mail->AddAddress(”sendto@email.com”); To send it to more than one person, just ad another bit of code with the other email address just below that. You could put this in a loop as well and get the email addresses out of a database.Next edit the subject where you see $mail->Subject = “Your subject”; and bellow that your HTML email. When sending html emails keep your HTML simple, use tables instead of DIVs and stay away from advanced CSS. This will insure that your message is send the way you want it to look. Bellow the HTML add the alternative plain test copy of the email that will be sent to clients that cant read HTML.  Save this code and run this page, your message should be sent. If you have any questions please feel free to leave a comment. Enjoy!

 

del.icio.us Digg Technorati StumbleUpon

Posted in General Opensource

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.