使用 google 脚本从线程中永久删除仅一条 gmail 消息 [英] permanently delete only one gmail message from a thread using a google script
问题描述
我想永久删除已在垃圾箱中的线程中的 Gmail 邮件.
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)...
作为一种解决方法,我使用了最初发布在此处的以下代码:永久删除电子邮件 1:
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());
}
}
这一直很好,直到不久前.如果草稿在包含超过 1 条消息的线程中,则只有草稿被删除...我现在在第 6 行收到一条错误消息:无法调用未定义的删除"方法".
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".
在这篇文章中:永久删除电子邮件 2,建议将第6行替换为
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?
我尝试在线程内调用草稿的消息 id 并使用原始第 6 行:
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());
}
}
}
}
但我得到了同样的错误,现在在第 10 行...
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) { });
}
您可以在 google 的开发者页面中找到:这里.在尝试这个 API"部分它可以工作,但在我的实现中,我在第 2 行收到一个错误,上面写着(从西班牙语翻译,所以我不知道它是否准确):后面缺少一个名称(?)(之后?)运算符."如果我将函数复制到单独的选项卡中,我可以保存它并显示相同的错误......
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...
问候,
推荐答案
我终于通过http请求成功了:
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);
}
}
}
}
这篇关于使用 google 脚本从线程中永久删除仅一条 gmail 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!