PHPMailer is a PHP class for sending email. It has far more funtionality compared to the regular mail() function, including attachments and inline images. It is very usefull for actions like "Contactus" forms, not allowing header injection and spamming. Supports SMTP.
How do I get this?
PHPMailer can be downloaded from the official site
. This is also the place where yo can find complete documentation.Mã:http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/PHPMailer%20v5.1/PHPMailer_v5.1.zip/download"]
[B]First steps
After downloading archived files, unzip them and upload to the web server.
Here's a sample for using SMTP. We assume that SMTP requires authorization. If it in't nessesary, just write $mail->SMTPAuth = false;. To use a number of servers use semicolumn for delimiter.
To add inforation about sender, use following functions:Mã:<?php require("class.phpmailer.php"); $mail = new PHPMailer();$mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "localhost"; $mail->SMTPAuth = true; $mail->Username = 'sendmail@tendomain'; //email đã đc tạo trên host $mail->Password = 'smtppassword'; //password cua eamail $mail->AddAddress("email@tendomain.com"); $mail->Subject = "Test 1"; $mail->Body = "Test 1 of PHPMailer."; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { echo "Letter is sent"; }?>
For specifying various types of recepients use these:Mã:$mail->From="sendmail@tendomain"; $mail->FromName="My site's mailer"; $mail->Sender="sendmail@tendomain"; // indicates ReturnPath header $mail->AddReplyTo("replies@example.com", "Replies for my site"); // indicates ReplyTo headers
[/CODE]Mã:$mail->AddAddress("mail1@domain.com", "Recepient 1"); $mail->AddCC("mail1@domain.com", "Recepient 1"); $mail->AddBCC("mail1@domain.com", "Recepient 1");
If you need 2 or more To: addresses, just call.
Additional languages
If the message contains cyrilic or other non-latin characters like unicode, you should specify the charset used:
HTML contentMã:$mail->CharSet="windows-1251"; $mail->CharSet="utf-8";
HTML emails are better formatted and more attractive. As there are mail clients that do not support HTML, you should provide alterntive text only content. Here's a sample:
ImagesMã:<?php require("class.phpmailer.php"); $mail = new PHPMailer();$mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "smtp1.example.com;smtp2.example.com"; $mail->SMTPAuth = true; $mail->Username = 'smtpusername'; $mail->Password = 'smtppassword'; $mail->From="mailer@example.com"; $mail->FromName="My site's mailer"; $mail->Sender="mailer@example.com"; $mail->AddReplyTo("replies@example.com", "Replies for my site"); $mail->AddAddress("email@example.com"); $mail->Subject = "Test 1"; $mail->IsHTML(true); $mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>"; $mail->AltBody="This is text only alternative body."; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { echo "Letter is sent"; } ?>
There are two ways to add images in the HTML content. You can specify absolute address that points image on your site. The case is by using this technique, one can track down who opens the email. That's why most emal clients do not display such images. To code around this, you can attach the image in the message and link to it by a special URI.
----------------------------------------------------------------------------------Mã:<?php require("class.phpmailer.php"); $mail = new PHPMailer();$mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "smtp1.example.com;smtp2.example.com"; $mail->SMTPAuth = true; $mail->Username = 'smtpusername'; $mail->Password = 'smtppassword'; $mail->From="mailer@example.com"; $mail->FromName="My site's mailer"; $mail->Sender="mailer@example.com"; $mail->AddReplyTo("replies@example.com", "Replies for my site"); $mail->AddAddress("email@example.com"); $mail->Subject = "Test 1"; $mail->IsHTML(true); $mail->AddEmbeddedImage('logo.jpg', 'logoimg', 'logo.jpg'); // attach file logo.jpg, and later link to it using identfier logoimg $mail->Body = "<h1>Test 1 of PHPMailer html</h1> <p>This is a test picture: <img src=\"cid:logoimg\" /></p>"; $mail->AltBody="This is text only alternative body."; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { echo "Letter is sent"; } ?>
Attachments
You may need to attach a file to the letter. Use AddAttachment() for that purpose:
Mã:<?php require("class.phpmailer.php"); $mail = new PHPMailer();$mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "localhost"; $mail->SMTPAuth = true; $mail->Username = 'smtpusername'; $mail->Password = 'smtppassword'; $mail->From="mailer@example.com"; $mail->FromName="My site's mailer"; $mail->Sender="mailer@example.com"; $mail->AddReplyTo("replies@example.com", "Replies for my site"); $mail->AddAddress("email@example.com"); $mail->Subject = "Your invoice"; $mail->IsHTML(false); $mail->AddAttachment('files/invoice-user-1234.pdf', 'invoice.pdf'); // attach files/invoice-user-1234.pdf, and rename it to invoice.pdf $mail->Body = "Please find your invoice attached."; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { echo "Letter is sent"; } ?>