简体   繁体   English

例外:无法发送电子邮件:没有收件人

[英]Exception: Unable to send e-mail: no recipient

Honestly I'm lost.老实说,我迷路了。 I'm trying to send email responses from a spreadsheet, found this script, edited it a bit and now it won't send emails.我正在尝试从电子表格发送 email 响应,找到此脚本,对其进行了一些编辑,现在它不会发送电子邮件。 This goes beyond my knowledge so now I'm here.这超出了我的知识范围,所以现在我在这里。 Someone please help me out.有人请帮帮我。

// add menu to Sheet
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  
  ui.createMenu("Send Emails")
  .addItem("Send Email Batch","createEmail")
  .addToUi();
}

/**
 * take the range of data in sheet
 * use it to build an HTML email body
 */
function createEmail() {
  var thisWorkbook = SpreadsheetApp.getActiveSpreadsheet();
  var thisSheet = thisWorkbook.getSheetByName('Formulierreacties 1');
  var startRow = 2;  
  var numRows = thisSheet.getLastRow();

  // get the data range of the sheet
  var allRange = thisSheet.getRange(startRow, 1, numRows, thisSheet.getLastColumn());
  
  // get all the data in this range
  var allData = allRange.getValues();
  
  // get the header row
  var headers = allData.shift();
  
  // create header index map
  var headerIndexes = indexifyHeaders(headers);
  
  allData.forEach(function(row,i) {
    if (!row[headerIndexes["Status"]]) {
      var   htmlBody = 
        `Hi,

          Thanks for filling in the warning appeal!

            Your answers:

              What is your user ID?
              ${row[headerIndexes["What is your user ID?"]]}

              What was the case ID of your warning?
              ${row[headerIndexes["What was the case ID of your warning?"]]}

              What was the reason for your warning?
              ${row[headerIndexes["What was the reason for your warning?"]]}

              How many points were you warned for?
              ${row[headerIndexes["How many points were you warned for?"]]}

              Did this warning lead to you being banned from the server?
              ${row[headerIndexes["Did this warning lead to you being banned from the server?"]]}

              Why do you think this warning should be lifted?
              ${row[headerIndexes["Why do you think this warning should be lifted?"]]}

                Reply:

                ${row[headerIndexes["Custom Reply"]]} 
 
                  Have a great day.

                    Regards,

                      JavaScript Universe Management Team`;
      
      var timestamp = sendEmail(row[headerIndexes["E-mailadres"]],htmlBody);
      thisSheet.getRange(i + 2, headerIndexes["Status"] + 1).setValue(timestamp);
    }
    else {
      Logger.log("No email sent for this row: " + i + 1);
    }
  });
}
  

/**
 * create index from column headings
 * @param {[object]} headers is an array of column headings
 * @return {{object}} object of column headings as key value pairs with index number
 */
function indexifyHeaders(headers) {
  
  var index = 0;
  return headers.reduce (
    // callback function
    function(p,c) {
    
      //skip cols with blank headers
      if (c) {
        // can't have duplicate column names
        if (p.hasOwnProperty(c)) {
          throw new Error('duplicate column name: ' + c);
        }
        p = index;
      }
      index++;
      return p;
    },
    {} // initial value for reduce function to use as first argument
  );
}

/**
 * send email from GmailApp service
 * @param {string} recipient is the email address to send email to
 * @param {string} body is the html body of the email
 * @return {object} new date object to write into spreadsheet to confirm email sent
 */
function sendEmail(recipient,body) {
  
  GmailApp.sendEmail(
    recipient,
    "Thanks for filling in the warning appeal!", 
    "",
    {
      htmlBody: body
    }
  );
  
  return new Date();
}

There's probs a lot wrong with this, but I just don't see it.这可能有很多问题,但我只是不明白。 I'm gonna look into it more, but I hope the wonderful smart developers of (Heaven) stackoverflow could see the issue and resolve it for me.我会更多地研究它,但我希望 (Heaven) stackoverflow 的优秀聪明的开发人员能够看到这个问题并为我解决它。 I normally don't ask to be spoon feed, but I have been staring at this for about 2 hours, got a headache, and officially lost and feeling like a complete and utter idiot.我通常不要求当勺子喂食,但我已经盯着这个看了大约 2 个小时,头疼,正式迷路了,感觉自己像个彻头彻尾的白痴。

Instead pushing lines and lines of code and realize at the end of the week that nothing work, start to code step by step.取而代之的是推动一行又一行的代码,并在周末意识到没有任何工作,开始一步一步地编写代码。 Just try to make this line working:试着让这条线工作:

GmailApp.sendEmail( "put your email here", 'this is a test', "and it work");

If it doesn't work, maybe:如果它不起作用,可能是:

  • GmailApp is not initialized, so the mail is not send GmailApp 未初始化,因此邮件未发送
  • you see no mail cause they goes into spam, try to send them to a mailer without any spam system.您看不到邮件,因为它们进入垃圾邮件,请尝试将它们发送到没有任何垃圾邮件系统的邮件程序。

If it works, change you call, you're content is no html, maybe this is the mistake如果有效,请更改您的电话,您的内容是没有 html,也许这是错误

GmailApp.sendEmail(
    recipient,
    "Thanks for filling in the warning appeal!", 
    body
  );

If it is no working, add the following line just before:如果它不起作用,请在前面添加以下行:

console.log('Sending email to', recipient);

And check recipient is correct.并检查收件人是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM