我是应用程序脚本的新手,我正在尝试从收件箱中读取一封电子邮件。我认为getThreads可以完成这项工作,但我仍然不完全理解如何使用它。当我尝试执行我在下面写的代码时,它出现了一个空错误。
在查看documentation of getThreads()
时,他们使用了以下示例:
// Log the subject lines of the threads labeled with MyLabel
var label = GmailApp.getUserLabelByName("MyLabel");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
Logger.log(threads[i].getFirstMessageSubject());
}
"MyLabel“代表什么?
这是我尝试的代码,但失败了
function myFunction() {
var label = GmailApp.getUserLabelByName('bobtheman@gmail.com');
var threads = label.getThreads();
for (var t in threads) {
var thread = threads[t];
// Gets the message body
var message = thread.getMessages()[0].getPlainBody();
}
GmailApp.sendEmail('barbrabat@gmail.com', 'hola', message)
}
发布于 2018-06-18 15:20:07
MyLabel是电子邮件的标签。这取决于您是否在特定电子邮件中添加了标签。您可以改用搜索方法。
function myFunction(){
var label = 'yourLabel'; // if no label, remove the label in search
// it would be better if you add a label to a specific email for fast and more precise searching
var searchEmail = GmailApp.search('from:me subject:"' + subject + '" label:' + label + '');
var threadId = searchEmail[0].getId(); // get the id of the search email
var thread = GmailApp.getThreadById(threadId); // get email thread using the threadId
var emailMsg = thread.getMessages()[0]; // get the content of the email
var emailContent = emailMsg.getPlainBody(); // get the body of the email
// check using log
Logger.log(emailContent);
// how to open log
// Ctrl + Enter
// Run this function before checking the log
}
谢谢
https://stackoverflow.com/questions/48479463
复制相似问题