0
votes

This is a similar question to this link, but slightly different: Skipping blank emails in Google Apps Script Mail Merge

I have a form/sheet set up that, when the users fills out the form, it generates a receipt email (does not contain the form contents as some receipts do) that is then sent to the recipient. However when the user leaves this field blank I get a form trigger error, which is understandable why the script didn't finish. I am trying to figure out how to keep the script from attempting to send an email when the recipient/email field is blank. Unfortunately, making the email field required on the form is not an option since, oddly, not everyone would have an email address (if this were an option I would certainly just require an email address to be entered).

I have tried the following code snippet based on the link provided above.

   // Send Email to recipient(s) declared above in @var sendEmail
   if (e.values[11] != null) { 

var sendEmail = e.values[11]; //email field column 
var subject = "subject message";
var body = "body message";

MailApp.sendEmail(sendEmail, subject, body, {
  name: "Community Home Health Care",
  body: body,
  noReply: true,           
})

I have also tried instead of

       (e.values[11] != null)

using

       (e.values[11] != "")

The remaining code I have omitted (goes above what I have shown) simply takes the form field responses and generates a document converted to a PDF which works as expected. The email section also works, just trying to eliminate the failed script emails I get occasionally.

Thanks

1

1 Answers

0
votes

First is an assumption that 12 items are passed (0 through 11) and the email is the last item to be passed. If that is the case, then test that the item is defined with:

if(typeof e.values[11] !== 'undefined')

If you are using the Mail Merge Tutorial linked to in the post you linked to, and using the getRowsData() function to get your form responses, you should be able to use the Header of the column containing the email address such as e.values.emailAddress and get:

if(typeof e.values.emailAddress  !== 'undefined')

This may vary based on how your data is defined.