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.
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.
You may also like How to send mail in PHP using PHPMailer and How to send attachment mail in PHP using PHPMailer.
Download PHPMailer Library.
PHP Code using PHPMailer with all Feature
require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = ""; /*SMTP server*/
$mail->SMTPAuth = true;
$mail->Port = '';
$mail->Username = ""; /*Username*/
$mail->Password = ""; /**Password**/
$address = "demo@gmail.com";
$mail->AddReplyTo("demo@gmail.com","demo");
$mail->SetFrom('demo@gmail.com', 'demo');
$mail->AddReplyTo("demo@gmail","replay");
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->AddAddress($address, "Demo");
$mail->addAddress('ellen@example.com');
$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!";
}