How to send mail in PHP using PHPMailer

PHPMailer is one of the most popular open source PHP libraries to send emails.

Although it was released in early 2000s, it is now a very commonly adopted approach among developers for sending emails in PHP.

In this article we have talk about why you should use PHPMailer instead of PHP mail() function and we have show some code samples on how to use this library.

You can add multiple attachments to the email by adding addAttachment() method multiple times.

PHPMailer is also used by popular PHP content management systems like WordPress, Drupal, Joomla etc.

Download PHPMailer Library.

PHP Code using PHPMailer send simple mail.


$mail  = new PHPMailer(); 

$address = "";

$mail->SetFrom('', 'demo');
$mail->AddAddress($address, "Demo");
$mail->Subject    = "PHPMailer Test sEND MAIL";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
$body  = "Mail body in HTML";
	echo "Mailer Error: " . $mail->ErrorInfo;
	echo "Message sent!";

Leave a Reply

Your email address will not be published. Required fields are marked *