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

408
Visualizações
google scripts throwing not a function error for getMessages()

I am trying to create a script for an autoreply function. I am currently trying to grab each of the emails assigned to a specific label and grab specific details off of each email, however I am receiving an error stating TypeError: threads.getMessages is not a function, even though I have triple checked that this is, indeed, a function of the GMAIL scripting. Have I done something wrong here in this code that may be causing this error?

  var labelName = "auto-reply-incoming";
  var receivedCount = 0;

// extract emails from label in Gmail
function extractEmails() {
  // get all email threads that match label
  var receivedSearchQuery = "label:"+labelName+" -is:sent";
  var threads = GmailApp.search(receivedSearchQuery, 0, 500);
  
  for (var i=0; i<threads.length; i++) {
    //count received emails in label
    receivedCount = receivedCount + threads[i].getMessageCount();
    //get body of each email
    var message = threads.getMessages()[i].getBody();
    //get recipient of each email
    var recipient = threads.getMessages()[i].getTo();
  }
  Logger.log(receivedCount);
  Logger.log(recipient);
}
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

The problem occurs because the code is using the threads iterator that might be bigger than the number of messages on certain threads, more specifically, the problem is in the following lines:

    //get body of each email
    var message = threads.getMessages()[i].getBody();
    //get recipient of each email
    var recipient = threads.getMessages()[i].getTo();

Here is a quick fix (replace the above, by the following)

    var messages = threads[i].getMessages();
    for( j = 0; j < messages.length; j++){
      //get body of each email
      var message = messages[j].getBody();
      //get recipient of each email
      var recipient = messages[j].getTo();
    }

Note: Logger.log(recipient) will log the recipient of the last message of the last thread.

about 4 years ago · Santiago Gelvez Relatório

0

Try this:

function extractEmails() {
  var labelName = "auto-reply-incoming";
  var receivedCount = 0;
  var receivedSearchQuery = "label:" + labelName + " -is:sent";
  var ts = GmailApp.search(receivedSearchQuery, 0, 500);
  var a = [];
  ts.forEach((t, i) => {
    let ms = t.getMessages();
    ms.forEach((m, j) => {
      let msg = m.getBody();
      let rec = m.getTo();
      a.push({ threadindex: j, messageindex: i, message: msg, recipient: rec })
    });
  });
  Logger.log(JSON.stringify(a));
}
about 4 years ago · Santiago Gelvez 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