【问题标题】:Trying To Send Creation Notification Email尝试发送创建通​​知电子邮件
【发布时间】:2017-07-21 17:23:16
【问题描述】:

我希望在用户创建包含来自该条目(或小部件)的信息的条目时发送一封通知电子邮件。

例如:

至:groupemail@company.com

主题:项目名称

body:“用户 1 创建了一个状态为进行中的新项目”。


我试图查看 Project Tracker App 以供参考,但我还是迷路了。

我假设我需要创建一个客户端脚本,执行类似下面的函数并将该函数添加到页面片段中“提交按钮”的 onClick 事件以创建新条目,但它不起作用。

提交按钮 onClick 事件:

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

客户端脚本:

    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));
      }
    }

当函数尝试运行时,它说“to”是未定义的,我确定我没有为“widgets.ProjectName.value”之类的东西使用正确的选项。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: google-app-maker


    【解决方案1】:

    没有用于发送电子邮件的客户端 API。您需要将 'sendEmailCreated_' 函数移动到服务器脚本并在 onCreate 模型事件中调用它(从安全角度来看,这是更可取的):

    // 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);
    

    ...或在创建回调中使用 google.script.run(安全性较低,因为您将 Mail API 暴露给最终用户):

    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
      }
    });
    

    如果你不关心安全和错误处理,那么客户端的代码可以简化为:

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

    关于该主题的有用链接:

    【讨论】:

      【解决方案2】:

      除了不使用实际上可以访问 MailApp 的服务器端脚本之外(正如 Pavel 在他的回答中指出的那样),我将指出您收到该错误的原因。

      你正在使用

      sendEmailCreated_(to, subject, body);
      

      来自 OnClick 事件,从未定义 tosubjectbody。相反,当尝试将某些内容从小部件传递到客户端脚本时,您应该使用以下内容:

      doSomething(widget);
      

      (因为您可以从小部件中检查 onClick 是否允许直接访问当前小部件)

      函数类似于

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

      因此,您需要确保您确实定义了要发送给函数的参数。

      如果您使用类似于

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-20
        相关资源
        最近更新 更多