Sending with Attachments (getFileById)

一曲冷凌霜 提交于 2019-12-25 16:57:33

问题


I created a Google form with a script that automatically sends the data to a particular email whenever someone completes the form. Everything works fine as the script does what it's supposed to do.

Here's the entire code

function forwardEmail(e) {

  var forwardEmail = "myemail@myemail.com";

  var senderEmail = e.values[1];
  var senderName = e.values[2];
  var senderLocation = e.values[3];
  var senderType = e.values[9];

  var studentName = e.values[4];
  var broadLeafId = e.values[5];
  var cVueId = e.values[6];
  var studentCampus = e.values[10];

  var issueCategory = e.values[8];
  var description = e.values[7];

  var subject = "FS Support: " + issueCategory + " - " + studentName;  
  var message = "\n Response Summary: \n\n Sender Information: \n Contact Name: " + senderName + 
      "\n Contact Email: " + senderEmail + 
      "\n Location: " + senderLocation + 
      "\n Employee Type: " + senderType + 
      "\n\n Student Information: \n Student Name: " + studentName +   
      "\n BroadLeaf ID: " + broadLeafId +
      "\n CampusVue ID: " + cVueId +
      "\n Student Campus: " + studentCampus +
      "\n\n Incident Information: \n Issue Category: " + issueCategory +   
      "\n Comments: " + description + 

 MailApp.sendEmail(forwardEmail, subject, message);

The code above works as it is.

Now, I want to use the same form/script to include/send attachments as well. I got the idea from this tutorial https://www.youtube.com/watch?v=f2xPgUhcPqc. Instead of assigning a "static" file using a specific ID, I wanted the script to get the ID of the file that is uploaded with the form and include it in the same email. So I modified the script:

function forwardEmail(e) {

  var forwardEmail = "myemail@myemail.com";

  var senderEmail = e.values[1];
  var senderName = e.values[2];
  var senderLocation = e.values[3];
  var senderType = e.values[9];

  var studentName = e.values[4];
  var broadLeafId = e.values[5];
  var cVueId = e.values[6];
  var studentCampus = e.values[10];

  var issueCategory = e.values[8];
  var description = e.values[7];

  var fileUrl = e.values[11];
  var fileId = fileUrl.split('id=')[1];
  Logger.log(fileId)

  var subject = "FS Support: " + issueCategory + " - " + studentName;  
  var message = "\n Response Summary: \n\n Sender Information: \n Contact Name: " + senderName + 
      "\n Contact Email: " + senderEmail + 
      "\n Location: " + senderLocation + 
      "\n Employee Type: " + senderType + 
      "\n\n Student Information: \n Student Name: " + studentName +   
      "\n BroadLeaf ID: " + broadLeafId +
      "\n CampusVue ID: " + cVueId +
      "\n Student Campus: " + studentCampus +
      "\n\n Incident Information: \n Issue Category: " + issueCategory +   
      "\n Comments: " + description + 
      "\n\n Attachment: " + fileUrl + 
      "\n Google Doc ID: " + fileId;

  var attachment = DriveApp.getFileById(fileId);
  var attachment_type = attachment.getAs(MimeType.JPEG);

  MailApp.sendEmail(forwardEmail, subject, message, {attachments: [attachment_type]});

The logger portion works as it is able to extract the ID, the problem is it just stops sending emails when I add and modify this portion:

  var attachment = DriveApp.getFileById(fileId);
  var attachment_type = attachment.getAs(MimeType.JPEG);

  MailApp.sendEmail(forwardEmail, subject, message, {attachments: [attachment_type]});

Does anyone have any idea on how to make this work? I am thinking of doing a "time-driven" trigger (instead of "on form suhbmit) to make sure that the file is indeed in the Google Drive before the email is sent.


回答1:


Try this:

var file = DriveApp.getFileById(FileId);

var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');

MailApp.sendEmail('myemail@myemail.com', 'Attachment example', '', {
     attachments: [file.getAs(MimeType.JPEG), blob]});


来源:https://stackoverflow.com/questions/41875637/sending-with-attachments-getfilebyid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!