admin管理员组文章数量:1026989
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 badge2 Answers
Reset to default 4The 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
MailApp.sendEmail(recipient, subject, body, options)
- https://developers.google/apps-script/guides/triggers/installable
- 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
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 badge2 Answers
Reset to default 4The 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
MailApp.sendEmail(recipient, subject, body, options)
- https://developers.google/apps-script/guides/triggers/installable
- 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
本文标签: google apps scriptForm submitted file MailAppStack Overflow
版权声明:本文标题:google apps script - Form submitted file MailApp - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745661176a2161900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论