-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.php
49 lines (39 loc) · 1.31 KB
/
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
<?php
$errors = [];
$errorMessage = '';
if (!empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$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($subject)) {
$errors[] = 'Subject field is empty)';
}
if (empty($message)) {
$errors[] = 'Message is empty, unable to send';
}
if (empty($errors)) {
$toEmail = 'soundwanders@gmail.com';
$emailSubject = 'Helter-Shelter Received a Contact E-mail';
$headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];
$bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
$body = join(PHP_EOL, $bodyParagraphs);
if (mail($toEmail, $emailSubject, $body, $headers)) {
header('Location: index.html');
} else {
$errorMessage = 'Oops, something went wrong. Please try again later';
}
} else {
$allErrors = join('<br/>', $errors);
$errorMessage = "<p style='color: purple;'>{$allErrors}</p>";
}
}
?>