forked from dennisivy/portfolio-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessContact.php
More file actions
110 lines (88 loc) · 2.97 KB
/
processContact.php
File metadata and controls
110 lines (88 loc) · 2.97 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
session_start();
$errors = [];
// CHECK ELEMENTs FUNCTIONS
function checkInput($el) {
global $greske;
if(!preg_match($el['regex'], $el['value'])) {
$errors[] = "Valid format: " + $el['example'];
}
}
function checkMsg($el) {
if($el['value'] == "" || strlen($el['value']) <= 9) {
$errors[] = $el['error'];
}
}
if(isset($_POST['submitContact'])) {
$errors = [];
var_dump($_POST);
$name = htmlspecialchars($_POST['name']);
$subject = htmlspecialchars($_POST['subject']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
$regexName = "/^[A-Z][a-z]{2,20}(\s[A-Z][a-z]{3,20}){1,3}$/";
$regexEmail = "/^[A-z\d\.\-]{5,100}\@[a-z]{2,10}\.[a-z]{2,20}$/";
$regexSubject = "/^[A-z\d\.\-\s.#]{5,100}$/";
$values = [
[
"type" => "input",
"value" => $name,
"regex" => $regexName,
"example" => "Blake Smith"
],
[
"type" => "input",
"value" => $subject,
"regex" => $regexSubject,
"example" => "Job offer"
],
[
"type" => "input",
"value" => $email,
"regex" => $regexEmail,
"example" => "[email protected]"
],
[
"type" => "textarea",
"value" => $message,
"example" => "Message must have at least 10 characters"
]
];
foreach ($values as $el) {
if($el['type'] == "input") {
checkInput($el);
} else {
checkMsg($el);
}
}
if(count($errors) == 0) {
// PASSED
$toEmail = "[email protected]";
$body = "<h2>Contact Request</h2>
<h4>Name</h4><p>{$name}</p>
<h4>Email</h4><p>{$email}</p>
<h4>Message</h4><p>{$message}</p>
";
// Email Headers
$headers = "MIME-Version: 1.0" ."\r\n";
$headers .="Content-Type:text/html;charset=UTF-8" . "\r\n";
// Additional Headers
$headers .= "From: " .$name. "<".$email.">". "\r\n";
if(mail($toEmail, $subject, $body, $headers)){
// Email Sent
$msg = 'Your email has been sent';
$state = true;
} else {
// Failed
$msg = 'Your email was not sent. Try again later';
$state = false;
}
$_SESSION['msg'] = $msg;
$_SESSION['state'] = $state;
header("Location: index.php#contact");
} else {
$_SESSION['errors'] = $errors;
header("Location: index.php#contact");
}
}
?>