-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.php
77 lines (60 loc) · 2.33 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$errors = [];
$errorMessage = ' ';
$successMessage = ' ';
if (!empty($_POST))
{
$name = $_POST['firstName'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name)) {
$errors[] = 'Name is empty';
}
if (empty($email)) {
$errors[] = 'Email is empty';
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email is invalid';
}
if (empty($message)) {
$errors[] = 'Message is empty';
}
if (!empty($errors)) {
$allErrors = join ('<br/>', $errors);
$errorMessage = "<p style='color: red; '>{$allErrors}</p>";
header("Location: contact.html?error=" . urlencode($allErrors));
exit;
} else {
$fromEmail = 'hello@musicintheneighbourhood.com';
$emailSubject = 'Hello from the Music in the Neighbourhood Society!';
// Create a new PHPMailer instance
$mail = new PHPMailer(exceptions: true);
try {
// Configure the PHPMailer instance
// Initialize and configure the PHPMailer instance
$mail->isSMTP();
$mail->Host = 'live.smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls'; // or 'tls'
$mail->Port = 587;
$mail->Username = 'api';
$mail->Password = 'b3c10492b779df1b1afb02c066e94eea';
// Set the sender, recipient, subject, and body of the message
$mail->setFrom($fromEmail, 'Music in the Neighbourhood'); // Set the sender email and name
$mail->addAddress($email); // The recipient’s email address
$mail->Subject = $emailSubject;
$mail->isHTML(true); // Corrected syntax for isHTML
$mail->Body = "<p>Hey {$name},</p><p>Thanks for contacting us! A member of our team will be in touch with you soon.</p>";
// Send the message
$mail->send () ;
$successMessage = "<p style='color: green; '>Thank you for contacting us :) A member of our team will be in touch with you shortly.</p>";
echo $successMessage;
} catch (Exception $e) {
$errorMessage = "<p style='color: red; '>Oops, something went wrong. Please try again later</p>";
echo $errorMessage;
}
}
}
?>