Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
Automatically delete emails from gmail account spam folder which contain specific keywords

In the past I have used a simple google apps script snippet to delete all spam email from my Gmail account once an hour. This solution worked however it meant I sometimes would not receive emails from people that Google chose to move to the spam folder. I decided the best solution would be to delete only the emails which contained certain keywords. After failing to find an example online of how to do this I came up with a solution after some research on the Gmail apps script API

Script source: Google Apps Script - Gmail, delete forever e-mails in trash with specific label

The original code to delete all email:

function deleteForever(userId) {
  var threads = GmailApp.search("in:spam");

  for (var i = 0; i < threads.length; i++) {
    Gmail.Users.Messages.remove(userId, threads[i].getId().toString());
  }
}
deleteForever("[your_email_here@gmail.com]")

Please note that "in:spam" can be changed to any location in your Gmail account. Be very careful when choosing the threads you wish to delete!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The solution required two pieces of information I didn't have before:

  1. the getPlainBody() method for getting a message's text
  2. the javascript match function

Rather than blindly deleting every message in the threads variable I use a very basic regular expression to match a list of keywords and only delete messages which match.

When using this script add a pipe | between words and wrap phrases which contain spaces in parentheses as shown below.

function deleteForevers(userId) {
  var threads = GmailApp.search("in:spam");

  for (var i = 0; i < threads.length; i++) {
    var msg = threads[i].getMessages()[0];
    if (msg.getPlainBody().match(/prizes|miracle|(real estate)|(nigerian prince)/gi) !== null){
        Gmail.Users.Messages.remove(userId, threads[i].getId().toString());
    }
  }
}
deleteForevers("[your_email_here@gmail.com]")

I hope this is helpful!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda