Skip to content Skip to sidebar Skip to footer

Trying To Understand GetThreads In GAS

I am new to app script and I am trying to read an email from my inbox. I thought that getThreads would do the job but I still don't fully understand how to use it. When I try to ex

Solution 1:

MyLabel is the label of the email. It depends whether you added a label to a specific email or not. You can use the search method instead.

function myFunction(){
   var label = 'yourLabel'; // if no label, remove the label in search
   // it would be better if you add a label to a specific email for fast and more precise searching
   var searchEmail = GmailApp.search('from:me subject:"' + subject + '" label:' + label + '');
   var threadId = searchEmail[0].getId(); // get the id of the search email
   var thread = GmailApp.getThreadById(threadId); // get email thread using the threadId
   var emailMsg = thread.getMessages()[0]; // get the content of the email
   var emailContent = emailMsg.getPlainBody(); // get the body of the email
   // check using log
   Logger.log(emailContent);
   // how to open log
   // Ctrl + Enter
   // Run this function before checking the log
}

Thanks


Post a Comment for "Trying To Understand GetThreads In GAS"