In this tutorial, I will show you how you can use PHPMailer library and speed up the mail sending. To make it easier we will be using Gmail SMTP to deliver our mail.
To continue in this you will need to save the following files with given code in the document below
- PHPMailer Library
This file contains the imports to PHPMailer library as well as all the configurations that are needed for mail server in order to deliver the emails efficiently. In this file, we set all the attributes that are needed to send a mail and we configure the Gmail SMTP server with the configuration as needed and when the code is executed, it will make use of the PHPMailer library to send the file.
$email = new PHPMailer();
$email->CharSet = “utf-8”;
$email->Username = “email@example.com”;
$email->Password = “your_gmail_password”;
$email->SMTPSecure = “ssl”;
$email->Host = “smtp.gmail.com”;
$email->Port = “587”;
$email->setFrom(‘firstname.lastname@example.org’, ‘your name’);
$email->AddAddress(‘email@example.com’, ‘receivers name’);
$email->Subject = ‘PHPMailer Mailing API Test’;
$email->Body = ‘Hi,
Email was generated using PHPMailer with Google SMTP
Welcome to PHPMailer 😀 :)’;
echo “Email Successfully sent”;
echo “Error in Sending Mail”.$email->ErrorInfo;
This library contains some built-in functions that are used to deliver emails easily.
You need to make sure that you always use an actual server in order to try the code out because in localhost most of the time SendMail functions do not work.
You can download the completing working source code here,
I hope I was able to help you with setting up Send E-Mail function using PHPMailer and Gmail SMTP.
Let me know your experience with the above functions in the comment below.