Trying To Send Creation Notification Email

邮差的信 提交于 2020-01-30 08:47:46

问题


I want a notification email to go out when a user creates an entry that contains information from that entry (or widget).

Ex:

to: groupemail@company.com

subject: Project Name

body: "User1 has created a new project with the Status of In Progress".


I tried to look at the Project Tracker App for reference, but I'm still lost.

I assumed I would need to create a Client Script, do something like the function below and add that function to the onClick Event for "Submit Button" in the Page Fragment for creating new entries, but it isn't working.

Submit Button onClick Event:

widget.datasource.createItem();
sendEmailCreated_(to, subject, body);
app.closeDialog();

Client Script:

    function sendEmailCreated_(to, subject, body) {
      var widgets = widget.root.descendants;     
      try {
        MailApp.sendEmail({
          to: 'groupemail@company.com',
          subject: widgets.ProjectName.value,
          body: 'User1 has created a new project with the Status of' + widgets.ProjectStatus.value,
          noReply: true
        });
       } catch (e) {
        // Suppressing errors in email sending because email notifications
        //   are not critical for the functioning of the app.
        console.error(JSON.stringify(e));
      }
    }

When the function tries to run it says "to" is undefined and I'm sure I'm not using the correct option for things like "widgets.ProjectName.value".

Any help would be greatly appreciated.

Thank you.


回答1:


There is no client side API to send emails. You need to move your 'sendEmailCreated_' function to Server Script and call it in onCreate model event (what it is preferable from the security perspective):

// onCreate model event receives about-to-create record
// Undersocre in the end of the function name means that
// function is private (cannot be called from the client)
sendEmailCreated_(record.to, record.subject, record.body);

...or using google.script.run in create callback(what is less secure, since you expose Mail API to the end user):

widget.datasource.createItem({
  success: function(record) {
    google.script.run
          .withSuccessHandler(function() {
            // TODO: Handle success
          })
          .withFailureHandler(function(error) {
            // TODO: Handle failure
          })
          .sendEmailCreated(record.to, record.subject, record.body);
          // sendEmailCreated - is a server side function.
          // It is public (can be called from the client), since
          // it doesn't end with underscore
  },
  failure: function(error) {
     // TODO: Handle failure
  }
});

If you don't care about security and error handling, then the client side of the code can be simplified to this:

widget.datasource.createItem(function(record) {
    google.script.run.sendEmailCreated(record.to, record.subject, record.body);
  });

Usefull links on the topic:

  • App Maker Email Sample
  • App Script Email API
  • App Script private functions



回答2:


In addition to not using server-side script, where MailApp can actually be accessed (as Pavel pointed out in his answer), I'm going to point out why you're getting that error.

You're using

sendEmailCreated_(to, subject, body);

from OnClick event, without ever defining to, subject or body. Instead, when trying to pass something from a widget to client script, you should use something like:

doSomething(widget);

(because the you can check from the widget that onClick allows direct access to the current widget)

And the function would be something like

function doSomething(widget) {
  var variable1 = widget.value;
  doSomethingElse(variable1);
}

So you need to make sure that you actually have defined the parameters you're sending to a function.

Your email example would not have those specific errors (but different ones explained by Pavel partially) if you used something along the lines of

var to = "example@example.com";
var subject = "Example subject";
var body = "Example text body";
sendEmailCreated_(to, subject, body);


来源:https://stackoverflow.com/questions/45224565/trying-to-send-creation-notification-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!