First of all, what is the main difference between PHPMailer and standard php mail() function? The answer is very simple PHPMailer supports attachments, multiple receipts, SMTP authentication and lots of other functions.

Please note that this tutorial only demonstrates how to use PHPMailer package, before trying the implement the code, make sure PHPMailer package is properly installed on your server.

Using the script

Following example demonstrates the basic use of the phpmailer class:

include_once("class.phpmailer.php"); //make sure this path is correct
$test_mail = new PHPMailer;
$test_mail->ClearAddresses();
$test_mail->AddAddress('[email protected]', 'Tester');
$test_mail->From='[email protected]';
$test_mail->FromName='Paul';
$test_mail->Subject='test subject';
$test_mail->isHTML=true; //true or false if plain text only
$test_mail->Body='Some text';

PHPMailer is a great tool when emailing a group of the users, or/and adding several attachments by using foreach() statement.

Adding an attachement

$test_mail->AddAttachement('full_path','name');

Full_path as the name suggests it's a full path to the file, name may be an actual file name or the name that file will appear as/be renamed to after the email has been sent and received by recipient's email client . You may also specify additional parameters such as encoding (base64 by default) and type of the file. However I advise to leave this setting alone, and let the recipient's email client to figure it out.

Finally once the setting are correct, and attachement are included (if any), we have to call send() function that sends out the email

If($test_mail->Send()) {
echo "Message sent";
else {
echo $test_mail->ErrorInfo;
}