1
votes

I am attempting to make my own version of Boomerang, a script that returns emails to your inbox at a later time as specified by the user.

I am using Google Apps Script and I have everything working except I can't return the email to the top of the inbox. I've tried forwarding the email with the relevant script, but to no avail.

Any thoughts?


Here's a few examples of the code I've tried (none of which produce the result I'm looking for):

Test 1:

  messages = GmailApp.getMessagesForThreads(threads);
  for (var i = 0 ; i < threads.length; i++) {
      messages[i][0].reply("<b>This message has been moved to the top of your inbox per your request. Please take action soon.</b>", {
        replyTo: "[email protected]"
      });
  }

Test 2:

  messages = GmailApp.getMessagesForThreads(threads);
  for (var i = 0 ; i < threads.length; i++) {
      forward("[email protected]", {
         htmlBody: "<b>This message has been moved to the top of your inbox per your request. Please take action soon.</b>",
         name: "ServAce85",
         replyTo: "[email protected]",
         from: "[email protected]",
         subject: "Re: " + messages[i].getSubject()

      });
   }

Test 3:

  messages = GmailApp.getMessagesForThreads(threads);
  for (var i = 0 ; i < threads.length; i++) {          
       messages[i][0].forward("[email protected]", {
         subject: messages[i][0].getSubject()
       });
  }

Test 4:

function moveEmail(threadsToMove) {
  var  threads = threadsToMove;
  var  messages = GmailApp.getMessagesForThreads(threads);
  for (var i = 0 ; i < threads.length; i++) {
    messages[i][0].forward(Session.getActiveUser().getEmail(), {
        subject: "Re: " + messages[i][0].getSubject(),
          htmlBody: "<b>This message has been moved to the top of your inbox per your request. Please take action soon.</b></n>" + Session.getActiveUser().getEmail() + "</n>" + messages[i][0].getBody()
      });
    GmailApp.sendEmail(Session.getActiveUser().getEmail(), "Re: " + messages[i][0].getSubject(), messages[i][0].getPermalink());
  }
}
2
Post the code you tried.Zig Mandel

2 Answers

1
votes

This seemed to work for me:

function moveEmail() {
  var  threads = GmailApp.getInboxThreads(0, 2); // Change to what you want the threads to be
  var  messages = GmailApp.getMessagesForThreads(threads);
  for (var i = 0 ; i < threads.length; i++) {
      messages[i][0].forward("[email protected]", {
          htmlBody: "<b>This message has been moved to the top of your inbox per your request. Please take action soon.</b></n>" + messages[i][0].getBody()
      });
   }
}
0
votes

I came here to look for a solution on something similar as Pause inbox.

Made this of it:

1) Make a filter in Gmail so every new mail bypasses the inbox and gets labeled as a custom label you created.

2) Make timetriggers for following google script function:

function moveTheMails(){
  var label0 = GmailApp.getUserLabelByName("Custom Label/Sublabel if wanted");
  var threads = label0.getThreads();
  for (var i = 0; i < threads.length; i++) {
    threads[i].moveToInbox();
    threads[i].removeLabel(label0);
  }
}

Voila, dependend on your timetriggers your new mails get delivered to your inbox at the custom times. As gmail-filter I used: from:(-{[email protected] OR [email protected]}) to:([email protected] OR [email protected]) The adresses in from are excluded (-) in the filter so mails from that persons are directly delivered in the inbox.