Created
September 24, 2017 17:40
-
-
Save summatix/e8d86e770a59d2fed791e0c24535226e to your computer and use it in GitHub Desktop.
Gmail: Automatically send email on trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sends an email when a trigger is received. | |
*/ | |
function RespondEmail(e) { | |
// All messages marked with this label will trigger a new email to be sent | |
var triggerLabel = "paymenttrigger"; | |
// The message content is pulled from a saved draft email. draftSubject | |
// specifies the subject to search for. | |
var draftSubject = "Your ebook is here!"; | |
// The name to reply as | |
var replyFrom = '"My Name" <[email protected]>'; | |
/** | |
* Loads the message from the saved draft with the specified subject. | |
* | |
* @param {string} - The subject of the draft to retrieve the message from. | |
* @return {GmailMessage} - A Gmail message loaded from the draft. | |
*/ | |
var getDraftMessage = (function(draftSubject) { | |
var message = ""; | |
// Cache the message so we only have to search for the draft once | |
return function(draftSubject) { | |
if (!message) { | |
// Loop through all saved drafts | |
var drafts = GmailApp.getDrafts(); | |
for (var i = 0; i < drafts.length; ++i) { | |
var draft = GmailApp.getDraft(drafts[i].getId()); | |
// End search if we've found the subject we're looking for | |
if (draft.getMessage().getSubject() == draftSubject) { | |
message = draft.getMessage(); | |
break; | |
} | |
} | |
if (!message) { | |
throw 'Cannot find draft with subject "' + draftSubject + '"'; | |
} | |
} | |
return message; | |
}; | |
}()); | |
/** | |
* Extracts the email address from the message. | |
* | |
* @param {string} - The message contents to extract the email address | |
* from. | |
* @return {string} - The extracted email address. | |
*/ | |
var extractEmailFromMessage = function(message) { | |
var matches = message.match(/You received a payment[ $.\w]+\(([^)]+)\)/im); | |
if (!matches) { | |
throw 'Cannot find email address inside "' + message + '"'; | |
} | |
return matches[1]; | |
}; | |
/** | |
* Sends a message based on the given GmailMessage. | |
* | |
* @param {string} - The email address to send the message to. | |
* @param {GmailMessage} - The GmailMessage to send. | |
*/ | |
var sendMessage = function(to, message) { | |
GmailApp.sendEmail(to, message.getSubject(), message.getPlainBody(), { | |
attachments: message.getAttachments(), | |
from: replyFrom, | |
htmlBody: message.getBody() | |
}); | |
}; | |
// Search for all messages marked with our label | |
var trigger = GmailApp.getUserLabelByName(triggerLabel); | |
var messages = GmailApp.search("label:" + triggerLabel); | |
// Send out a new email for all the messages we found. Make sure to remove | |
// the trigger label so the messages aren't picked up next time. | |
for (var i = 0; i < messages.length; ++i) { | |
var sendTo = extractEmailFromMessage(messages[i].getMessages()[0].getPlainBody()); | |
sendMessage(sendTo, getDraftMessage(draftSubject)); | |
messages[i].removeLabel(trigger); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment