-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemail.php
184 lines (141 loc) · 4.95 KB
/
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
require_once __DIR__ . '/app/includes/Swift-5.0.1/lib/swift_required.php';
session_start();
// name and path of the configuration file for this script
$config_file = dirname(__FILE__) . "/app/config/config.ini";
// check that the configuration file is readable
if (file_exists($config_file) && is_readable($config_file)) {
$config = parse_ini_file($config_file);
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo "Could not read " . $config_file;
exit();
}
// determine the base URL for redirects back to the homepage
$path = pathinfo('//' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$url = $path['dirname'];
// check that the CSRF name & token is part of the POST request and that it matches the values set in session by index.php
if (isset($_POST['csrf']) && isset($_POST['token']) && ($_SESSION['csrf_token'][$_POST['csrf']] === $_POST['token'])) {
// prevent re-use of this session
unset($_SESSION['csrf_token'][$_POST['csrf']]);
// check if a timestamp has been recorded of a successful email being sent
if (isset($_SESSION['csrf_time_since_request'])) {
$seconds_passed = (time() - $_SESSION['csrf_time_since_request']);
// can't submit another form for a minute
if ($seconds_passed < 60) {
$_SESSION['csrf_lockout_time'] = (60 - $seconds_passed);
}
else {
unset($_SESSION['csrf_lockout_time']);
}
}
$errors = array();
$valid = false;
if (!(isset($_SESSION['csrf_lockout_time']))) {
$fields = array();
// sanitize POST fields
foreach ($_POST as $key => $value) {
$fields[$key] = trim($value);
}
// validate each field
if ($fields['contactname'] === '') {
$errors['contactname'] = 'This field is required.';
}
if ($fields['email'] === '') {
$errors['email'] = 'This field is required.';
}
elseif (!(filter_var($fields['email'], FILTER_VALIDATE_EMAIL))) {
$errors['email'] = 'Please enter a valid email address.';
}
if ($fields['subject'] === '') {
$errors['subject'] = 'This field is required.';
}
if ($fields['message'] === '') {
$errors['message'] = 'This field is required.';
}
if ($fields['dob'] !== '') {
// our spambot honeypot has been filled in
$errors['honeypot'] = true;
}
if (empty($errors)) {
try {
// create the transport
switch ($config['transport_method']) {
case 'smtp':
$transport = Swift_SmtpTransport::newInstance();
if ($config['smtp_host'] !== '') $transport->setHost($config['smtp_host']);
if ($config['smtp_port'] !== '') $transport->setPort($config['smtp_port']);
if ($config['smtp_encryption'] !== '') $transport->setEncryption($config['smtp_encryption']);
if ($config['smtp_username'] !== '') $transport->setUsername($config['smtp_username']);
if ($config['smtp_password'] !== '') $transport->setPassword($config['smtp_password']);
break;
case 'sendmail':
if ($config['sendmail_cmd'] !== '') {
$transport = Swift_SendmailTransport::newInstance($config['sendmail_cmd']);
}
else {
$transport = Swift_SendmailTransport::newInstance();
}
break;
default:
// default to PHP's mail() function
$transport = Swift_MailTransport::newInstance();
break;
}
// create the mailer using the transport
$mailer = Swift_Mailer::newInstance($transport);
// create a message
$message = Swift_Message::newInstance('GeoViQua tutorial contact form submission')
->setFrom(array($config['from_address'] => 'GeoViQua tutorial'))
->setTo(explode(';', $config['to_address']))
->setBody(
'The following form was submitted:' . PHP_EOL . PHP_EOL .
'Name: ' . $fields['contactname'] . PHP_EOL .
'Email address: ' . $fields['email'] . PHP_EOL .
'Subject: ' . $fields['subject'] . PHP_EOL .
'Message: ' . $fields['message']
);
// send the message
$numSent = $mailer->send($message);
if ($numSent > 0) {
// the email was sent
$valid = true;
// make a note of the time this successful request was made
$_SESSION['csrf_time_since_request'] = time();
}
}
catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
echo "Exception occurred: " . $e->getMessage();
exit();
}
}
}
else {
// inform the user of the number of seconds that they must wait
$errors['locked_out'] = 'You must wait ' . $_SESSION['csrf_lockout_time'] . ' seconds before you can submit this form again.';
}
// the response to send back
$data = array(
'valid' => $valid,
'errors' => $errors,
'fields' => $fields
);
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
// if this isn't an ajax request, save the response to session & redirect
$_SESSION['response'] = $data;
header('Location: ' . $url);
exit();
}
else {
// send a JSON response
die(json_encode($data));
}
}
else {
// possible CSRF forgery, silently redirect back to homepage
header('Location: ' . $url);
exit();
}
?>