-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.php
More file actions
58 lines (50 loc) · 1.9 KB
/
contact.php
File metadata and controls
58 lines (50 loc) · 1.9 KB
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
<?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 ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Simple spam prevention by limiting the number of messages sent from a single user to 3
$userEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$messageCount = (int) (isset($_COOKIE[$userEmail]) ? $_COOKIE[$userEmail] : 0);
if ($messageCount >= 3) {
http_response_code(400);
echo "You have reached the message limit. Please contact us through a different method.";
exit;
}
// Increment the message count
$messageCount++;
setcookie($userEmail, $messageCount, time() + 86400 * 365); // Store the message count for a year
// Set up PHPMailer
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'jcytfchurch92@gmail.com';
$mail->Password = 'txncdnjbiolxvxvs';
$mail->setFrom('jcytfchurch92@gmail.com', 'JCYTF');
$mail->addAddress($email); // Replace with the receiver's email address
$mail->Subject = $subject;
$mail->Body = "Name: $name\nEmail: $email\nSubject: $subject\n\n$message";
// Send the email
$mail->send();
http_response_code(200);
echo "success";
} catch (Exception $e) {
http_response_code(500);
echo "An error occurred while sending the email. Please try again later.";
}
} else {
http_response_code(405);
echo "Method not allowed.";
}
?>