使用谷歌脚本永久删除线程中的一条Gmail邮件 [英] permanently delete only one gmail message from a thread using a google script

查看:165
本文介绍了使用谷歌脚本永久删除线程中的一条Gmail邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在垃圾桶中的线程中永久删除Gmail邮件。



我在那里合并了几个脚本,因此我可以延迟并跟踪电子邮件。它通过保存草稿起作用,然后脚本将草稿复制到新电子邮件中,在指定时间发送并将原稿发送到垃圾箱。问题是,有一段时间,垃圾邮件中的草稿会再次发送(我还没有弄清楚为什么)...

作为一种解决方法,我使用了以下最初发布的代码:永远删除电子邮件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());
}
}

这很好,直到前一阵子。如果草稿在多条消息中的线程中,只有草稿被删除了......我现在在第6行出现了一个错误:无法调用方法删除undefined。



在这篇文章中:永久删除电子邮件2 ,建议用

 替换第6行Gmail.Users.Threads.remove('me' ,线程[I] .getId()); 

这种情况不会出现任何错误,但如果草稿位于具有多条消息的线程中,整个线程被删除,而不仅仅是草稿...



那么,有没有办法让只删除草稿?



我试着在线程中调用草稿的消息ID并使用原始行6:

 函数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 if(messages [j] .isDraft()){
Logger.log('id msg:' +消息[j] .getId());
Gmail.Users.Message.remove('me',messages [j] .getId());
}
}
}
}

但我得到了同样的错误,现在在线10 ...

我也试过使用这个函数:

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

你可以在google的开发者页面找到:这里。在尝试这个API部分它的工作原理,但在我的实施中,我得到了第2行的错误,说(从西班牙语翻译,所以我不知道它是否确切):名称(?) (后?)运算符。如果我复制功能在一个单独的选项卡中,我可以保存它,并显示相同的错误...



任何帮助将不胜感激...



问候,

解决方案

通过http请求:

  function cleanUp2(){
var threads = GmailApp.search(in:垃圾是:草稿);
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); (var j = 0; j if(messages [j] .isDraft()){
Logger.log('b
$ b' 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);
}
}
}
}


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,

解决方案

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);            
      }    
    }
  }
}

这篇关于使用谷歌脚本永久删除线程中的一条Gmail邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆