google apps script - Form submitted file MailApp - Stack Overflow

I cant figure out how to get a file that is submitted in a form to send as a email.function onFormSubm

I cant figure out how to get a file that is submitted in a form to send as a email.

function onFormSubmit(e) {
  var responses = e.response.getItemResponses();
  var fileItem = responses[1]; // Assuming the file is the first response
  var responses = e.response.getItemResponses().map(item => item.getResponse());
  // Get the file response
  var uFile = fileItem.getResponse();

  // Email subject and body
  var subject = "New File Uploaded";
  var body = "A new file has been uploaded.\n\n";

  if (uFile) {
    // If a file is uploaded, include the file details in the email

    
    body += responses
  MailApp.sendEmail("[email protected]", subject, body,
    { attachments: uFile });
    Logger.log("File sent successfully!");
  } 
}

I have looked over a few forms to find anything but nope. So I come hoping some one can help.

I cant figure out how to get a file that is submitted in a form to send as a email.

function onFormSubmit(e) {
  var responses = e.response.getItemResponses();
  var fileItem = responses[1]; // Assuming the file is the first response
  var responses = e.response.getItemResponses().map(item => item.getResponse());
  // Get the file response
  var uFile = fileItem.getResponse();

  // Email subject and body
  var subject = "New File Uploaded";
  var body = "A new file has been uploaded.\n\n";

  if (uFile) {
    // If a file is uploaded, include the file details in the email

    
    body += responses
  MailApp.sendEmail("[email protected]", subject, body,
    { attachments: uFile });
    Logger.log("File sent successfully!");
  } 
}

I have looked over a few forms to find anything but nope. So I come hoping some one can help.

Share Improve this question edited Nov 16, 2024 at 13:09 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Nov 16, 2024 at 10:00 user27821343user27821343 11 silver badge1 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 4

The attachments property of the options object of MailApp.sendEmail should be an array of Interface BlobSource objects.

The example below works for a Google Form with File Upload as the first question.

function formSubmitHandler(e) {
    const formResponse = e.response;
    // Response to the first question
    const itemResponse = formResponse.getItemResponses()[0];
    if(itemResponse.getItem().getType() === FormApp.ItemType.FILE_UPLOAD){        
        const file = DriveApp.getFileById(itemResponse.getResponse());
        const recipient = "Please provide an email address here";
        const subject = "Demo sending file uploded as attachmet";
        const body = "Please checkout the attachment";
        MailApp.sendEmail(recipient, subject, body, {attachments: [file]});
        Logger.log("Email sent");
    } else {
        Logger.log("Email was not sent");
    }
}

Please note that the above code should be added to the Apps Script project bound to a Form, and an installable trigger should be created.

References

  1. MailApp.sendEmail(recipient, subject, body, options)
  2. https://developers.google/apps-script/guides/triggers/installable
  3. https://developers.google/apps-script/guides/triggers/events

Sending Email on Form Submit Using GmailApp

A modified version of the script which would be for the form it can handle multiple questions and that answers would attach in your code: body += responses.

I opted to use GmailApp for the reason for the future if OP wants to use it for managing emails, especially if you plan to work with features like threads, labels, and more.

Code.gs

function onFormSubmit(e) {

  var response = e.response;
  var itemResponses = response.getItemResponses();
  var responses = itemResponses.map(item => item.getResponse().concat("\n"));
  responses.shift()
  var file = itemResponses[0].getResponse();  // returns the file id

  var subject = "New File Uploaded";
  var body = "A new file has been uploaded.\n\n";

  if (file) {
    body += responses.toString().replace(",","");
    GmailApp.sendEmail("[email protected]", subject, body, { attachments: [DriveApp.getFileById(file)] });
    Logger.log("File sent successfully!");
  }
}

Sample Output:

Sample Form:

References:

Installable Triggers

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745661177a4638846.html

相关推荐

  • google apps script - Form submitted file MailApp - Stack Overflow

    I cant figure out how to get a file that is submitted in a form to send as a email.function onFormSubm

    22天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信