Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var signature = '';
- //enter your label names here.
- var labelForInboundEmails = 'New Inbound Email';
- var labelToApplyAfterCreatingDraft = 'Waiting for your reply';
- var yourEmailAddress = 'you@domain.com';
- //-------------------------------------------------------------------------------------------------------------------//
- //-------------------------------------------------------------------------------------------------------------------//
- // PURPOSE: //
- // Automatically adds your signature to Gmail incoming emails, and sorts your inbox using a couple of labels //
- // As Android gmail doesn't support HTML signatures, creating a draft via a google script is a workaround. //
- // //
- // //
- // SETUP GUIDE: //
- // 1. Create the two labels on line 4 and 5 in your gmail (customise as you want), and set your email in line 6 //
- // 2. Create a draft email, with the signature you want to apply, subject 'signature' (lowercase) //
- // 3. Run 'getGmailSignature', and copy the entire output of the console "Info" to line 1's variable //
- // 4. Set up a gmail filter for "Includes the words: label:unread" //
- // 5. Have this filer apply the label you selected on line 4 //
- // 6. Set this script to run every x minutes in your "Triggers" tab on google scripts //
- // 7. ??? emails will automatically get a new Draft //
- //-------------------------------------------------------------------------------------------------------------------//
- //-------------------------------------------------------------------------------------------------------------------//
- //can run this if signature is changed, then paste the console output into the above var
- function getGmailSignature() {
- //find a draft email with the subject 'signature'
- var draft = GmailApp.search("subject:signature label:draft", 0, 1);
- //print the body of that email to the console for copy/paste into line 1 var
- console.log(draft[0].getMessages()[0].getBody());
- }
- function createEmailDraft() {
- //find threads with the label
- const inboundThreads = GmailApp.search('label:"'+labelForInboundEmails+'"');
- //for each thread with that label, find all messages
- for (const thread of inboundThreads) {
- //get all messages in thread
- const messages = thread.getMessages();
- messageNum = 1;
- for (message of messages) {
- //if messageNum is the same as current count of messages create a draft
- if (messageNum === thread.getMessageCount()) {
- message.createDraftReply(
- '', //body
- {
- //set the htmlBody of the email to the var signature
- htmlBody: signature,
- from: yourEmailAddress
- }
- );
- //remove that label from this thread
- var inboundLabel = GmailApp.getUserLabelByName(labelForInboundEmails);
- thread.removeLabel(inboundLabel);
- var outboundLabel = GmailApp.getUserLabelByName(labelToApplyAfterCreatingDraft);
- thread.addLabel(outboundLabel);
- }
- messageNum = messageNum + 1;
- }
- }
- //this section removes the "labelToApplyAfterCreatingDraft" after replying
- const outboundThreads = GmailApp.search('label:"'+labelToApplyAfterCreatingDraft+'"');
- for (const thread of outboundThreads) {
- const messages = thread.getMessages();
- messageNum = 1;
- for (message of messages) {
- if (messageNum === thread.getMessageCount()) {
- if(message.getFrom().includes(yourEmailAddress) && !message.isDraft()) {
- var outboundLabel = GmailApp.getUserLabelByName(labelToApplyAfterCreatingDraft);
- thread.removeLabel(outboundLabel);
- }
- }
- messageNum = messageNum + 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment