1
votes

I have an automation script that handles emails with a particular subject. It was working for a while, but now there is a second type of email that gets the same label and has a very similar subject. The subject I want is RITM12345 Approval Request where 12345 can be any number. The subject I don't want is CHG12345 Approval Request where 12345 can be any number.

I wanted to initially put a regular expression inside the GmailApp.search function, but the regex is taken as a literal string. I can't seem to find any documentation on how to filter this during the email search. I tried GmailApp.search('subject:"Approval Request"-CHG'), but this does not work because the rest of the CHG string has numbers appended to it.

Currently, I am doing this:

  var threadSubject = /RITM([0-9]+) Approval Request/;      
  var threads = GmailApp.search('from:"email.com" is:unread subject: "Approval Request" label:"Approval Request"', 0,1);

  if (threads.length > 0) {
    for (var i=0; i < 1; i++) {
      var messages = GmailApp.getMessagesForThread(threads[i]);
        for (var j=0; j < messages.length; j++ ) {
          var message = threads[i].getMessages()[j];                        
          var subject = message.getSubject();              
        }
        if (subject.match(threadSubject)) { 
          do automation code here if it matches my regex, then mark thread as read
        } 

The code re-runs in a minute and finds next unread message

The inherent problem with this code is that I only want to find unread messages with the desired subject. Currently, this code is only locating the unread CHG12345 Approval Request thread over and over again (since it is the first thread found). Marking this unwanted thread read or changing this unwanted threads label are also not an option.

Also, one other thing to note. I am only grabbing one email at a time in my loop and the script triggers every minute. This is done for logging reasons and I was also getting bugs when the script would try to handle 20+ emails all at once

2

2 Answers

1
votes

I tried on my email and it works pretty well for me. I was surprised how long it takes to run.

function getMyMessages() 
{      
  var threads = GmailApp.search('from:"email.com" subject:"Approval Request"');//gets Approval Requests
  var s='';
  for(var i=0;i<threads.length;i++)
  {
     var messages = GmailApp.getMessagesForThread(threads[i])
     for (var j=0; j<messages.length;j++) 
     {
       if(messages[j].isUnread())
       {   
         var message=messages[j].getBody()
         var subject=messages[j].getSubject();
         var from=messages[j].getFrom();
         var to=messages[j].getTo();
         if(subject.match(/RITM\d+/))//requires RITM in the subject or it won't be displayed
         {   
           s+=Utilities.formatString('<br />From: %s<br />To: %s<br />Subject: %s<br />Message: %s<br /><hr width="100%">', from,to,subject,message);
           messages[j].markRead();//if found it marks as read
         }
       }
     } 
  }
  var ui=HtmlService.createHtmlOutput(s).setWidth(1000);
  SpreadsheetApp.getUi().showModelessDialog(ui, 'My Messages');//displays messages that pass the filter.
}
1
votes

Can you try something like this?

var threads = GmailApp.search('from:"email.com" is:unread subject: "Approval Request" label:"Approval Request"'); // remove 0 and 1

threads.forEach(function (t) {
  if (t.getFirstMessageSubject().match(/RITM\d+/) {
    toDoWithCorrectMessage(t);
  }
});

I've successfully used this code

function myFunction() {
  var threads = GmailApp.search('from: "[email protected]"');
  Logger.log(threads.length);
  var counter = 0;
  threads.forEach(function (t) {
    if (t.getFirstMessageSubject().match("50%")) {
       counter++;
     }
  });
  Logger.log(counter);  
}

to search from a daily newsletter all the mail with "50%". This is the result:

[17-10-26 19:25:32:914 CEST] 214.0
[17-10-26 19:26:01:963 CEST] 3.0

32 seconds to run through 214 mails. Do you have more mails?

With search parameter as 'is: unread' only:

[17-10-26 10:30:56:822 PDT] 500.0
[17-10-26 10:32:01:373 PDT] 4.0

if the search is repeated it is actually cached, thus it is really faster (here a third test with again all unread email)

[17-10-26 20:04:03:824 CEST] Start
[17-10-26 20:04:04:681 CEST] 500.0
[17-10-26 20:05:11:128 CEST] 4.0