<?php
/**
* Slack Incoming Webhook - Single PHP File
*
* @author Chris - http://codepad.co/cheers | https://github.com/webremote
* @copyright Copyright (c) 2016
* @version 0.0.28
* @build 20160204
*/
// Settings
$token = 'xxx-xxx-xxx-xxx-xxx'; // Request your token via https://api.slack.com/tokens
$channel = '#mandrill-log';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['mandrill_events'])) {
foreach (json_decode($_POST['mandrill_events'], true) as $entry) {
// Default values
$attachments = [];
$type = 'Unknown';
$emoji = 'question';
$text = print_r($entry, true);
switch ($entry['event']) {
case 'send':
$text = 'Sent a mail to ' . $entry['msg']['email'] . ' with subject "' . $entry['msg']['subject'] . '" (from: ' . $entry['msg']['sender'] . ')';
$emoji = 'email';
$type = 'Sent';
break;
case 'click':
$text = 'Somebody from ' . $entry['location']['country'] . ' clicked the URL "' . $entry['url'] . '"';
$emoji = 'point_right';
$type = 'Click';
$attachments = [
'fallback' => $text,
'text' => 'URL clicked: ' . $entry['url'],
'fields' => [
[
'title' => 'Location',
'value' => $entry['location']['country'] . (!empty($entry['location']['region']) ? PHP_EOL . $entry['location']['region'] : '') . (!empty($entry['location']['city']) ? PHP_EOL . $entry['location']['city'] : ''),
'short' => true,
],
[
'title' => 'Browser',
'value' => $entry['user_agent_parsed']['type'],
'short' => true
],
[
'title' => 'Subject',
'value' => $entry['msg']['subject'],
'short' => true,
],
[
'title' => 'Sender',
'value' => $entry['msg']['sender'],
'short' => true
]
],
'thumb_url' => $entry['user_agent_parsed']['ua_icon']
];
break;
case 'open':
$text = 'Somebody from ' . $entry['location']['country'] . ' opened an email "' . $entry['msg']['subject'] . '"';
$emoji = 'open_book';
$type = 'Read';
$attachments = [[
'fallback' => $text,
'fields' => [
[
'title' => 'Location',
'value' => $entry['location']['country'] . (!empty($entry['location']['region']) ? PHP_EOL . $entry['location']['region'] : '') . (!empty($entry['location']['city']) ? PHP_EOL . $entry['location']['city'] : ''),
'short' => true,
],
[
'title' => 'Browser',
'value' => $entry['user_agent_parsed']['type'] . ($entry['user_agent_parsed']['mobile'] ? ' - Mobile' : ' - Desktop'),
'short' => true
],
[
'title' => 'Subject',
'value' => $entry['msg']['subject'],
'short' => true,
],
[
'title' => 'Sender',
'value' => $entry['msg']['sender'],
'short' => true
]
],
'thumb_url' => $entry['user_agent_parsed']['ua_icon']
]];
break;
case 'soft_bounce':
$text = 'Email to ' . $entry['msg']['email'] . ' with subject "' . $entry['msg']['subject'] . '" got (soft) bounced';
$emoji = 'space_invader';
$type = 'Bounce';
break;
case 'hard_bounce':
$text = 'Email to ' . $entry['msg']['email'] . ' with subject "' . $entry['msg']['subject'] . '" got (hard) bounced (' . $entry['msg']['diag'] .')';
$emoji = 'skull_and_cross_bones';
$type = 'Bounce';
break;
}
if ($entry['type'] == 'blacklist') {
if ($entry['action'] == 'add') {
$type = 'Blacklist';
$emoji = 'new_moon_with_face';
$text = 'The e-mail ' . $entry['reject']['email'] . ' has been added to the blacklist (Reason: ' . $entry['reject']['reason'] . ', expires at ' . $entry['reject']['expires_at'] . ')';
}
}
if (empty($attachments)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Webremote.Mandrill.Webhook');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/chat.postMessage?');
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'token' => $token,
'channel' => $channel,
'username' => 'Mandrill-' . $type,
'text' => $text,
'icon_emoji' => ':' . $emoji . ':'
]);
curl_exec($ch);
curl_close($ch);
}
elseif (!empty($attachments)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Webremote.Mandrill.Webhook');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/chat.postMessage?' . http_build_query([
'token' => $token,
'channel' => $channel,
'username' => 'Mandrill-' . $type,
'text' => $text,
'attachments' => json_encode($attachments),
'icon_emoji' => ':' . $emoji . ':'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'content-length: ' . strlen($dataSend)
]);
$response = curl_exec($ch);
curl_close($ch);
}
}
}
}
This snippet is a fully functional incoming webhook for Mandrill that automatically posts to a Slack channel through the API. Including (working) attachments for detailed view.
1 Response
Fixed the emoji/icon for Blacklist event
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.