-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_contact.php
63 lines (53 loc) · 1.63 KB
/
submit_contact.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require "vendor/autoload.php";
$mail = new PHPMailer(true);
$name = $_POST["name"] ?? null;
$email = $_POST["email"] ?? null;
$message = $_POST["message"] ?? null;
if (!$name || !$email || !$message) {
echo "Please fill in all the required fields!";
exit();
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email address!";
exit();
}
if (strlen($message) < 50) {
echo "Message should be at least 50 characters long!";
exit();
}
try {
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "marufsarkercse2022@gmail.com";
$mail->Password = "mradurptsatagwkp";
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
//Recipients
$mail->setFrom("marufsarkercse2022@gmail.com", "Green Garden");
$mail->addAddress("marufsarkercse2022@gmail.com", "$name");
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = "Green Garden Contact Form Submission";
$mail->Body = "Name: $name <br> Email: $email <br> Message: $message";
// Send email
if ($mail->send()) {
$_SESSION["message"] = "Message sent successfully!";
} else {
$_SESSION["message"] = "Message could not be sent!";
}
// echo 'Message has been sent';
$name = "";
$email = "";
$message = "";
header("Location: index.php");
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
header("Location: contact.php");
exit();
}