Sending an email from a Google Form submission
Instructions
- Create a Google Form and fill it out
- Then, go to the Responses tab of the from and press the Sheet icon.
- Create a new linked Sheet
- In that Sheet, select Tools > Script Editor
- Delete the code that’s there and paste the code below
- Change the code to meet your needs. The lines that are commented out ( with //) don’t affect the program and are there to help you.
Video
Watch a two-minute walkthrough here.
Code
function sendFormByEmail(e)
{
// don't touch these
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
// put the email you want to send to below
var email = "ian.hyzy@mavenwave.com";
// to write a message that includes the value of a form, you'll need the following snippet:
// e.namedValues[headers[1]].toString()
// replace the [1] with the number of the column you want to grab the value from
// Remember the first column (A) is 0, B is 1, etc.
// Put your message in quotes and add a plus sign like so:
var message = "The second column value from your form is " + e.namedValues[headers[1]].toString() + "; isn't it neat to automatically create emails?\n";
message += "This is the second line of the email. Use 'message +=' to add more lines like this. Don't forget the semicolon at the end!\n\n";
message += "New lines here don't correlate to new lines in the email. Use the \\n to make a new line in the email.\n";
// you see two slashes in the above line of code, because otherwise the script would make a new line instead of displaying "\n"
// you can do this for the subject as well.
var subject = e.namedValues[headers[2]].toString() + " was your Short Answer selection.";
// Send the email
MailApp.sendEmail(email, subject, message);
// One more step! You're almost done.
// select "Edit" at the top of this screen and select "Current project's triggers"
// add a new trigger. Make the first selection "sendFormByEmail"
// make the second selection "From Spreadsheet"
// make the third selection "on form submit"
// press "save" and you're done!
// This script sends a simple text email. to add formatting and have more control over the appearance,
// you'll need to pass an HTML document as a message. To do so,
// Replace the 'message' variable with your full HTML formatted email, and replace the "send email" line with
// MailApp.sendEmail(email, subject, message, {htmlBody: message});
// Original script by @snipeyhead: https://snipe.net/2013/04/26/email-contents-google-form/
}
Comments
0 comments
Please sign in to leave a comment.