This is my first time to use Apps Scripts. I am only using this on my personal email account. I am trying to use a script that I came across on StackOverflow that is supposed to permanently delete emails with specific words that are in my spam folder. I have tested the code and it seems to correctly find the emails based on the text that I specify.
My problem is that I cannot get it perform the actual deletion of the emails. I think that I am not specifying my email address correctly. In my code example, I used me. I have also tried hard coding my email, but that did not work.
When I try to execute the code, it gives me the following error: "GoogleJsonResponseException: API call to gmail.users.messages.delete failed with error: Invalid user id specified in request/Delegation denied at deleteForever(Code:17:29)
What is the proper way to reference my personal email account?
function setTrigger() {
ScriptApp.newTrigger('deleteForever')
.timeBased()
.everyMinutes(1)
.create()
}
function deleteForever() {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
var msg = threads[i].getMessages()[0];
console.log(i)
if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
console.log('Found It!')
Gmail.Users.Messages.remove(me, threads[i].getId.toString());
}
}
}
Based on your code, here are the problems:
Gmail.Users.Messages.remove()
as a variable that is non-existing, thus causing the Invalid user id
error.message
ID instead of the threads[i]
ID as required on the id
parameter of the users.messages.delete method.function deleteForever() {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
var msg = threads[i].getMessages()[0];
console.log(i)
if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
console.log('Found It!')
Gmail.Users.Messages.remove('me', msg.getId());
}
}
}
After running the script: