英文:
Transfer all mails of specific label to another mail (apps script)
问题
我想创建一个脚本,将特定标签的电子邮件转发到另一个电子邮件地址。
我已经开始编写我的代码,但肯定有错误,因为我从Logger.log中没有收到任何邮件。
你能帮我解决这个问题吗?
function searchLabels() {
// 1. 常量和变量的声明
const ui = SpreadsheetApp.getUi();
var label, threads, msgs;
var mail = "test@123.com";
// 2. 从标签获取邮件
label = ui.prompt("在这里输入标签:");
threads = GmailApp.search('label:' + label.getResponseText());
msgs = GmailApp.getMessagesForThreads(threads);
// 3. 将邮件转发到另一个地址
for (let i = 0; i < msgs.length; i ++){
GmailApp.sendEmail(mail,msgs[i]);
}
}
英文:
I'd like to create a script to transfer emails from a specific label to another email address.
I've started to write my code but there must be an error somewhere as I'm not getting any mail from my Logger.log.
Can you please help me with this?
function searchLabels() {
// 1. Declaration of constants and variables
const ui = SpreadsheetApp.getUi();
var label, threads, msgs;
var mail = "test@123.com"
// 2. Get messages from label
label = ui.prompt("Write your label here : ");
threads = GmailApp.search('label:' + label.getResponseText());
msgs = GmailApp.getMessagesForThreads(threads);
// 3. Transfer msgs to another mail
for (let i = 0; i < msgs.length; i ++){
GmailApp.sendEmail(mail,msgs[i]);
}
}
答案1
得分: 1
以下是翻译好的内容:
官方文档中的getMessagesForThreads(threads)
部分如下所示。
> 返回
> GmailMessage[][]
— 一个包含消息数组的数组,外部数组中的每个项目对应一个线程,内部数组包含该线程中的消息
在您的脚本中,msgs[i]
是包含消息的数组。此外,在GmailApp.sendEmail(mail,msgs[i]);
的情况下,参数为sendEmail(recipient, subject, body, options)
。我认为这可能是您当前问题的原因。
而且,当您想要传递这些消息时,我认为GmailMessage类的forward(recipient)
可能可以使用。
如果您希望通过包括这些修改点来实现“将特定标签的所有邮件转发到另一个邮件”,那么以下修改如何?
从:
for (let i = 0; i < msgs.length; i ++){
GmailApp.sendEmail(mail,msgs[i]);
}
到:
for (let i = 0; i < msgs.length; i++) {
for (let j = 0; j < msgs[i].length; j++) {
msgs[i][j].forward(mail);
}
}
或者
msgs.forEach(thread => {
thread.forEach(message => {
message.forward(mail);
});
});
参考文档:
英文:
The official document of getMessagesForThreads(threads)
says as follows.
> Return
> GmailMessage[][]
— an array of arrays of messages, where each item in the outer array corresponds to a thread and the inner array contains the messages in that thread
In your script, msgs[i]
is an array including the messages. And also, in the case of GmailApp.sendEmail(mail,msgs[i]);
, the arguments are sendEmail(recipient, subject, body, options)
. I thought that this might be the reason for your current issue.
And, when you want to transfer the messages, I thought that forward(recipient)
of Class GmailMessage might be able to be used.
If you want to achieve "Transfer all mails of specific label to another mail" by including these modification points, how about the following modification?
From:
for (let i = 0; i < msgs.length; i ++){
GmailApp.sendEmail(mail,msgs[i]);
}
To:
for (let i = 0; i < msgs.length; i++) {
for (let j = 0; j < msgs[i].length; j++) {
msgs[i][j].forward(mail);
}
}
or
msgs.forEach(thread => {
thread.forEach(message => {
message.forward(mail);
});
});
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论