-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.php
More file actions
88 lines (68 loc) · 1.96 KB
/
contact.php
File metadata and controls
88 lines (68 loc) · 1.96 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
<?php
$emailFrom = 'Contact form <contact-form@simplecode.gr>';
$emailSendTo = 'Send to <hello@simplecode.gr>';
$emailSubject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'email' => 'Email', 'message' => 'Message');
$resStatusOkMsg = 'Contact form successfully submitted.';
$resStatusErrMsg = 'There was an error while submitting the form. Please try again later.';
/*** * * ***/
$EXCEPTION_FORM_IS_EMPTY = 'Form is empty';
/*** * * ***/
error_reporting(0);
/*** * * ***/
try
{
// message
$m = "New message from contact form\n=============================\n";
$m .= (function($fields, $post, $EXCEPTION_FORM_IS_EMPTY) {
if (count($post) == 0) {
throw new \Exception($EXCEPTION_FORM_IS_EMPTY);
}
$message = '';
foreach ($post as $key => $value) {
if (isset($fields[$key])) {
$message .= "$fields[$key]: $value\n";
}
}
return $message;
})($fields, $_POST, $EXCEPTION_FORM_IS_EMPTY);
// headers
$h = array(
'Content-Type: text/plain; charset="UTF-8";',
'From: ' . $emailFrom,
'Reply-To: ' . $emailFrom,
'Return-Path: ' . $emailFrom,
);
/*** * * ***/
mail(
$emailSendTo, // send to
$emailSubject, // subject
$m, // message
implode("\n", $h) // headers
);
/*** * * ***/
$res = array(
'type' => 'success',
'message' => $resStatusOkMsg
);
} catch (\Exception $e) {
$res = array(
'type' => 'danger',
'message' => $resStatusErrMsg
);
}
/*** * * ***/
// if req is AJAX -> JSON res
// else -> display $res['message']
if (
!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
$j = json_encode($res);
/*** * * ***/
header('Content-Type: application/json');
echo $j;
} else {
echo $res['message'];
}