尝试发送大量电子邮件时,“电子邮件无效:电子邮件(第24行,文件“代码”)

时间:2019-09-22 19:52:21

标签: google-apps-script google-sheets

请帮忙,我没有编码经验,我想发送大量电子邮件,但是在尝试运行该功能时,它总是说无效的电子邮件:电子邮件(第24行,文件“代码”)。

// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
 function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1; // First row of data to process
  var numRows = 1718; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 3);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var name = row[0];
    var emailAddress = row[1]; // First column
    var message = 'Hello, '+ name + row[2]; // Second column
    var emailSent = row[4]; // Third column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject = 'Wanting To Represent Your School';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is 
interrupted
      SpreadsheetApp.flush();
    }
  }
}

问题就在这里...     MailApp.sendEmail(emailAddress,主题,消息); 至少这就是我的意思。

此代码的目标是向一所大学询问信息,其中我有一个Google表格,第一列是学校的名称,第二列是学校的电子邮件,第三列是学校的电子邮件成为学校的信息。我只运行了一次函数,它就将电子邮件发送给了两所学校,因为我忘记更改要处理的行数,但是现在我不想再次运行。请帮忙,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

尝试一下:

function sendEmails2() {
  var sheet=SpreadsheetApp.getActiveSheet();
  var startRow=2;
  var dataRange=sheet.getRange(startRow,1,sheet.getLastRow()-1,3);
  var data = dataRange.getValues();
  for (var i=0;i<data.length;i++) {
    var row=data[i];
    var name=row[0];//Column 1
    var emailAddress=row[1]; //Column 2
    var message='Hello, '+ name + row[2]; // Second column
    var emailSent=row[2];//Column 3
    if (emailSent !== "EMAIL_SENT") { 
      var subject = 'Wanting To Represent Your School';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue("EMAIL_SENT");
    }
  }
}