Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

javascript - Google Apps Script - Gmail, delete forever e-mails in trash with specific label

I'm trying to make a script that automatically deletes e-mails from a certain sender immediately and permanently, as Gmail only allows for a filter which sends an e-mail to trash for 30 days. Please do not suggest that the default filter is enough, as for my situation, it is vital that I do not know that I was sent an e-mail from this sender.

My current script looks like this:

function deleteForever(labelName) {
    var threads = GmailApp.search("in:trash label:" + labelName);
    for (var i = 0; i < threads.length; i++) {
      threads[i].moveToTrash(); // Where I would need a delete forever trigger
    }
};

However, I have been unable to figure out a way to use a GmailThread and to delete it permanently as there does not exist a function for this purpose. I was looking to see if there was a way I could finish the task using JavaScript, but have been unable figure out a method.

Does anyone have an idea how I can set these e-mails to delete themselves permanently when received?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@karan's answer already points to the solution which worked for me, but being inexperienced / not-a-professional-developer, it took me a little work to translate it into a working solution to the original question. Here's a concise description of the steps I used to perform this task:

  1. Create the following function in my script:

    function deleteForever(userId, labelName) {
      var threads = GmailApp.search("in:trash label:" + labelName);
      for (var i = 0; i < threads.length; i++) {
        Gmail.Users.Messages.remove(userId, threads[i].getId());
      }
    }
    
  2. To enable advanced services for this script, locate Resources on the menu, and select Advanced Google services...

  3. Enable Gmail API on the list.

  4. Before selecting OK, click on the Google Developers Console link. Search for gmail, and enable the service there as well.

  5. Done, select OK; the function should now work. (Comment: as mentioned in the link @karan provided, one can use "me" for userID, or alternatively provide one's Gmail address: "<address>@gmail.com".)

(Steps to enable advanced services for my script are based on Google's guide here.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...