-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforwarder.php
More file actions
78 lines (63 loc) · 2.01 KB
/
forwarder.php
File metadata and controls
78 lines (63 loc) · 2.01 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
<?php
ini_set('display_errors', 1);
error_reporting(-1);
function sparkpost($method, $uri, $payload = [], $headers = []) {
$defaultHeaders = [ 'Content-Type: application/json' ];
$curl = curl_init();
$method = strtoupper($method);
$finalHeaders = array_merge($defaultHeaders, $headers);
$url = 'https://api.sparkpost.com:443/api/v1/'.$uri;
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($method !== 'GET') {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $finalHeaders);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$payload = file_get_contents('php://input');
try {
$messages = json_decode($payload, true);
} catch (Exception $ex) {
die('no payload given');
}
if (empty($messages)) {
die('no messages given');
}
foreach ($messages as $message) {
$content = $message['msys']['relay_message']['content'];
if (!empty($message['msys']['relay_message']['rcpt_to']) && strpos($message['msys']['relay_message']['rcpt_to'], 'your@email.com') === false) {
return;
}
$apiKey = "SPARKPOST_API_KEY_HERE";
$replyTo = '';
foreach ($content['headers'] as $header) {
if (!empty($header['From'])) {
$replyTo = $header['From'];
}
}
$sendContent = [
'from' => 'your@email.com',
'reply_to' => $replyTo,
'subject' => $content['subject']
];
if ($content['email_rfc822_is_base64']) {
$sendContent['email_rfc822'] = base64_decode($content['email_rfc822']);
} else {
$sendContent['html'] = $content['html'];
$sendContent['text'] = $content['text'];
}
$payload = [
'content' => $sendContent,
'recipients' => [
[ 'address' => 'your@recipient.com' ],
],
];
$headers = [ 'Authorization: ' . $apiKey ];
$email_results = sparkpost('POST', 'transmissions', $payload, $headers);
// error_log(json_encode(json_decode($email_results, false), JSON_PRETTY_PRINT));
}
?>