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.
Try this: