Skip to content

Instantly share code, notes, and snippets.

@andrewmwilson
Last active May 31, 2023 13:02
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save andrewmwilson/5cab8367dc63d87d9aa5 to your computer and use it in GitHub Desktop.
Save andrewmwilson/5cab8367dc63d87d9aa5 to your computer and use it in GitHub Desktop.
Google Apps Script: Send Gmail emails to Slack
// You will also need to create a gmail filter to add the 'send-to-slack' label
// to any emails you want sent to slack
function sendEmailsToSlack() {
var label = GmailApp.getUserLabelByName('send-to-slack');
var messages = [];
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
messages = messages.concat(threads[i].getMessages())
}
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
Logger.log(message);
var output = '*New Email*';
output += '\n*from:* ' + message.getFrom();
output += '\n*to:* ' + message.getTo();
output += '\n*cc:* ' + message.getCc();
output += '\n*date:* ' + message.getDate();
output += '\n*subject:* ' + message.getSubject();
output += '\n*body:* ' + message.getPlainBody();
Logger.log(output);
var payload = {
'username': 'Monkeybot',
'text': output,
'channel' : '#some-channel',
'icon_emoji': ':hear_no_evil:',
};
var options = {
'method' : 'post',
'payload' : Utilities.jsonStringify(payload),
};
// replace this with your own Slack webhook URL
// https://crowdscores.slack.com/services
var webhookUrl = 'https://hooks.slack.com/services/****/****/****';
UrlFetchApp.fetch(webhookUrl, options);
}
// remove the label from these threads so we don't send them to
// slack again next time the script is run
label.removeFromThreads(threads);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment