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.
PHP Code using PHPMailer send simple mail.
require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$address = "demo@gmail.com";
$mail->AddReplyTo("demo@gmail.com","demo");
$mail->SetFrom('demo@gmail.com', 'demo');
$mail->AddReplyTo("demo@gmail","replay");
$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";
$mail->MsgHTML($body);
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}