permanently delete only one gmail message from a t

2019-03-05 08:37发布

问题:

I want to permanently delete a Gmail message inside a thread already in the trash.

I merged a few scripts around there, so I can delay and track emails. It works by saving a draft, then the script copy the draft into a new email, send it at the specified time and send the original draft to trash. The problem is that once in a while, the drafts that are in the trash are sent again (i haven't been able to figure out why yet)...

As a workaround, I was using the following code that that was originally posted here: delete forever emails 1:

function cleanUp() {
  var threads = GmailApp.search("in:trash is:draft");
  Logger.log(threads.length);
  for (var i = 0; i < threads.length; i++) {
    Logger.log(threads[i].getId());
    Gmail.Users.Message.remove('me',threads[i].getId());
  }
}

This was working fine, until a while ago. If the draft was inside a thread with more than 1 message, only the draft was deleted... I got now an error on line 6 that says: "Cannot call method "remove" of undefined".

In this post: delete forever emails 2, it is suggested to replace line 6 by

Gmail.Users.Threads.remove('me',threads[i].getId());

This dosn't get any errors, but if the draft is in a thread with more than one message, the whole thread is deleted instead of only the draft...

So, is there a way to get only the draft erased?

I tried calling the message id of the draft inside the thread and use the original line 6:

function cleanUp2() {
  var threads = GmailApp.search("in:trash is:draft");
  Logger.log(threads.length);      
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    Logger.log(messages.length);        
    for (var j = 0; j < messages.length; j++){
      if (messages[j].isDraft()){
        Logger.log('id msg: ' + messages[j].getId());
        Gmail.Users.Message.remove('me',messages[j].getId());        
      }    
    }
  }
}

But I got the same error, now on line 10...

I also tried using this function:

function deleteMessage(userId, messageId) {
  var request = gapi.client.gmail.users.messages.delete({
    'userId': userId,
    'id': messageId
  });
  request.execute(
    function(resp) { });
}

That you can find in the developers page of google: here. In the "try this API" section it works, but in my implementation i got an error on line 2 that says (translated from Spanish so i don't know if it will be exact): "a name (?) is missing behind (after?) operator "."" And if i copy the function in a separated tab, i can save it and the same error is showed...

Any help will be appreciated...

Regards,

回答1:

i finally made it trough an http request:

function cleanUp2() {
  var threads = GmailApp.search("in:trash is:draft");
  Logger.log(threads.length);

  var userId = 'xxxxx@gmail.com';
  var options = {
   'method' : 'delete',
   'muteHttpExceptions': true
 };

  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    Logger.log(messages.length);

    for (var j = 0; j < messages.length; j++){
      if (messages[j].isDraft()){
        Logger.log('id msg: ' + messages[j].getId());
        var url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/' + messages[j].getId();
        var response = UrlFetchApp.fetch(url,options);
        Logger.log(response);            
      }    
    }
  }
}