【问题标题】:getChild of body of gmail email using Goole Apps Scripts (GAS)使用 Google Apps 脚本 (GAS) 获取 gmail 电子邮件正文的 getChild
【发布时间】:2023-03-30 01:55:01
【问题描述】:

问题

我收到自动发送的电子邮件,其中包含特定的客户详细信息。我有重复使用模板回复每封电子邮件的任务。我想利用 Google Apps 脚本自动化该过程。

我在做什么

我已经弄清楚如何收集我正在回复的电子邮件的正文。我正在尝试获取第三段信息并将其存储在变量中。

这是我的代码:

function autoReply() {

//Capturing the automated email  
var queryInbox = "is:unread from:(example@gmail.com)";
var locatedEmail = GmailApp.search(queryInbox);

for (var i in locatedEmail){
  var thread = locatedEmail[i];
  var messages = thread.getMessages();
  var msgBody = messages[i].getBody();
  var clientsEmail = msgBody.getChild('p')[3]; //Attempting to obtain the third paragraph of the body.
  
  if(messages.length === 1) { 
    var body = "<p> The clients email is: " + clientsEmail + "</p>";
  };

  var options = { name: "Temp Name",htmlBody: body };
  thread.reply(body, options);
  thread.markRead();
  thread.moveToArchive();
    
  }
};

注意:附上图片作为上下文。

【问题讨论】:

    标签: javascript regex google-apps-script


    【解决方案1】:

    我相信你的目标如下。

    • 当线程有一条消息时,您要检索电子邮件正文。
    • 您想从电子邮件中检索第 3 段。
    • 您要回复包含检索到的第 3 段的消息。

    修改点:

    • for (var i in unread){,我认为您可能会使用locatedEmail
    • 当你想用messages.length === 1回复消息时,需要修改var msgBody = messages[i].getBody();。因为在您的 for 循环中,索引 i 用于 var thread = locatedEmail[i];var msgBody = messages[i].getBody();
    • 就您而言,我认为getPlainBody() 而不是getBody() 可能更合适。

    当以上几点反映到你的脚本中时,它变成如下。

    修改脚本:

    function autoReply() {
      var queryInbox = "is:unread from:(example@gmail.com)";
      var locatedEmail = GmailApp.search(queryInbox);
      locatedEmail.forEach(thread => {
        var messages = thread.getMessages();
        if (messages.length === 1) {
          var msgBody = messages[0].getPlainBody();
          var clientsEmail = msgBody.split("\n")[2];  // Here, the 3rd paragraph is retrieved.
          var body = "<p> The clients email is: " + clientsEmail + "</p>";
          var options = { name: "Temp Name",htmlBody: body };
          thread.reply(body, options);
          thread.markRead();
          thread.moveToArchive();
        }
      });
    }
    

    参考:

    【讨论】:

    • 谢谢你,它工作得很好。您对第 1 点是正确的,我编辑了一些名称来创建示例,但未能正确匹配它们。
    • @Bjaeg 感谢您的回复。我不得不为我糟糕的英语水平道歉。不幸的是,我无法理解You are correct about point 1 I edited some names to create the example and failed to match them back correctly.。我可以问你关于我的回答问题的细节吗?我想正确理解它并想到修改点。
    • @Bjaeg 有什么可以解决您的问题的吗?如果我的回答对您的情况没有用。我必须道歉并修改它。如果您能合作解决您的问题,我很高兴。我想考虑一下解决方案。
    • @Bjaeg 感谢您的回复。我很高兴你的问题得到了解决。如果您的问题得到解决,请按接受按钮。与您有相同问题的其他人也可以将您的问题作为可以解决的问题。而且我认为您的问题和解决方案将对他们有用。
    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 2012-07-25
    • 2016-05-05
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多