Search code examples
google-apps-scriptgoogle-sheetsgoogle-docsgoogle-apps

Why does MailApp.sendEmail from Google Spreadsheet Script not Work?


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)

12 8-test-your-code

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:

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


Solution

  • 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("[email protected]", // to
                      "[email protected]",       // 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

    19 google-script-no-save-new-version

    Then create your new version:

    20 google-script-save-new-version

    Re-Publish the Updated Script as a Web App

    20 a-publish

    Select the latest project version to deploy:

    21 deploy-new-version

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

    22 1-deploy-as-web-app

    Done.

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