簡體   English   中英

使用 Goole Apps 腳本 (GAS) 獲取 gmail email 主體的子項

[英]getChild of body of gmail email using Goole Apps Scripts (GAS)

問題

我收到了帶有特定客戶詳細信息的自動電子郵件。 我有重復的任務,即使用模板回復每個 email。 我想使用 Google Apps 腳本自動化該過程。

收件箱電子郵件示例

我在哪里

我已經弄清楚如何收集我正在回復的 email 的正文。 我正在嘗試獲取第三段信息並將其存儲在變量中。

這是我的代碼:

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();
    
  }
};

注意:為上下文附加了 Img。

我相信你的目標如下。

  • 當線程有一條消息時,您想檢索 email 的正文。
  • 您想從 email 的消息中檢索第 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();
    }
  });
}

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM