Google Script Lesson 8 – Modify Doc and Send Email

This example builds on the previous one, but in addition to appending the text to a Google Doc, it emails the student with both the feedback and a link to the doc. Click here to open it and make a copy for yourself. It does so with an additional 4 commands (and comments) at the end of the appendAndSend function:

 // Get the URL of the document.
  var url = doc.getUrl();

  // Get the name of the document to use as an email subject line.
  var subject = "Feedback: " + doc.getName();

  // Construct email body; feedback message and the url to doc
  var body = message + "\n\nLink to your doc: " + url;
  
  // Send email
  GmailApp.sendEmail(studentEmail, subject, body);

Notice that we are running two new methods on ‘doc’: getUrl and getName. To see the full list of methods available, type doc followed by a period ‘.’ or go to https://developers.google.com/apps-script/reference/document/document

It is a good practice to create variables for the ‘to’, ‘subject’, and ‘body’ fields of the email. Once those are created, you can use the GmailApp and the sendEmail message to actually send the email.

I would recommend replacing the test email, johnnysmith@school.edu, with your own email to test this function.

Challenge

How would you adjust this program to send the message to a parent address as well as the student? Go through the program step by step, adding a parentEmail value to the data array, passing it to appendAndSend, receive it, and then email it.

If you have a second email account, use one to test the student email and the second to test a parent email.