Pekempy

Google Script - Auto signature

Jan 12th, 2022 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var signature = '';
  2.  
  3. //enter your label names here.
  4. var labelForInboundEmails = 'New Inbound Email';
  5. var labelToApplyAfterCreatingDraft = 'Waiting for your reply';
  6. var yourEmailAddress = 'you@domain.com';
  7.  
  8. //-------------------------------------------------------------------------------------------------------------------//
  9. //-------------------------------------------------------------------------------------------------------------------//
  10. //    PURPOSE:                                                                                                       //
  11. //    Automatically adds your signature to Gmail incoming emails, and sorts your inbox using a couple of labels      //
  12. //    As Android gmail doesn't support HTML signatures, creating a draft via a google script is a workaround.        //
  13. //                                                                                                                   //
  14. //                                                                                                                   //
  15. //    SETUP GUIDE:                                                                                                   //
  16. //    1. Create the two labels on line 4 and 5 in your gmail (customise as you want), and set your email in line 6   //
  17. //    2. Create a draft email, with the signature you want to apply, subject 'signature' (lowercase)                 //
  18. //    3. Run 'getGmailSignature', and copy the entire output of the console "Info" to line 1's variable              //
  19. //    4. Set up a gmail filter for "Includes the words: label:unread"                                                //
  20. //    5. Have this filer apply the label you selected on line 4                                                      //
  21. //    6. Set this script to run every x minutes in your "Triggers" tab on google scripts                             //
  22. //    7. ??? emails will automatically get a new Draft                                                               //
  23. //-------------------------------------------------------------------------------------------------------------------//
  24. //-------------------------------------------------------------------------------------------------------------------//
  25.  
  26. //can run this if signature is changed, then paste the console output into the above var
  27. function getGmailSignature() {
  28.   //find a draft email with the subject 'signature'
  29.   var draft = GmailApp.search("subject:signature label:draft", 0, 1);
  30.   //print the body of that email to the console for copy/paste into line 1 var
  31.   console.log(draft[0].getMessages()[0].getBody());
  32. }
  33.  
  34. function createEmailDraft() {
  35.   //find threads with the label
  36.   const inboundThreads = GmailApp.search('label:"'+labelForInboundEmails+'"');
  37.   //for each thread with that label, find all messages
  38.   for (const thread of inboundThreads) {
  39.     //get all messages in thread
  40.     const messages = thread.getMessages();
  41.     messageNum = 1;
  42.     for (message of messages) {
  43.       //if messageNum is the same as current count of messages create a draft
  44.       if (messageNum === thread.getMessageCount()) {
  45.         message.createDraftReply(
  46.           '', //body
  47.           {
  48.             //set the htmlBody of the email to the var signature
  49.             htmlBody: signature,
  50.             from: yourEmailAddress
  51.           }
  52.         );
  53.         //remove that label from this thread
  54.         var inboundLabel = GmailApp.getUserLabelByName(labelForInboundEmails);
  55.         thread.removeLabel(inboundLabel);
  56.         var outboundLabel = GmailApp.getUserLabelByName(labelToApplyAfterCreatingDraft);
  57.         thread.addLabel(outboundLabel);
  58.       }
  59.       messageNum = messageNum + 1;
  60.     }
  61.   }
  62.   //this section removes the "labelToApplyAfterCreatingDraft" after replying
  63.   const outboundThreads = GmailApp.search('label:"'+labelToApplyAfterCreatingDraft+'"');
  64.   for (const thread of outboundThreads) {
  65.     const messages = thread.getMessages();
  66.     messageNum = 1;
  67.     for (message of messages) {
  68.       if (messageNum === thread.getMessageCount()) {
  69.         if(message.getFrom().includes(yourEmailAddress) && !message.isDraft()) {
  70.           var outboundLabel = GmailApp.getUserLabelByName(labelToApplyAfterCreatingDraft);
  71.           thread.removeLabel(outboundLabel);
  72.         }
  73.       }
  74.       messageNum = messageNum + 1;
  75.     }
  76.   }
  77. }
Add Comment
Please, Sign In to add comment