Follow us on

banner image

Send Email in PHP

 

Send-email-in-php-techs-awesome
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;
USE 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',
PRIMARY KEY (`id`)
) 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">
<fieldset>
<div class="form-group">
<label>Name:
<input type="text" placeholder="Your Name" id="uname" class="form-control" name="uname">
</label>
</div>
<div class="form-group">
<label class="col-lg-12 control-label" for="uemail">Email:
<input type="text" placeholder="Your Email" id="uemail" class="form-control" name="uemail">
</label>
</div>
<div class="form-group">
<label class="col-lg-12 control-label" for="pass1">Password:
<input type="password" placeholder="Password" id="pass1" class="form-control" name="pass1">
</label>
</div>
<div class="form-group">
<label class="col-lg-12 control-label" for="pass1">Confirm Password:
<input type="password" placeholder="Password" id="pass2" class="form-control" name="pass2">
</label>
</div>
<div class="form-group">
<div class="col-lg-10">
<button class="btn btn-primary" type="submit" name="sub">Submit</button>
</div>
</div>
</fieldset>
</form>


Code Showing Server Side Form Validation Using PHP

<?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";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":email_id", $email);
$stmt->execute();
$result = $stmt->fetchAll();
if ($result[0]["count"] > 0) {
$msg = "Email already exist";
$msgType = "warning";
} else {
$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);
$stmt->execute();
$result = $stmt->rowCount();
if ($result > 0) {
$lastID = $DB->lastInsertId();
$msg = "User registered successfully";
$msgType = "success";
} else {
$msg = "Failed to create User";
$msgType = "warning";
}
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
?>

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';


<?php
// include the phpmailer class file
require_once "phpmailer/class.phpmailer.php";
// my message to send to the user
$lastID = $DB->lastInsertId();
$message = '<html><head>
<title>Email Verification</title>
</head>
<body>';
$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
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;
// 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
$mail->Port = 465;
// set your username here
$mail->Username = 'youremail@gmail.com';
$mail->Password = 'your password';
// set your subject
$mail->Subject = trim("Email Verifcation - www.techsawesome.in");
// sending mail from
$mail->SetFrom('youremail@gmail.com', 'Your Name');
// sending to
$mail->AddAddress($email);
// set the message
$mail->MsgHTML($message);
try {
$mail->send();
} 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";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":id", $id);
$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) > 0) {
if ($result[0]["status"] == "approved") {
$msg = "Your account has already been activated.";
$msgType = "info";
} else {
$sql = "UPDATE `tbl_users` SET `status` = 'approved' WHERE `id` = :id";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":id", $id);
$stmt->execute();
$msg = "Your account has been activated.";
$msgType = "success";
}
} else {
$msg = "No account found";
$msgType = "warning";
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
}

Configuration Here: index.php file(Enter Your Details)

Send-email-in-php-techs-awesome

**Important Step** : 

  1. Login in to your Gmail account through which you want to send email.
  2. Go to Manage your Google Account Section.
  3. Go to Security section on left Sidebar.
  4. Go to Less Secure app Access Section.
  5. Turn Allow Less Secure App to ON.

Download Full Code Here:


Password : 123456

Send Email in PHP Send Email in PHP Reviewed by Techs Awesome on November 12, 2020 Rating: 5

No comments:

Adsense 728x90

Powered by Blogger.