How do I send emails to users using PHP?

Aishwarya Survase
4 min readJun 2, 2021

I have got one mini-project related to the hill station booking system. I went through the requirements and drawn DFD hot the flow works that makes us easy to implement the hill station booking system. This website includes features such as the Home tab show attractive images of the hill station. About us gives more defined information about the company. The gallery tab shows the hill stations present only from Maharashtra. It has a zoom feature for a picture. The tours tab gives us to search for an appropriate destination and season at a low price. Blog tab indicates that people visited from tours they like to write a review for that destination and services provided by us. Contact is the tab for people to reach out to the company and solve the queries. It has a Google Maps location feature where customers can easily reach out. The registration tab is to maintain data at the admin side for communication. The user gets a successful login message in his mail after login and who should be already registered. I want to share the email sending feature. I hope others will help with it.

Hill station website
About US
Gallery include pictures of hill stations from Maharashtra
Search tours

I have used this email sending feature for both registrations and login successful messages.

Registration form
Registration Successful message
Downloaded PHPMailer-master
Path of Email
Code of email
Registration form
Registration Successful message
login form
login successful message

To integrate email service in the project. We have to download PHPMailer-master from google chrome extract the folder where our webpages are present. Use snippet code from the internet for email integration.

We should follow some changes such as path for PHPMailer-master, sender, receiver, subject, and message. The next step is that the email id we are using that email is less secure feature must be on. Otherwise, the message won’t be sent. If the issue is there for the less secure on /off option then first remove the 2 step authentication feature then it will definitely work.

Email code Snippet.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("recepient1@example.com", "Recepient Name");
$mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("reply@yourdomain.com", "Reply");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
$mail->send();
echo "Message has been sent successfully";
} catch (Exception $e) {
echo "Mailer Error: " . $mail->ErrorInfo;
}

--

--

Aishwarya Survase

I am a PHP web developer. Interested to share knowledge about it.