Sending Email Using PHPMailer
Lets learn how to send email (HTML or plain text) using PHPMailer. It’s easier and simpler than what you think. Before using this mailer class, I have tried using PEAR Mail. PEAR Mail is not as good as PHPMailer. Anyway, PHPMailer is much more flexible than PEAR Mail because not all hosting provider provide this PEAR Mail. As another reason, we can’t attach file using PEAR Mail. Therefore, PHPMailer is the best class for you to send simple to complex email with attachment. PHPMailer is absolutely free to use.
Lets start using this PHPMailer
Download PHPMailer Package
As the first step, you need to download PHPMailer package. PHPMailer package can be downloaded for free here. Copy the package into your project folder. Please read the installation guide to know how to place this package.
Start Coding
[php]<?
// By : Isusx Programming Corner
// URL : http://isusx.com
//Include PHPMailer class in our script
require("class.phpmailer.php");
//Create a new object of the PHPMailer
$mail = new PHPMailer();
//Assigning Parameters
//Set the sender email
$mail->From = "username@domainname.com";
//Set the sender name
$mail->FromName = "Your Name";
//Set the email destination (can multiple)
$mail->AddAddress("destination1@domain.com", "name1");
$mail->AddAddress("destination2@domain.com", "name2");
$mail->AddAddress("destination3@domain.com", "name3");
//Add CC (optional, can multiple)
$mail->AddCC("cc1@domain.com", "name1");
$mail->AddCC("cc2@domain.com", "name2");
$mail->AddCC("cc3@domain.com", "name3");
//Add BCC (optional, can multiple)
$mail->AddBCC("cc1@domain.com", "name1");
$mail->AddBCC("cc2@domain.com", "name2");
$mail->AddBCC("cc3@domain.com", "name3");
//Add Reply To (optional, can multiple)
$mail->AddReplyTo("reply1@domain.com", "name1");
$mail->AddReplyTo("reply2@domain.com", "name2");
$mail->AddReplyTo("reply3@domain.com", "name3");
//Set the email subject
$mail->Subject = "Email Subject";
//You need to specify these attributes if
//your mail server requires authentication
//Else, please skip the following code
//Tells PHPMailer to use SMTP to send email
$mail->IsSMTP();
//Indicate that your mail server require authentication
$mail->SMTPAuth = true;
//Set the mail server dns name
$mail->Host = "mailserver.com";
//Set the port used to send email (default port is 25)
$mail->Port = "25";
//Set the username to access the mail server
$mail->Username = "username";
//Set the password to access the mail server
$mail->Password = "password";
//End of authentication section
//Content of the email (Choose one of these 2 options)
//First option - Sending plain text email
$mail->Body = "Email Content \n\nBla..Bla..Bla…";
//End of first option code
//Second option - Sending HTML email
$mail->Body = "Dear, <b>target name </b>! <br /><br />Just try sending email!";
//This text will be displayed when
//the client’s email reader does not support HTML
$mail->AltBody = "Your email reader does not support HTML.";
//Tell PHPMailer that your email contain HTML
$mail->IsHTML(true);
//End of second option code
//Adding attachment (Optional)
$mail->AddAttachment("c:/filename.zip", "newfilename.zip");
//Note that you can add multiple attachment just by calling
//the AddAttachment method again and again
//Send The Email
if(!$mail->Send()){
echo ‘Message was not sent.’;
echo ‘Mailer error: ‘ . $mail->ErrorInfo;
}else
echo ‘Message has been sent.’;
?>[/php]
Related Links
PHPMailer homepage
php mail() function reference
Standard E-mail RFC 822
MIME E-mail RFC 2046
Email Standards Project




