![](/img/trans.png)
[英]Extracting data from gmail into a spreadsheet using Google Apps scripts
[英]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。
我相信你的目標如下。
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.