Skip to content

Instantly share code, notes, and snippets.

@dbrant
Last active May 2, 2022 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrant/d053ea62008b425953e08c10dfac730b to your computer and use it in GitHub Desktop.
Save dbrant/d053ea62008b425953e08c10dfac730b to your computer and use it in GitHub Desktop.
Google Apps Script for deleting empty spam messages from Gmail
function deleteMessageIfEmpty() {
var threads = GmailApp.search('is:unread in:inbox')
for (const thread of threads) {
const messages = thread.getMessages()
if (messages.length != 1) { continue; }
const msg = messages[0];
const words = msg.getPlainBody().trim().split(" ");
//if (msg.getSubject.length == 0 && msg.getPlainBody().trim().length == 0
if (words.length < 3 && msg.getBody().length < 256) {
console.log("Deleting empty spam from: " + msg.getFrom())
const reply = msg.reply("** Address not found **\n\n\
Your message wasn't delivered to " + msg.getTo() + " because the address couldn't be found, \
or is unable to receive mail.\n\nLearn more here: https://support.google.com/mail/?p=3DNoSuchUser\n\n\
The response was:\n\n550 5.1.1 The email account that you tried to reach does not exist. Please \
try double-checking the recipient's email address for typos or unnecessary \
spaces. Learn more at https://support.google.com/mail/?p=3DNoSuchUser m189-\
20020a633fc6000000b003806a0489e6sor8827957pga.40 - gsmtp", {
subject: "Delivery Status Notification (Failure)",
name: "Mail Delivery Subsystem",
replyTo: "mailer-daemon@googlemail.com"
})
msg.moveToTrash()
reply.moveToTrash()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment