Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active August 25, 2018 05:49
Show Gist options
  • Select an option

  • Save n8henrie/6f8dfa1ce2ec066cdf08 to your computer and use it in GitHub Desktop.

Select an option

Save n8henrie/6f8dfa1ce2ec066cdf08 to your computer and use it in GitHub Desktop.
A Google Script that adds extra powerful functions to my 'scripts' label, e.g. the ability to send emails to Mail Drop for OmniFocus.
/**
* n8GmailFilter.gs
* Adds extra powerful functions to my 'scripts' label, e.g.
* the ability to send emails to Mail Drop for OmniFocus.
*
* Details and setup instructions at: http://n8henrie.com/2014/08/using-google-apps-script-to-send-tasks-to-omnifocus/
*/
// CUSTOMIZE THESE VALUES
var myLabelName = 'your gmail label';
var taskEmail = 'your_task_manager@email.com';
var myDebug = false;
// END CUSTOMIZE THESE VALUES
// get the label for given name
var retryCount = 0;
var success = false;
while ( !success & retryCount++ <= 10 ){
try{
var label = GmailApp.getUserLabelByName(myLabelName);
success = true;
}catch(ex){
Logger.log(ex);
}
Utilities.sleep(1000);
}
function bankEmail(messageId) {
var message = GmailApp.getMessageById(messageId);
var source = message.getRawContent();
var body = message.getPlainBody();
var myRegex = /Transaction: \r\nAmount of Transaction: (\$\d+\.\d{2}) \r\nDate: ([01]\d\/[0-3]\d\/20\d{2})/;
var matched = myRegex.exec(source)[0]; // The whole match, mostly for debugging
var amount = myRegex.exec(source)[1]; // The first matched group
var date = myRegex.exec(source)[2]; // The second matched group
var taskName = 'Transaction: ' + amount + ' on ' + date;
var taskBody = body;
MailApp.sendEmail(taskEmail, taskName, taskBody);
message.getThread().removeLabel(label);
}
function yourNewFunction(messageId) {
message = GmailApp.getMessageById(messageId);
// do stuff here!
}
function processMessage(message) {
var bcc = message.getBcc();
var cc = message.getCc();
var body = message.getPlainBody();
var date = message.getDate();
var from = message.getFrom();
var subject = message.getSubject();
var to = message.getTo();
var messageId = message.getId();
var source = message.getRawContent();
if ( to == 'youremail@email.com'
&& from == 'Your Bank <yourbank@email.com>'
&& subject == 'Some consistent subject' ) {
bankEmail(messageId);
}
else if ( to == 'youremail@email.com'
&& from == 'Someone Else <someoneelse@email.com>'
&& /search string/i.test(body) ) {
yourNewFunction(messageId);
}
else {
if (myDebug) {
Logger.log("\nDarn, didn't match!");
Logger.log('to: ' + to);
Logger.log('from: ' + from);
Logger.log('subject: ' + subject);
Logger.log('bcc: ' + bcc);
Logger.log('cc: ' + cc);
Logger.log('date: ' + date);
Logger.log('messageId: ' + messageId);
Logger.log('body: ' + body);
Logger.log('source: ' + source);
}
}
}
function processLabel() {
// Iterate through threads in the given label
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
// Iterate through messages in a thread
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
processMessage(message);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment