在 Google AppMaker 中发送电子邮件时出错

Error in sending Email in Google AppMaker

我想在用户单击按钮时发送电子邮件通知。该按钮将调用 sendEmail(widget) 函数并调用客户端脚本,如下所示:

function sendEmail(widget){

  var item = widget.datasource.item;

  google.script.run
    .withFailureHandler(function(error) {
      console.text = error.message;
    })
   .withSuccessHandler(function(result) {
      console.text = 'succeed';
   })
 .sendEmails(item);
}

然后它将在 item 上传递数据源并从服务器脚本调用 sendEmails(item) 函数,如下所示:

function sendEmails(item){  

  var to = item.OwnerEmail;

  var subject = 'Please Review';

  var body = 'hello<br/>my name is Muhammad Alif bin Azali';

  MailApp.sendEmail({
      to: to,
      subject: subject,
      htmlBody: body,
      noReply: true
  });
}

但是当我点击按钮时却出现了以下错误。有帮助吗?

看起来您的邮件正文需要转换为 HTML 正文,

这是示例

function sendEmails(item){  

  var to = item.OwnerEmail;

  var subject = 'Please Review';

  var body = 'hello<br/>my name is Muhammad Alif bin Azali'; 
  var template = HtmlService.createTemplate(body);
  var htmlBody = template.evaluate().getContent();


  MailApp.sendEmail({
      to: to,
      subject: subject,
      htmlBody: htmlBody ,
      noReply: true
  });
}

不幸的是,您不能将任何您想要的参数作为参数传递给您的服务器函数。与服务器的通信有一些 limitations:

...most types are legal, but not Date, Function, or DOM element...

...objects that create circular references will also fail...

A​​pp Maker 的记录肯定违反了这些限制。

有不同的策略来处理这个限制,其中之一是将记录的键作为函数的参数传递。

// Client script
function sendEmail(widget) {
  var item = widget.datasource.item;

  google.script.run
  ...
 .sendEmails(item._key);
}

// Server script
function sendEmails(itemKey) {
  var item = app.models.MyModel.getRecord(itemKey);
  ...
}