PHP has its own built in mail() function which is used to send emails. But it’s not flexible to use for sending emails as there is no guarantee about email delivery and it also take a lot of time to send email. There is one more disadvantage of this mail, that is most of emails goes to SPAM folder. To overcome these problems we have PHPMailer library. PHPMailer is world widely use tool for sending emails. Its very secure and easy to use.
So let’s see how to integrate it on our PHP project.
First of all you need to grab your copy of PHPMailer from their official download page.
(You may also like: Why to Choose PHP?)
Here is a small function that uses PHPMailer class, you can use it easily to send email. If you download my source file that I have given with this article. I have uses two main files. And one folder PHPMailer that basically contain PHPMailer library files. I have deleted all unwanted files from it and only take important files. So here is the way to use PHPMailer Class.
sendmail.php this file contain a simple function which uses PHPMailer to send emails. You have to call this function whenever you want to send email.
function send_mail($to, $body, $subject)
$mail = new PHPMailer;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your gmail email@example.com';
$mail->Password = 'your gmail password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'your gmail firstname.lastname@example.org';
$mail->FromName = 'Your Name';
$mail->addReplyTo('your gmail email@example.com', 'Reply');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
echo 'Message could not be sent.';
echo 'Message has been sent';
index.php here I have included above sendmail.php file so that I can call send_mail() function.
$to = "firstname.lastname@example.org";
$body = "This is message body";
$subject = “This is Subject Example”;
send_mail($to, $body, $subject);
So this was the way to integrate PHPMailer on your own web server. If you have any type of query you can comment below. And if you like this tutorial don’t forget to subscribe us and share it with you friends 🙂