MailApp.sendEmail函数运行无错误,但没有发送电子邮件 [英] MailApp.sendEmail function runs without errors but no emails are sent

查看:94
本文介绍了MailApp.sendEmail函数运行无错误,但没有发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下脚本运行时没有任何可见的错误,但是没有发送电子邮件。有什么想法吗?

The below script runs without any visible error, but no emails are sent. Any ideas as to why?

function sendEmailLoop() {

  var sheets =   SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach(function(sheet) {
    var range = sheet.getDataRange();

    if (sheet.getName() == "Summary") //Disregard tab named 'Summary' 
    {      
    }
    else {    
      var range = sheet.getDataRange(); //to set the range as array
      var values = range.getDisplayValues(); //to get the value in the array
      var lastRow = range.getLastRow();
      var ss = SpreadsheetApp.getActiveSpreadsheet();  //declare the spreadsheet
      var sheet = ss.getSheetByName("Sheet1");
      var Title = values[0][0];         //[Title] cell A1
      var URL = values[0][1];           //[URL] cell B1
      var i;
      var logContent = '';

      for (i = 3; i < lastRow; i++) {   
        var Name = values[i][0];     //[Name] cell A++
        var Email = values[i][1];        // [Email] cell B++

        Logger.log('to: ' + Email);
        Logger.log('subject: ' + Name + Title + 'Test');
        Logger.log('message: ' + 'This is a test message for the training that can be found at ' + URL);
        /*
        MailApp.sendEmail({
          to: Email,
          subject: Name + Title + 'Test',
          message: 'This is a test message for the training that can be found at ' + URL});
        */
      }; //end for loop - email tab data
    };   // end 'else'
  }); // end function(sheet)       
} // end SendEmailLoop()  

这是成功执行的Stackdriver日志(成功表示没有错误,但仍未发送电子邮件):

Here is the Stackdriver log of a successful execution (success meaning no errors, but still no emails are sent):

与脚本关联的电子表格的结构:

The structure of the spreadsheet associated with the script:

注意-此脚本的早期版本不包含 sheets.forEach()方法调用(即遍历电子表格的每个标签),并且电子邮件发送得很好。

Note - an earlier version of this script didn't include the sheets.forEach() method call (ie, to loop through each tab of the spreadsheet) and the emails were sent fine.

是否缺少发送或接收电子邮件的原因是否与我有很多工作表并且此功能正在遍历它们有关?

Could the lack of emails being sent or received be related to the fact that I have many sheets and this function is looping through them?

推荐答案

答案:


您需要取消注释 MailApp 代码。

Answer:

You need to uncomment your MailApp code.

我测试了您的代码,对我来说似乎没有问题,包括收据在电子邮件中,仅需要删除 MailApp 调用周围的代码注释( / * * /

I tested your code and it seems to run without issue for me, including the receipt of the emails, only that the code comments around your MailApp call need to be removed (the /* and the */.

我还建议在事件 .getDataRange中发送电子邮件之前添加条件行。 ()获得看似空的行:

I would also suggest adding a conditional line before you send the email in the event .getDataRange() obtains seemingly empty rows:

if (Email == "") {
  continue;
};
MailApp.sendEmail({
  to: Email,
  subject: Name + Title + 'Test',
  message: 'This is a test message for the training that can be found at ' + URL});

我希望这是

  • JavaScript Comments

这篇关于MailApp.sendEmail函数运行无错误,但没有发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆