Skip to content

Instantly share code, notes, and snippets.

@ano
Last active February 18, 2022 12:35
Show Gist options
  • Save ano/0070ab14812f42706c9c10b8845f8bc6 to your computer and use it in GitHub Desktop.
Save ano/0070ab14812f42706c9c10b8845f8bc6 to your computer and use it in GitHub Desktop.
Send slack messages to email
function display_name(text) {
/* Remove all quotes
Remove whitespace, brackets, and commas from the ends. */
return text.replace(/(^[\s,>]+)|"|([\s,<]+$)/g, '');
}
function get_email_adresses(addr_list) {
/* Regex source:
https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
*/
var email_re = /[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/g;
var emails = [], match, idx = 0;
while (match = email_re.exec(addr_list)) {
var display;
if (display = display_name(addr_list.substring(idx, match['index']))) {
emails.push('"' + display + '" ' + '<' + match[0] + '>');
}
else {
emails.push(match[0]);
}
idx = match['index'] + match[0].length;
}
return emails;
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
function get_message(slack_message){
var address = get_email_adresses(slack_message);
for(var i = 0; i < address.length; i++){
slack_message = replaceAll(slack_message,address[i],"");
}
return slack_message.trim();
}
var slack_message = 'ano.tisam@ffa.int ano.tisam@gmail.com hey guys this is a test email sent from slack';
var email_adresses = get_email_adresses(slack_message);
var email_message = get_message(slack_message);
console.log(email_adresses);
console.log(email_message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment