附件未通过电子邮件发送 - GScript

时间:2017-10-19 18:00:40

标签: pdf google-apps-script gmail-api

我正在设立一个生日电子邮件程序,向学生发送问候以及一些生日作品。我将学生生成的作品转换为PDF(无法弄清楚如何从Google云端硬盘中嵌入图片!)并且该脚本主要取自我放在一起的工作证书制作者,该制作者设法将PDF发送给用户。电子邮件发送的是文本但不是pdf。从日志文件中,我可以告诉它正在查找图片文件。任何帮助将不胜感激!

 function SendEmailsOnBirthday() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheetBirthDay = ss.getSheetByName('StudentImport');
 var holder = sheetBirthDay.getRange("A2:K1501").getValues();

 //folder holding birthday pictures
 var folderID = DriveApp.getFolderById("0Bw-NxCTsRPcScnRLb29iZ2tqSEU");

 var Today = new Date();
 var TodayMonth = Today.getMonth();
 var TodayDay = Today.getDay();

//make an array of all the picture file IDs
 var Files = folderID.getFiles();
 var FileList = [];
 while (Files.hasNext()){
   var file = Files.next();
   var FileID = file.getId();
   FileList = FileList.concat([FileID]);
   }

for (i in holder){
  var RandomPicture = Math.floor(Math.random() * FileList.length);

  if (holder[i][10].indexOf("@")>-1){
  //check for month and day!
  var BirthDate = holder[i][7];
  var BirthMonth = BirthDate.getMonth();
  var BirthDay = BirthDate.getDay();
  if (TodayMonth === BirthMonth && TodayDay === BirthDay){
  var First = holder[i][1];
  var Last = holder[i][0];
  var Email = holder[i][10];

  var pdf = DriveApp.getFileById(FileList[RandomPicture]).getAs("application/pdf");

  var subject = "Happy Birthday Fellow Falcon!";
  var body = "We just wanted to wish you a happy birthday from the St. Ignatius Staff and Students!";


   MailApp.sendEmail(Email, subject, body, {noReply: true, attachements: pdf});



}}}}

这是日志文件....如果有帮助:

[17-10-20 09:04:20:454 EDT] Starting execution
[17-10-20 09:04:20:462 EDT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[17-10-20 09:04:20:525 EDT] Spreadsheet.getSheetByName([StudentImport]) [0.063 seconds]
[17-10-20 09:04:20:595 EDT] Sheet.getRange([A2:K1501]) [0.069 seconds]
[17-10-20 09:04:20:665 EDT] Range.getValues() [0.069 seconds]
[17-10-20 09:04:20:871 EDT] DriveApp.getFolderById([0Bw-NxCTsRPcScnRLb29iZ2tqSEU]) [0.199 seconds]
[17-10-20 09:04:20:872 EDT] Folder.getFiles() [0 seconds]
[17-10-20 09:04:21:464 EDT] FileIterator.hasNext() [0.591 seconds]
[17-10-20 09:04:21:465 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:465 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:465 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:466 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:466 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:466 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:467 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:467 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:467 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:468 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:468 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:468 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:469 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:469 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:469 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:470 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:470 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:470 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:471 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:471 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:471 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:471 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:472 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:472 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:472 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:472 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:472 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:473 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:473 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:473 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:474 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:474 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:474 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:474 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:474 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:475 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:475 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:475 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:475 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:476 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:476 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:476 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:477 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:477 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:477 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:477 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:477 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:478 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:478 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:478 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:478 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:479 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:479 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:479 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:480 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:480 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:480 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:480 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:480 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:481 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:481 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:481 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:481 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:482 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:482 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:482 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:483 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:483 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:483 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:483 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:483 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:484 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:484 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:484 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:484 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:485 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:485 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:485 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:486 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:486 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:486 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:486 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:487 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:487 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:487 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:487 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:487 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:488 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:488 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:488 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:489 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:489 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:489 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:490 EDT] FileIterator.next() [0 seconds]
[17-10-20 09:04:21:490 EDT] File.getId() [0 seconds]
[17-10-20 09:04:21:490 EDT] FileIterator.hasNext() [0 seconds]
[17-10-20 09:04:21:641 EDT] DriveApp.getFileById([0Bw-NxCTsRPcSNW5nZXRRWXVYMnM]) [0.151 seconds]
[17-10-20 09:04:21:642 EDT] File.getAs([application/pdf]) [0 seconds]
[17-10-20 09:04:21:685 EDT] MailApp.sendEmail([mperzan@tbcschools.ca, Happy Birthday Fellow Falcon!, We just wanted to wish you a happy birthday from the St. Ignatius Staff and Students!, {noReply=true, attachements=[Blob]}]) [0.041 seconds]
[17-10-20 09:04:22:304 EDT] DriveApp.getFileById([0Bw-NxCTsRPcSUUdaYWFCU19DSHc]) [0.618 seconds]
[17-10-20 09:04:22:304 EDT] File.getAs([application/pdf]) [0 seconds]
[17-10-20 09:04:22:337 EDT] MailApp.sendEmail([mperzan@tbcschools.ca, Happy Birthday Fellow Falcon!, We just wanted to wish you a happy birthday from the St. Ignatius Staff and Students!, {noReply=true, attachements=[Blob]}]) [0.032 seconds]
[17-10-20 09:04:22:382 EDT] Execution succeeded [1.919 seconds total runtime]

1 个答案:

答案 0 :(得分:0)

似乎从您添加的日志中,检索要发送的文件blob可以正常工作。那么下面的修改呢?

来自:

从您添加的日志中,您的当前脚本似乎在下面。

MailApp.sendEmail([Email, subject, body, {noReply=true, attachements=[Blob]}])

致:

MailApp.sendEmail({
  to: Email,
  subject: subject,
  body: body,
  noReply: true,
  attachments: [pdf]
});

参考:

  • MailApp.sendEmail()的详细信息为here

如果这不起作用,我很抱歉。