Skip to content

Instantly share code, notes, and snippets.

@cggaurav
Created October 2, 2013 22:00
Show Gist options
  • Save cggaurav/6801114 to your computer and use it in GitHub Desktop.
Save cggaurav/6801114 to your computer and use it in GitHub Desktop.
PodioMail
/*
---------------------------------------------
G M A I L A U T O F O R W A R D I N G
---------------------------------------------
The forwardMail function will auto-forward mails
associated with a particular Gmail label to the
corresponding email address specified in Google Docs.
For a video demo, go to http://www.labnol.org/?p=20665
For help, send me an email at [email protected]
Written by Amit Agarwal - January 5, 2012
Updated: July 1, 2013
*/
function createTrigger() {
var triggers = ScriptApp.getScriptTriggers();
for(var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
if ( ! GmailApp.getUserLabelByName("forwarded") ) {
GmailApp.createLabel("forwarded");
}
ScriptApp.newTrigger('forwardMail')
.timeBased()
.everyMinutes(1)
.create();
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menu = [
{name: "Initialize", functionName: "Initialize"},
{name: "Start Auto-Forward", functionName: "Start"},
{name: "Uninstall", functionName: "Uninstall"}
];
ss.addMenu("Gmail Forward", menu);
}
function Initialize() {
Logger.log("Hello World");
createTrigger();
}
function Start() {
createTrigger();
}
function Uninstall() {
var triggers = ScriptApp.getScriptTriggers();
for(var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function forwardMail() {
var fwd = GmailApp.getUserLabelByName("forwarded");
var data = SpreadsheetApp.getActiveSheet().getRange("A2:B11").getValues();
for (i in data) {
var row = data[i];
var name = row[0].toString();
var email = row[1].toString();
if ((name != "") && (email != "")) {
var threads = GmailApp.search("in:" + name + " -in:forwarded", 0, 10);
//var threads = GmailApp.search("in:" + name, 0, 2);
for (var x in threads) {
var messages = threads[x].getMessages();
for (var y in messages) {
autoforward (messages[y],email);
//Logger.log("FUCKYEAH");
//Logger.log(messages[y].getPlainBody());
//Logger.log("\n\n\n\n\n");
}
threads[x].addLabel(fwd);
}
}
}
}
function autoforward (msg, email) {
//var body = msg.getBody();
//var attachments = msg.getAttachments();
//var regMessageId = new RegExp(msg.getId(), "g");
// if (body.match(regMessageId) != null) {
// var inlineImages = {};
// var nbrOfImg = body.match(regMessageId).length;
// var imgVars = body.match(/<img[^>]+>/g);
// var imgToReplace = [];
// for (var i = 0; i < imgVars.length; i++) {
// if (imgVars[i].search(regMessageId) != -1) {
// var id = imgVars[i].match(/Inline\simages?\s(\d)/);
// if (id) {
// imgToReplace.push([parseInt(id[1]), imgVars[i]]);
// }
// }
// }
// imgToReplace.sort(function (a, b) {
// return a[0] - b[0];
// });
// for (var i = 0; i < imgToReplace.length; i++) {
// var attId = (attachments.length - nbrOfImg) + i;
// var title = 'inlineImages' + i;
// inlineImages[title] = attachments[attId].copyBlob().setName(title);
// attachments.splice(attId, 1);
// var newImg = imgToReplace[i][1].replace(/src="[^\"]+\"/, "src=\"cid:" + title + "\"");
// body = body.replace(imgToReplace[i][1], newImg);
// }
// }
//msg.forward(email);
//var headers = "---------- Forwarded message ----------\nFrom: 張瀟 Yolanda <[email protected]>"
var body = "---------- Forwarded message ----------\nFrom: " + msg.getFrom() + "\n\n\n\n" + msg.getPlainBody();
GmailApp.sendEmail(email, msg.getSubject(), body, { attachments: msg.getAttachments()});
// GmailApp.sendEmail(email, msg.getSubject(), headers + msg.getPlainBody(),
// {attachments: attachments, htmlBody: body, cc: cc, inlineImages: inlineImages});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment