MailApp.sendEmail上的服务器错误,但电子邮件仍然在Google电子表格脚本中发送

时间:2014-02-26 05:04:05

标签: google-apps-script

以下函数给出错误

  

很抱歉,发生了服务器错误。请稍等一下再试一次。   (第144行,文件“代码”)驳回

每次运行。电子邮件已发送,文件中的第144行是MailApp.sendEmail({ 知道可能需要导致此错误的内容吗?

function sendEmails(e) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dataSheet = ss.getSheetByName("Bookings");

  var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 4);
  var templateSheet = ss.getSheetByName("Templates");
  var emailAddressReplyTo = templateSheet.getRange("B1").getValue();
  var emailSubject = templateSheet.getRange("B2").getValue();
  var emailHTMLTemplate = templateSheet.getRange("C3").getValue();

  // Create one JavaScript object per row of data.
  var objects = getRowsData(dataSheet);

  // For every row object, create a personalized email from a template and send
  // it to the appropriate person.
  for (var i = 0; i < objects.length; ++i) {
    // Get a row object
    var rowData = objects[i];
    rowData.rowNumber = i + 2;
    if (!rowData.action) { // if no state send booking confirmation

    var emailHTMLBody = fillInTemplateFromObject(emailHTMLTemplate, rowData);


    var emailSubjectText = fillInTemplateFromObject(emailSubject, rowData);

    var emailText = processHTML(emailHTMLBody);

    var sendToEmailAddress = rowData.emailAddress;


 MailApp.sendEmail({
     to: sendToEmailAddress,
     replyTo: emailAddressReplyTo,
     subject: emailSubjectText,
     body: emailText,
     htmlBody: emailHTMLBody
   });

      }
   }
  }
}

3 个答案:

答案 0 :(得分:0)

您的脚本中存在一些问题。

下面的代码的目标是什么?

emailAddressReplyTo, htmlBody: emailHTMLBody});

似乎您没有正确复制和粘贴它,或者这就是您的代码失败的原因。

答案 1 :(得分:0)

我将电子邮件地址,电话号码和姓名更改为虚拟值。我记录了传递给MailApp.sendEmail的每个变量,其顺序与传递顺序相同。第一部分是我在SendMail之前添加的行。

Logger.log(sendToEmailAddress);
Logger.log(emailAddressReplyTo);
Logger.log(emailSubjectText);
Logger.log(emailText);
Logger.log(emailHTMLBody];

以下是日志条目:

[14-03-01 20:20:35:253 EST] kmail@gmail.com 
[14-03-01 20:20:35:254 EST] bmail@me.com 
[14-03-01 20:20:35:254 EST] Registration Canada 2014 Completed By K Sum
[14-03-01 20:20:35:254 EST] Hi K Sum, Thanks for your Canada (Editor's note: Body too long to post) Warm wishes, C Nan www.boxwood.org 111.222.3333 (CAN) 111.222.3333 (USA) Click here to Edit your Registration Your submitted details are: Primary Contact Name: K Sum Primary Contact Telephone Number: 444-555-6666 Primary Contact Email Address: kmail@gmail.com Registration(s): Adult Work Study Registration: K Sum Total Registration(s) Cost in US Dollars: $ Registration Fees Break Down as: Adult Registration Cost: $395 Billet Cost: $250 Shuttle Cost: $50 – user284774 Mar 2 at 1:53   
[14-03-01 20:20:35:255 EST] Hi, <br/> <p>(Editor: Body too long to post)</p> <p>Warm wishes,<br/> C Nan<br/> <a href="box.org">www.box.org</a><br/>; 1.2.3 (CAN)<br/> 1.2.3 (USA)<br/> </p> <p><b><a href=docs.google.com/forms/d/1N8l-rlHL_XtcACg/… here to Edit</a></b></p> </p> <h2>Details:</h2> <p><b>Contact:</b> K Sum<br/> <b>Telephone:</b> 444-555-6666<br/> <b>Email:</b> kmail@gmail.com</p> <h3>Registration(s):</h3> <p>Adult: K Sum<br/> <b>Total Cost:</b> $<br/> <b>Registration Fees:</b><br/> A Cost: $395<br/> B Cost: $250<br/> S Cost: $50<br/>  

奇怪的是,即使我只运行一次函数,变量也会多次列出。这个集合似乎缺少通过fillInTemplateFromObject()调用以及变量放置在变量中的信息:

[14-03-01 20:20:35:486 EST] undefined 
[14-03-01 20:20:35:486 EST] bmail@me.com 
[14-03-01 20:20:35:487 EST] Registration to Boxwood Canada 2014 Completed By
[14-03-01 20:20:35:487 EST] Hi , Than (Editor's note: Body too long to post) rg! Warm wishes, C Nan www.box.org 1.2.3 (CAN) 1.2.3 (USA) Click here to Edit your Registration Your submitted details are: Name: Telephone Number: Email Address: Registration(s): Total Cost in : $ Registration Fees Break Down as: 
[14-03-01 20:20:35:487 EST] Hi , <br/> <p>Thanks for you age at <a href="box.org/canada_participant">www.box.org/…; where w dnburg!</p> <p>Warm wishes,<br/> C Nan<br/> <a href="box.org">www.box.org</a><br/>; 1.2.3 (CAN)<br/> 1.2.3 (USA)<br/> </p> <p><b><a href=>Click here to Edit your Registration</a></b></p> </p> <h2>Details are:</h2> <p><b>Name:</b> <br/> <b>Telephone Number:</b> <br/> <b>Email Address:</b> </p> <h3>Registration(s):</h3> <p><br/> <b>Total Cost in :</b> $<br/> <b>Registration Fees Break Down as:</b><br/>

这是Logger调用的所有列表。我不得不删除部分以使其适合这些评论。我删除了直文的部分。我也确实更改了网址,特别是编辑表单条目的网址。代码中没有调用sendEmails函数,并且当前项目中只有一个触发器。我不相信它会运行两次但可能是错误的。

答案 2 :(得分:0)

原来问题是代码试图为电子表格中的第一个空行发送第二封电子邮件。当我更改测试以查看是否需要发送电子邮件以测试空行时,一切运行良好。所以

    if (!rowData.action) { // if no state send booking confirmation

成为:

    if ((!rowData.action) && (rowData.countOfRegistrations > 0.9)) { // if no state AND there are 1 or more registrations send booking confirmation