-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
151 lines (145 loc) · 5.67 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
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
<?php
/*
* Author: Emiliano Vargas
* Date: Jun 2020
* Description: Automate your SES bounces and complaints to be sent automatically to you Slack Channel.
*/
//Set timezone to retrieve correct timestamps
date_default_timezone_set('America/Argentina/Buenos_Aires');
//Set your Slack Webhook URL
$webhook = 'https://hooks.slack.com/YOUR_URL_TO_THE_WEBHOOK';
require 'vendor/autoload.php';
use Aws\Sns\Message;
use Aws\Sns\MessageValidator;
// Make sure the request is POST.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(404);
die;
}
//Magic from the AWS-SDK.
$message = Message::fromRawPostData();
//Uncomment this if you want to confirm a subscription from a topic.
// if ($message['Type'] === 'SubscriptionConfirmation') {
// // Confirm the subscription by sending a GET request to the SubscribeURL
// file_get_contents($message['SubscribeURL']);
// }
//Validate that the msg comes from AWS. More magic from the AWS-SDK.
$validator = new MessageValidator();
//If the message is valid
if ($validator->isValid($message)) {
//We load the notification message into $json.
$json = json_decode($message['Message']);
var_dump($json);
//Switch to evaluate if the notification is from a bounce or a complaint and then we prepare the json to be sent to Slack.
switch (true) {
case $json->bounce != null:
$data = array(
'blocks' =>
array(
0 =>
array(
'type' => 'section',
'text' =>
array(
'type' => 'mrkdwn',
'text' => "🚫*SES " . $json->notificationType . " Received*🚫\n" . $json->bounce->bouncedRecipients[0]->emailAddress . " " . $json->bounce->bouncedRecipients[0]->action,
),
),
1 =>
array(
'type' => 'section',
'fields' =>
array(
0 =>
array(
'type' => 'mrkdwn',
'text' => "*BounceType:*\n" . $json->bounce->bounceType,
),
1 =>
array(
'type' => 'mrkdwn',
'text' => "*bounceSubType:*\n" . $json->bounce->bounceSubType,
),
2 =>
array(
'type' => 'mrkdwn',
'text' => "*Timestamp:*\n" . date('l jS \of F Y h:i:s A'),
),
3 =>
array(
'type' => 'mrkdwn',
'text' => "*Source:*\n" . $json->mail->source . " (" . $json->mail->sourceIp . ")",
),
),
),
2 =>
array(
'type' => 'divider'
),
),
);
break;
case $json->complaint != null:
$data = array(
'blocks' =>
array(
0 =>
array(
'type' => 'section',
'text' =>
array(
'type' => 'mrkdwn',
'text' => "⚠*SES " . $json->notificationType . " Received*⚠\n" . $json->complaint->complainedRecipients[0]->emailAddress,
),
),
1 =>
array(
'type' => 'section',
'fields' =>
array(
0 =>
array(
'type' => 'mrkdwn',
'text' => "*Complaint Type:*\n" . $json->complaint->complaintFeedbackType,
),
1 =>
array(
'type' => 'mrkdwn',
'text' => "*User Agent:*\n" . $json->complaint->userAgent,
),
2 =>
array(
'type' => 'mrkdwn',
'text' => "*Timestamp:*\n" . date('l jS \of F Y h:i:s A'),
),
3 =>
array(
'type' => 'mrkdwn',
'text' => "*Source:*\n" . $json->mail->source . " (" . $json->mail->sourceIp . ")",
),
),
),
2 =>
array(
'type' => 'divider'
),
),
);
break;
}
//We send the JSON to the Slack Webhook
$curl = curl_init($webhook);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_exec($curl);
}
//If message it's not valid.
else {
//Send the error message + the IP that originated the notification.
$curl = curl_init($webhook);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array(
'text' => '🚫Ha fallado la verificación del mensaje. WHO: ' . $_SERVER['REMOTE_ADDR'],
)));
curl_exec($curl);
}