Warm tip: This article is reproduced from serverfault.com, please click

Google Apps Script to send different auto-replies based on keyword in email body

发布于 2020-11-29 00:48:38

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". But if it's anything else it will say "Sorry, your keyword was not recognized". Preferably would like to add more than one keyword in the future...

However, no matter what I send, it is only auto-replying with "This is my test"....

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 = "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();
}
}
}
}
}
Questioner
Scott Neustadt
Viewed
0
Mario 2020-11-29 09:53:11

Explanation / Issues:

You have one obvious issue with your code and one additional modification you need to do according to your comments:

  • Replace if(msg = "test") with if(msg == "test") otherwise the first if block statement will be always executed.

  • According to your comments msg is not just a single word but a text. And you want to check if test is between two words. Assuming that these words are this and that you can use the following regular expression to see if test is between this and that:

    const regex = RegExp(/(?<=this.*?)test(?=.*?that)/);
    regex.test(msg) // this returns true if test is between this and that
    

The RegExp.prototype.test() method allows you to validate your regular expression and get true if there is a match between the regular expression and a specified string, in your case msg.

Solution:

function autoReplier() 
{var labelObj = GmailApp.getUserLabelByName('autoreply');
var gmailThreads;
var messages;
const regex = RegExp(/(?<=this.*?)test(?=.*?that)/); //modify this and that if you want to use other words
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 (regex.test(msg)) {
        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();
}}}}
}