使用 Google Apps 脚本发送带附件的电子邮件失败
Fail to Send an email with attachment using Google Apps Script
function SendEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Name List").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var lr = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
//Logger.log(quotaLeft);
if ((lr-1) > quotaLeft){
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + (lr-1) + "emails. Emails were not sent.");
} else {
for (var i = 2;i<=lr;i++){
var currentName = ss.getRange(i, 1).getValue();
var currentAppNo = ss.getRange(i, 2).getValue();
var currentEmail = ss.getRange(i, 3).getValue();
var messageBody = templateText.replace("{First Name}",currentName).replace("{App No}",currentAppNo);
var subjectLine = "CONGRATULATION ON YOUR VAL APPROVAL " + currentName
var attachmentBody = DriveApp.getFilesByName("THE ROOM SCRIPT.pdf");
MailApp.sendEmail(currentEmail, subjectLine, messageBody)
} //close for loop
} //close else statement
}
我有一个包含电子邮件列表的 Google 电子表格。我想建立一个例程,自动将电子邮件发送到这些电子邮件地址。我还想在此电子邮件中附加 PDF。 PDF 文件位于我的 Google 驱动器上。
这似乎行不通
您可能希望在脚本中更改以下两点。
getFilesByName()
获取具有该名称的文件集合(作为 FileIterator 对象)。如果只有一个这样的文件,您需要将该行更改为
var attachmentBody = DriveApp.getFilesByName("THE ROOM SCRIPT.pdf").next; // To get the first such file
- 正如@ross 所说,
sendMail()
函数需要像这样包含附件:
MailApp.sendEmail(currentEmail, subjectLine, messageBody, {
attachments: [attachmentBody.getAs(MimeType.PDF)]
});
function SendEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Name List").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var lr = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
//Logger.log(quotaLeft);
if ((lr-1) > quotaLeft){
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + (lr-1) + "emails. Emails were not sent.");
} else {
for (var i = 2;i<=lr;i++){
var currentName = ss.getRange(i, 1).getValue();
var currentAppNo = ss.getRange(i, 2).getValue();
var currentEmail = ss.getRange(i, 3).getValue();
var messageBody = templateText.replace("{First Name}",currentName).replace("{App No}",currentAppNo);
var subjectLine = "CONGRATULATION ON YOUR VAL APPROVAL " + currentName
var attachmentBody = DriveApp.getFilesByName("THE ROOM SCRIPT.pdf");
MailApp.sendEmail(currentEmail, subjectLine, messageBody)
} //close for loop
} //close else statement
}
我有一个包含电子邮件列表的 Google 电子表格。我想建立一个例程,自动将电子邮件发送到这些电子邮件地址。我还想在此电子邮件中附加 PDF。 PDF 文件位于我的 Google 驱动器上。
这似乎行不通
您可能希望在脚本中更改以下两点。
getFilesByName()
获取具有该名称的文件集合(作为 FileIterator 对象)。如果只有一个这样的文件,您需要将该行更改为
var attachmentBody = DriveApp.getFilesByName("THE ROOM SCRIPT.pdf").next; // To get the first such file
- 正如@ross 所说,
sendMail()
函数需要像这样包含附件:
MailApp.sendEmail(currentEmail, subjectLine, messageBody, {
attachments: [attachmentBody.getAs(MimeType.PDF)]
});