Practical No.
14
Program Code : Write a program to send and receive mail using PHP
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Send Email</title>
</head>
<body>
<form class="" action="send.php" method="post">
Email <input type="email" name="email" value=""> <br>
Subject <input type="text" name="subject" value=""> <br>
Message <input type="text" name="message" value=""> <br>
<button type="submit" name="send">Sendk/button>
</form>
</body>
</html>
Index.php :
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
if (isset($_POST["send"])) {
try {
$mail = new PHPMailer(true);
// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Your Gmail address
$mail->Password = 'dkkfltmzjramvqzs'; // Your Gmail app password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Correct encryption type
$mail->Port = 465;
// Sender and recipient settings
$mail->setFrom('[email protected]', 'Your Name'); // Your Gmail address
$mail->addAddress($_POST["email"]); // Recipient's email address
// Email content
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
$mail->send();
echo "
<script>
alert('Email sent successfully!');
document.location.href = 'index.php';
</script>
";
} catch (Exception $e) {
echo "
<script>
alert('Email could not be sent. Mailer Error: {$mail->ErrorInfo}');
</script>
";
?>
Output :