Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

409
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda