Why does MailApp.sendEmail from Google Spreadsheet

2019-02-26 23:02发布

问题:

I'm Trying to send email using MailApp.sendEmail in a Google Spreadsheet Script following this example: https://mashe.hawksey.info/2014/07/google-sheets-as-a-database-insert-with-apps-script-using-postget-methods-with-ajax-example/ by @mhawksey

see: https://docs.google.com/spreadsheets/d/1mhY_hJMMPTmxq3TFAUHK0tKFhGYBpzus-dXE3U5TQdc/edit?usp=sharing

The Test web app for your latest code. function works. (email is sent)

But when I try to trigger the sendEmail() via a POST request it does not work.

I'm following the documentation: https://developers.google.com/apps-script/reference/mail/mail-app

And yet when I trigger the sendEmail() function from my script, it does not send an email.

What am I doing wrong?

Looked at:

  • MailApp.sendEmail Not Working?
  • Google script mailapp.sendEmail only works with my email address

But I want to understand if I am doing something wrong...

Full Steps and code: https://github.com/nelsonic/web-form-to-google-sheet#12-google-apps-script

回答1:

Thanks to @ZigMandel for pointing me the right direction ... here are the steps for anyone else facing this issue:

Invoke the MailApp.sendEmail Method

In your Google Spreadsheet Script call:

MailApp.sendEmail("recipient.address@gmail.com", // to
                  "sender.name@gmail.com",       // from
                  "Your Subject Goes Here",      // email subject
                  "What ever you want to say");  // email body

As per the docs: https://developers.google.com/apps-script/reference/mail/mail-app#sendemailto-replyto-subject-body

For this new functionality to work in your script you will need to "Save a New Version" and (Re-)Deploy your App! (simply clicking the "Save" is Not Enough!)

Save a New Version of your Script

It's not immediately obvious but you have to click on "Manage Versions..." see: https://developers.google.com/apps-script/guide_versions

Then create your new version:

Re-Publish the Updated Script as a Web App

Select the latest project version to deploy:

Click "OK". No need to update the script url in your HTML form (it does not change - there are pros & cons to this...)

Done.

For the complete solution see: https://github.com/nelsonic/web-form-to-google-sheet