简体   繁体   中英

If includes “test” between two specific characters

Basically I am trying to setup different auto-replies based on the content of the email body. Below is my current code... In this case, I am trying to set it up so that if an email was sent with the body "test" it will auto-reply with "This is my test". However, I did a print of msg to find out that the body of the email contained a lot more than just "test":

<https://voice.google.com>
test
YOUR ACCOUNT <https://voice.google.com> HELP CENTER
<https://support.google.com/voice#topic=1707989> HELP FORUM
<https://productforums.google.com/forum/#!forum/voice>
This email was sent to you because you indicated that you'd like to receive
email notifications for text messages. If you don't want to receive such
emails in the future, please update your email notification settings
<https://voice.google.com/settings#messaging>.
Google LLC
1600 Amphitheatre Pkwy
Mountain View CA 94043 USA

The text around "test" will always be the same. The below code works.... however, I would like to specify that it must include "test" between "google.com>" and "YOUR". I've tried a few different things with no luck...

Any thoughts on how to fix this?

function autoReplier() 
{var labelObj = GmailApp.getUserLabelByName('autoreply');
var gmailThreads;
var messages;
var sender;for (var gg = 0; gg < labelObj.getUnreadCount(); 
gg++) {
gmailThreads = labelObj.getThreads()[gg];
messages = gmailThreads.getMessages();
for (var ii = 0; ii < messages.length; ii++) 
  {if (messages[ii].isUnread()){msg = 
messages[ii].getPlainBody();
    if(msg.includes(`test`)) {
        sender = 
messages[ii].getFrom();MailApp.sendEmail(sender, "Auto Reply", "This is my test");                                                                     
        messages[ii].markRead();
        messages[ii].moveToTrash();
    } else {
    sender = 
messages[ii].getFrom();MailApp.sendEmail(sender, "Auto Reply", "Sorry, your keyword was not 
recognized");                                                                         
        messages[ii].markRead();
        messages[ii].moveToTrash();
}
}
}
}
}

Explanation:

I tried to replicate your problem and I think I did.

You could use a regular expression to achieve your goal but this solution is simpler and it worked for me so I hope it will do for you.

  • Split msg by empty spaces and given the structure of your email, you can check for this:

     msg.split(' ')[0] == '<https://voice.google.com>\r\ntest\r\nYOUR'
  • I also formatted your code because it is indeed very confusing.

Solution:

function autoReplier() {
  var labelObj = GmailApp.getUserLabelByName('autoreply');
  var gmailThreads;
  var messages;
  var sender;
  for (var gg = 0; gg < labelObj.getUnreadCount(); gg++) {
     gmailThreads = labelObj.getThreads()[gg];
     messages = gmailThreads.getMessages();
     for (var ii = 0; ii < messages.length; ii++){
        if (messages[ii].isUnread()){
           var msg = messages[ii].getPlainBody();
           var check = msg.split(' ')[0] == '<https://voice.google.com>\r\ntest\r\nYOUR';
           if(check) {
              sender = messages[ii].getFrom();
              MailApp.sendEmail(sender, "Auto Reply", "This is my test");                                                                     
              messages[ii].markRead();
              messages[ii].moveToTrash();
           } 
           else {
              sender = messages[ii].getFrom();
              MailApp.sendEmail(sender, "Auto Reply", "Sorry, your keyword was not recognized");                                                                         
              messages[ii].markRead();
              messages[ii].moveToTrash();
           }
        }
     }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM