- click here to download php mailer.
- extract the downloaded zip file.
- rename the extracted file from
PHPMailer-master
toPHPMailer
. - copy the folder from downloaded location to the working directory.
-
First of all, we need to set two factor authentication for our gmail account. click here to set two factor authentication.
-
Now create App password
-
click on on App Password. login to your account.
-
A window will open.
-
from the select app menu, select other and give name as you want. I am giving it
PhpMailer
. -
click on
GENERATE
button.
Window as above containing password will appear. copy the password and keep it safe for further use.
<title>Send Mail</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
/>
<div class="container m-5 d-flex justify-content-center">
<div class="col col-5">
<?php
session_start();
if (isset($_SESSION['status']) && $_SESSION['status'] == "email sent successfully") { ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Successful!</strong> OTP successfully sent to
<?php echo $_SESSION['email']; ?>
</div>
<?php unset($_SESSION['status']);
}
?>
<h1 class="text-center">New Email</h1>
<form action="sendmail.php" method="POST">
<div class="mb-3">
<label for="email" class="form-label">To</label>
<input
type="email"
class="form-control"
id="email"
name="email"
aria-describedby="emailHelp"
/>
</div>
<div class="mb-3">
<label for="subject" class="form-label">Subject</label>
<input
type="text"
class="form-control"
name="subject"
id="subject"
aria-describedby="helpId"
placeholder=""
/>
</div>
<div class="mb-3">
<label for="body" class="form-label">Body</label>
<textarea
class="form-control"
name="body"
id="body"
rows="5"
></textarea>
</div>
<button type="submit" class="btn btn-primary" name="submit">
send
</button>
</form>
</div>
</div>
In this case our file is sendmail.php
.
<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "[email protected]"; // your own email address from which you want to send email
$mail->Password = ""; // your app password generated previously.
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
if (isset($_POST['submit'])) {
$mail->setFrom('[email protected]'); // your own email, same as the username
$mail->addAddress($_POST['email']); // getting email from form.
$mail->isHTML(true);
$mail->Subject = $_POST['subject']; // getting subject form form
$mail->Body = $_POST['body']; // getting body from form
$mail->send();
$_SESSION['status'] = "email sent successfully";
$_SESSION['email'] = $_POST['email'];
} else {
echo "Please try again";
}
header("location:index.php");
?>