javascript Google Apps Script - Gmail,永久删除垃圾箱中带有特定标签的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15995031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Apps Script - Gmail, delete forever e-mails in trash with specific label
提问by SpeedBurner
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.
我正在尝试制作一个脚本,该脚本可以立即永久地自动删除来自某个发件人的电子邮件,因为 Gmail 只允许过滤器将电子邮件发送到垃圾箱 30 天。请不要建议默认过滤器就足够了,至于我的情况,重要的是我不知道我收到了来自该发件人的电子邮件。
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.
但是,我一直无法找到一种使用 GmailThread 并永久删除它的方法,因为不存在用于此目的的功能。我想看看是否有一种方法可以使用 JavaScript 完成任务,但一直无法找到一种方法。
Does anyone have an idea how I can set these e-mails to delete themselves permanently when received?
有谁知道我如何设置这些电子邮件以在收到时永久删除自己?
采纳答案by Corey G
It is not possible, by design, to delete an email permanently using GmailApp.
根据设计,不可能使用 GmailApp 永久删除电子邮件。
回答by Jonathan Y.
@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:
@karan 的回答已经指出了对我有用的解决方案,但是由于缺乏经验/不是专业开发人员,我花了一点时间将其转化为原始问题的可行解决方案。以下是我用来执行此任务的步骤的简要说明:
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()); } }
To enable advanced services for this script, locate
Resources
on the menu, and selectAdvanced Google services...
Enable
Gmail API
on the list.Before selecting
OK
, click on theGoogle Developers Console
link. Search forgmail
, and enable the service there as well.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"
.)
在我的脚本中创建以下函数:
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()); } }
要为此脚本启用高级服务,请
Resources
在菜单上找到,然后选择Advanced Google services...
Gmail API
在列表中启用。在选择之前
OK
,请单击Google Developers Console
链接。搜索gmail
,并在那里启用该服务。完成,选择
OK
;该功能现在应该可以工作了。(评论:如@karan 提供的链接中所述,可以使用"me"
userID,也可以提供自己的 Gmail 地址:"<address>@gmail.com"
。)
(Steps to enable advanced services for my script are based on Google's guide here.)
(为我的脚本启用高级服务的步骤基于此处的Google 指南。)
回答by Daniel Bultas
This script works for Google Apps Script. You have to just connect and auth services together.
此脚本适用于 Google Apps 脚本。您只需将服务连接在一起并进行身份验证。
function myFunction() {
var labelName = "deleteForever"
var threads = GmailApp.search("in:trash label:" + labelName);
for (var i = 0; i < threads.length; i++) {
Gmail.Users.Messages.remove('me', threads[i].getId());
}
}
回答by Karan
If it helps someone, it can be done using the advanced services.
如果它对某人有帮助,则可以使用高级服务来完成。
https://developers.google.com/gmail/api/v1/reference/users/messages/delete
https://developers.google.com/gmail/api/v1/reference/users/messages/delete
The method in advanced services is
高级服务中的方法是
Gmail.Users.Messages.remove(userId, id)
Gmail.Users.Messages.remove(userId, id)
回答by Ryosuke Hujisawa
try this
试试这个
function delete_all_mail(){
var myspreadsheet = SpreadsheetApp.openById('1AG1fZ9BuS8***********');
var mysheet = myspreadsheet.getSheets()[0];
//0~500
var threads = GmailApp.getInboxThreads(0 , 500);
for(var i = 0; i < threads.length; i++)
{
threads[i].moveToTrash();
}
}