In this tutorial, I am gonna show you how to Send Email in PHP using MySQL and Bootstrap. In this tutorial, We have a register form in which user creates an account, when clicking on a submit, a message appears on top showing successfully sending of a mail to the user in order to verify for the account he created, he receives a email in his account which he entered, opening the email, gets a verification link to verify for the account, after clicking a link, user will be redirected to the verification page showing verified account.PHP-Mailer Library is used to send mail through your Gmail account from your local server
Create a database with user table
Run this Query in your Database SQL
CREATE DATABASE IF NOT EXISTS login_system;
CREATE TABLE IF NOT EXISTS `tbl_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`pass` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`status` enum('approved','pending') NOT NULL DEFAULT 'pending',
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
Create html form with four input fields like username, email ,password and confirm password
<form class="form-horizontal contactform" action="index.php" method="post" name="f">
<input type="text" placeholder="Your Name" id="uname" class="form-control" name="uname">
<label class="col-lg-12 control-label" for="uemail">Email:
<input type="text" placeholder="Your Email" id="uemail" class="form-control" name="uemail">
<label class="col-lg-12 control-label" for="pass1">Password:
<input type="password" placeholder="Password" id="pass1" class="form-control" name="pass1">
<label class="col-lg-12 control-label" for="pass1">Confirm Password:
<input type="password" placeholder="Password" id="pass2" class="form-control" name="pass2">
<button class="btn btn-primary" type="submit" name="sub">Submit</button>
Code Showing Server Side Form Validation Using PHP
if (isset($_POST["sub"])) {
$name = trim($_POST["uname"]);
$pass = trim($_POST["pass1"]);
$email = trim($_POST["uemail"]);
$sql = "SELECT COUNT(*) AS count from tbl_users where email = :email_id";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":email_id", $email);
$result = $stmt->fetchAll();
if ($result[0]["count"] > 0) {
$msg = "Email already exist";
$sql = "INSERT INTO `tbl_users` (`name`, `pass`, `email`) VALUES " . "( :name, :pass, :email)";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":name", $name);
$stmt->bindValue(":pass", md5($pass));
$stmt->bindValue(":email", $email);
$result = $stmt->rowCount();
$lastID = $DB->lastInsertId();
$msg = "User registered successfully";
$msg = "Failed to create User";
} catch (Exception $ex) {
Send mails with your Google account from localhost using PHP-Mailer
**Configure in index.php file with your Gmail Id and Password as Shown Below*
// set your username here
$mail->Username = 'youremail@gmail.com';
$mail->Password = 'your password';
// include the phpmailer class file
require_once "phpmailer/class.phpmailer.php";
// my message to send to the user
$lastID = $DB->lastInsertId();
<title>Email Verification</title>
$message .= '<h1>Hi ' . $name . '!</h1>';
$message .= '<p><a href="'.SITE_URL.'activate.php?id=' . base64_encode($lastID) . '">CLICK TO ACTIVATE YOUR ACCOUNT</a>';
$message .= "</body></html>";
// php mailer code starts
$mail = new PHPMailer(true);
// telling the class to use SMTP
// enable SMTP authentication
// sets the prefix to the server
$mail->SMTPSecure = "ssl";
// sets GMAIL as the SMTP server
$mail->Host = "smtp.gmail.com";
// set the SMTP port for the GMAIL server
// set your username here
$mail->Username = 'youremail@gmail.com';
$mail->Password = 'your password';
$mail->Subject = trim("Email Verifcation - www.techsawesome.in");
$mail->SetFrom('youremail@gmail.com', 'Your Name');
$mail->AddAddress($email);
$mail->MsgHTML($message);
} catch (Exception $ex) {
echo $msg = $ex->getMessage();
Verify User Account With Verification Link
if (isset($_GET["id"])) {
$id = intval(base64_decode($_GET["id"]));
$sql = "SELECT * from tbl_users where id = :id";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":id", $id);
$result = $stmt->fetchAll();
if (count($result) > 0) {
if ($result[0]["status"] == "approved") {
$msg = "Your account has already been activated.";
$sql = "UPDATE `tbl_users` SET `status` = 'approved' WHERE `id` = :id";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":id", $id);
$msg = "Your account has been activated.";
$msg = "No account found";
} catch (Exception $ex) {
Configuration Here: index.php file(Enter Your Details)
**Important Step** :
- Login in to your Gmail account through which you want to send email.
- Go to Manage your Google Account Section.
- Go to Security section on left Sidebar.
- Go to Less Secure app Access Section.
- Turn Allow Less Secure App to ON.
Download Full Code Here:
No comments: