-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.php
47 lines (41 loc) · 1.55 KB
/
send_email.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
<?php
require 'utils/PHPMailer/src/PHPMailer.php';
require 'utils/PHPMailer/src/SMTP.php';
require 'utils/PHPMailer/src/Exception.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'constants.php';
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = SMTP_HOST; // Use constants from config.php
$mail->SMTPAuth = true;
$mail->Username = SMTP_USERNAME; // Use constants from config.php
$mail->Password = SMTP_PASSWORD; // Use constants from config.php
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('[email protected]', "$name");
$mail->addAddress('[email protected]');
$mail->Subject = 'New Contact Form Submission';
$mail->Body = "You have received a new message from the contact form.\n\n" .
"Name: $name\n" .
"Email: $email\n" .
"Message:\n$message";
if ($mail->send()) {
echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}
} catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
}
} else {
echo "Invalid request method.";
echo '<br><br>';
echo '<button onclick="window.location.href=\'index.php\'">Go Back</button>';
}
?>