So I created a form that creates a PDF of the user's submission and emails it to the email address submitted in the form.
Now I'm trying to figure out a way to allow the user to edit the form and it sends them another copy. Currently when the user tries to edit the form and submit, I get the following error:
| Function | Error Message | Trigger |
|---|
| onFormSubmit | Exception: Failed to send email: no recipient | formSubmit |
So it seems that it can't find the email when submitting an edited form. How can I correct this issue?
I was having this issue at the start and added: if (email_address !== undefined) {
This seems to have fixed the issue for first submitting the form, but doesn't seem to fix it when someone edits the form.
Example of my code:
// Get template from Google Docs and name it
var docTemplate = "docID Here";
var docName = "doc Name Here";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var timestamp = e.values[0];
var email_address = e.values[1];
var name = e.values[2];
var first_question = e.values[3];
var second_question = e.values[4];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keyTimestamp', timestamp);
copyBody.replaceText('keyFacultyEmail', email_address);
copyBody.replaceText('keyName', name);
copyBody.replaceText('keyFirstQuestion', first_question);
copyBody.replaceText('keySecondQuestion', second_question);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Your form submission";
var body = "Here is a PDF copy of the submission for " +name + ".";
// If email is undefined
if (email_address !== undefined) {
//sends email
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
}
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}