
Better Mailboxapp GMail Integration
I love the Mailboxapp mobile email client. It's simple, clean and very effective. But it's missing a few things for me. With a little Google Apps Scripting I was able to make a few improvements.
What's Missing in Mailboxapp? (for me)
- Robust GMail Labeling
Currently Mailboxapp only allows you to use sublabels under the [Mailbox] label.
- Marking as SPAM
I want to move messages in
[Mailbox]/Spam to Spam label.
- Easy forwarding
I want to create tasks in Wunderlist and forward emails to work.
Google App Script
Google App Script allows you to write javascript apps and hook into Google APIs like Gmail.
Check out https://developers.google.com/apps-script/your_first_script for documentation and a video tutorial.
Before using Google App Script, I tried to do some of this using ifttt… It almost did the trick but I couldn't quite get it to work.
Using Google App Script I was able to leverage the Gmail Script API. I was also able to schedule the script to run once every minute.
Robust GMail Labeling
Now I'll show you how I handled GMail Labeling, i.e., moving messages from [Mailbox]/>labelName to labelName. Note the script creates labelName if it doesn't already exist.
First I set some variables like where to forward emails and some regex to identify which folders to label/archive. I also create a simple Gmailapp API wrapper to reduce API calls.
var WORK_EMAIL = 'martin.ruiz@wellsfargo.com';
var WORK_LABEL = 'Fwd To Work';
var MAILBOX = '[Mailbox]';
var ARCHIVE_PREFIX = '>';
var MAILBOX_ARCHIVE_REGEX = new RegExp('^\\[Mailbox\\]/' + ARCHIVE_PREFIX);
var MAILBOX_REGEX = new RegExp(/^\[Mailbox\]\/\>/);
var TODO_LABEL = 'Todo';
var TODO_EMAIL = 'me@wunderlist.com';
var MESSAGE_URL_BASE = 'https://mail.google.com/mail/u/0/#all/';
var SPAM_LABEL = 'Spam';
/**
* A simple wrapper for GmailApp API so I can minimize the
* number of API calls and keep it below the service limit.
* 10,000 calls is the limit. Using this wrapper I cache
* the results and reuse across function calls.
* https://developers.google.com/apps-script/class_gmailapp
*/
var Gmail = (function () {
var labels = GmailApp.getUserLabels();
var labelMap = {};
for (var i in labels) {
var label = labels[i];
labelMap[label.getName()] = label;
}
var getLabelByName = function (labelName) {
return labelMap[labelName];
};
return {
getUserLabels: function () { return labelMap; },
getUserLabelByName: getLabelByName
};
})();
The following code moves messages in [Mailbox]/>labelName to labelName. I use the prefix, >, to identify labels I want to archive. Every minute messages are archived and cleared.
/**
* archives messages in [Mailbox]/>labelName to
* labelName. It also sets the message as read.
* Note that I use '>' to signal which labels to
* archive.
*/
function archiveMessages(labelName) {
var label, archiveLabel, page, mailboxLabel;
mailboxLabel = MAILBOX + '/' + ARCHIVE_PREFIX + labelName;
label = Gmail.getUserLabelByName(mailboxLabel);
archiveLabel = Gmail.getUserLabelByName(labelName);
// create labelName if it doesn't exist
if (!archiveLabel) {
archiveLabel = GmailApp.createLabel(labelName);
}
page = null;
while(!page || page.length == 100) {
page = label.getThreads(0, 100);
if (page.length > 0) {
archiveLabel.addToThreads(page);
label.removeFromThreads(page);
GmailApp.markThreadsRead(page);
}
}
}
function doArchive() {
var labelName, labels;
labels = Gmail.getUserLabels();
// find and archive all labels in [Mailbox] with '>' prefix
for (labelName in labels) {
if (MAILBOX_ARCHIVE_REGEX.test(labelName)) {
labelName = labelName.replace(MAILBOX_ARCHIVE_REGEX,'');
archiveMessages(labelName);
}
}
}
You can find the rest of the code here.
To install, cut-n-paste-modify code in a new script @ script.google.com. Click on clock to schedule runMailboxCompanion every minute.
Email me at mr.ruiz@gmail.com if you have any questions
Next Steps
- add simple UI to configure.
- make available as installable plugin/script.
- add some new features like auto-filter creator for scheduling.
There are some emails I only want to read on weekends or evenings etc. I want to write a script that creates a filter for emails labeled
[Mailbox]/Weekend that skips inbox and moves messages back to inbox on weekends. This way I don't have to do manually everytime.