-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
101 lines (83 loc) · 2.94 KB
/
index.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
<?php
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
require __DIR__ . '/vendor/autoload.php';
$config = [
'ALLOW_ORIGIN' => getenv('ALLOW_ORIGIN'),
'DSN' => getenv('DSN') ?: $_POST['DSN'] ?? $_GET['DSN'] ?? '',
'SENDER' => getenv('SENDER') ?: $_POST['SENDER'] ?? $_GET['SENDER'] ?? '',
'RECIPIENT' => getenv('RECIPIENT') ?: $_POST['RECIPIENT'] ?? $_GET['RECIPIENT'] ?? '',
'REPLY_TO' => getenv('REPLY_TO') ?: $_POST['REPLY_TO'] ?? $_GET['REPLY_TO'] ?? '',
'SUBJECT' => getenv('SUBJECT') ?: $_POST['SUBJECT'] ?? $_GET['SUBJECT'] ?? 'Post to email',
'REDIRECT' => getenv('REDIRECT') ?: $_POST['REDIRECT'] ?? $_GET['REDIRECT'] ?? $_SERVER['HTTP_REFERER'] ?? '',
'HONEYPOT' => getenv('HONEYPOT') ?: $_POST['HONEYPOT'] ?? $_GET['HONEYPOT'] ?? '',
];
$required = ['ALLOW_ORIGIN', 'DSN', 'SENDER', 'RECIPIENT'];
foreach ($required as $key) {
if (!$config[$key]) {
http_response_code(400);
exit("Missing config for '{$key}'");
}
}
header("Access-Control-Allow-Origin: {$config['ALLOW_ORIGIN']}");
if ($config['REDIRECT']) {
if (!filter_var($config['REDIRECT'], FILTER_VALIDATE_URL)) {
http_response_code(400);
exit("REDIRECT '{$config['REDIRECT']}' is not a valid URL");
}
header("Location: {$config['REDIRECT']}", true, 302);
}
if ($config['HONEYPOT'] && isset($_POST[$config['HONEYPOT']]) && $_POST[$config['HONEYPOT']] !== '') {
http_response_code(400);
exit('Spam detected');
}
try {
$transport = Transport::fromDsn($config['DSN']);
} catch (Exception $e) {
http_response_code(400);
exit("DSN '{$config['DSN']}' is not a valid/supported DSN");
}
try {
$sender = Address::create($config['SENDER']);
} catch (Exception $e) {
http_response_code(400);
exit("SENDER '{$config['SENDER']}' is not a valid/supported address");
}
try {
$recipient = Address::create($config['RECIPIENT']);
} catch (Exception $e) {
http_response_code(400);
exit("RECIPIENT '{$config['RECIPIENT']}' is not a valid/supported address");
}
try {
$replyTo = Address::create($config['REPLY_TO'] ?: $config['SENDER']);
} catch (Exception $e) {
http_response_code(400);
exit("REPLY_TO '{$config['REPLY_TO']}' is not a valid/supported address");
}
// request is valid
echo 'OK';
$data = array_diff_key($_POST, $config, [$config['HONEYPOT'] => true]);
if (!$data) {
// requests without (non-config/-honeypot) body are considered tests
// and can be used to test or healthcheck;
// no email will be sent
exit;
}
ob_start();
include __DIR__.'/templates/plain.php';
$plain = ob_get_clean();
ob_start();
include __DIR__.'/templates/html.php';
$html = ob_get_clean();
$email = (new Email())
->from($sender)
->to($recipient)
->replyTo($replyTo)
->subject($config['SUBJECT'])
->text($plain)
->html($html);
$mailer = new Mailer($transport);
$mailer->send($email);