为 Google 云端硬盘文件夹中的每个附件发送单独的电子邮件
Sending individual email for each attachment in a Google Drive folder
我正在尝试编写一个脚本,该脚本将从特定 Google 驱动器文件夹中获取所有文件并将它们作为电子邮件附件发送。我需要每个附件都是单独的电子邮件。
这是我目前的情况:
function sendEmails() {
var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
var attachments = [];
while (files.hasNext()) {
var file = files.next();
attachments.push(file.getAs(MimeType.JPEG));
Logger.log(attachments)
var emailAddress = "testest@test.com";
var message = "image attached";
var subject = "test";
var attachment = (attachments)
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message,
attachments: attachments
})
}
}
此刻,脚本正在发送电子邮件但连续添加附件。因此,如果该文件夹有 3 个 JPEG,它将发送 3 封电子邮件,一封包含 1 个附件,另一封包含 2 个,第三封包含所有 3 个附件。
我对这一切真的很陌生,但真的很喜欢摆弄它。我对获取文件后要做什么以及如何 link 将它们作为他们自己的个人电子邮件的附件感到困惑。我需要给每个附件一个单独的数组吗?
提前致谢。
push - 将文件添加到堆栈。
pop - 从堆栈中检索并删除文件。
使用 push,您只需将另一个文件添加到当前文件列表即可。
attachments.push(file.getAs(MimeType.JPEG));*
您需要做的是从列表中弹出 个文件以将其删除。因此,与其像这样获取整个列表:
var attachment = (attachments)
你应该这样做:
var attachment = attachments.pop()
目前您的 while {...}
语句开始于向列表中添加另一个文件,然后发送整个文件列表而不删除已发送的文件。
因此您的代码应如下所示:
function sendEmails() {
var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
var attachments = [];
while (files.hasNext()) {
var file = files.next();
attachments.push(file.getAs(MimeType.JPEG));
Logger.log(attachments)
var emailAddress = "testest@test.com";
var message = "image attached";
var subject = "test";
var attachment = (attachments.pop())
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message,
attachments: attachments
})
}
}
就我个人而言,我会删除该列表,因为您不需要存储每个文件,您只想将它们分开发送。
function sendEmails() {
var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
while (files.hasNext()) {
var file = files.next();
// gets curent file only
var attachments = (file.getAs(MimeType.JPEG));
Logger.log(attachments)
var emailAddress = "testest@test.com";
var message = "image attached";
var subject = "test";
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message,
attachments: attachments
})
}
}
我正在尝试编写一个脚本,该脚本将从特定 Google 驱动器文件夹中获取所有文件并将它们作为电子邮件附件发送。我需要每个附件都是单独的电子邮件。
这是我目前的情况:
function sendEmails() {
var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
var attachments = [];
while (files.hasNext()) {
var file = files.next();
attachments.push(file.getAs(MimeType.JPEG));
Logger.log(attachments)
var emailAddress = "testest@test.com";
var message = "image attached";
var subject = "test";
var attachment = (attachments)
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message,
attachments: attachments
})
}
}
此刻,脚本正在发送电子邮件但连续添加附件。因此,如果该文件夹有 3 个 JPEG,它将发送 3 封电子邮件,一封包含 1 个附件,另一封包含 2 个,第三封包含所有 3 个附件。
我对这一切真的很陌生,但真的很喜欢摆弄它。我对获取文件后要做什么以及如何 link 将它们作为他们自己的个人电子邮件的附件感到困惑。我需要给每个附件一个单独的数组吗?
提前致谢。
push - 将文件添加到堆栈。
pop - 从堆栈中检索并删除文件。
使用 push,您只需将另一个文件添加到当前文件列表即可。
attachments.push(file.getAs(MimeType.JPEG));*
您需要做的是从列表中弹出 个文件以将其删除。因此,与其像这样获取整个列表:
var attachment = (attachments)
你应该这样做:
var attachment = attachments.pop()
目前您的 while {...}
语句开始于向列表中添加另一个文件,然后发送整个文件列表而不删除已发送的文件。
因此您的代码应如下所示:
function sendEmails() {
var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
var attachments = [];
while (files.hasNext()) {
var file = files.next();
attachments.push(file.getAs(MimeType.JPEG));
Logger.log(attachments)
var emailAddress = "testest@test.com";
var message = "image attached";
var subject = "test";
var attachment = (attachments.pop())
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message,
attachments: attachments
})
}
}
就我个人而言,我会删除该列表,因为您不需要存储每个文件,您只想将它们分开发送。
function sendEmails() {
var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
while (files.hasNext()) {
var file = files.next();
// gets curent file only
var attachments = (file.getAs(MimeType.JPEG));
Logger.log(attachments)
var emailAddress = "testest@test.com";
var message = "image attached";
var subject = "test";
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message,
attachments: attachments
})
}
}