-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemail-to-slack.php
136 lines (120 loc) · 4.42 KB
/
email-to-slack.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
<?php
/**
* Email to Slack
*
* @author Mehdi Chaouch (@MehdiChch)
* @license MIT
* @see https://github.com/mehdichaouch/email-to-slack
*/
function validate()
{
$json = json_decode(file_get_contents('php://input'), true);
if ('message' != $json['event']['type']) {
http_response_code(422); // event type must be `message`
exit();
}
$appId = ($json['api_app_id'] === $_ENV['APP_ID']);
$token = ($json['token'] === $_ENV['VERIFICATION_TOKEN']);
$teamId = ($json['team_id'] === $_ENV['TEAM_ID']);
$channel = (isset($json['event']['channel']) && $json['event']['channel'] === $_ENV['USLACKBOT_CHANNEL']);
$user = (isset($json['event']['user']) && 'USLACKBOT' === $json['event']['user']);
$subtype = (isset($json['event']['subtype']) && 'file_share' === $json['event']['subtype']);
$error = '';
if ($appId && $token && $teamId && $channel && $user && $subtype) {
return true;
} elseif (!$appId) {
$error = 'APP_ID is not right!';
} elseif (!$token) {
$error = 'TOKEN is not right!';
} elseif (!$teamId) {
$error = 'TEAM_ID is not right!';
} elseif (!$channel) {
$error = 'USLACKBOT channel is not right!';
} else if (in_array('X-Slack-Retry-Num', $_SERVER)) {
http_response_code(409);
exit('Duplicate');
}
http_response_code(400);
exit($error);
}
function main()
{
if ('GET' === $_SERVER['REQUEST_METHOD']) {
header('Location: //github.com/mehdichaouch/email-to-slack', true, 303); // RTFM
exit();
}
if ('POST' === $_SERVER['REQUEST_METHOD']) {
$json = json_decode(file_get_contents('php://input'), true);
if (in_array('challenge', array_keys($json))) {
header('Content-Type: text/plain');
exit($json['challenge']);
}
if (validate()) {
$email = $json['event']['files'][0];
$allTo = [];
foreach ($email['to'] as $emailTo) {
$allTo[] = $emailTo['original'];
}
$data = [
'text' => '',
'attachments' => [
[
'fallback' => 'An email was sent by ' . $email['from'][0]['original'],
'color' => '#d32600',
'pretext' => '',
'author_name' => $email['from'][0]['original'],
'author_link' => 'http://gmail.com/',
'author_icon' => '',
'title' => $email['title'],
'title_link' => 'http://gmail.com/',
'text' => $email['plain_text'],
'fields' => [],
'footer' => 'Sent to : ' . implode(', ', $allTo),
'footer_icon' => '',
'ts' => $email['timestamp'],
]
]
];
$allCc = [];
foreach ($email['cc'] as $emailCc) {
$allCc[] = $emailCc['original'];
}
if ($allCc = implode(', ', $allCc)) {
$data['attachments'][0]['fields'][] = ['title' => 'cc', 'value' => $allCc];
}
if ($email['attachments']) {
$data['attachments'][0]['fields'][] = [
'title' => '',
'value' => 'This email also has attachments',
'short' => false,
];
}
$curl = curl_init();
curl_setopt_array(
$curl,
[
CURLOPT_URL => $_ENV['INCOMING_WEBHOOK_URL'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
]
);
$response = curl_exec($curl);
curl_close($curl);
http_response_code(200);
exit('ok');
} else {
http_response_code(401);
exit('Bad request');
}
}
}
main();