I want to resolve the error.

492 views
Skip to first unread message

STORES MJ

unread,
Mar 29, 2022, 4:36:05 AM3/29/22
to Google Apps Script Community
I am trying to send an email, but I get this error.
Exception: Failed to send email: no recipient
sendMail
@ コード.gs:18
The code is as follows

function sendMail(){
  const sheet = SpreadsheetApp.getActiveSheet();
  const lastRow = 100;
  sheet.getLastRow();
  const values = sheet.getRange(1, 1, lastRow, 3).getValues();
  const doc = DocumentApp.openById('1B3VaUtRu5yEcG57i1sOse9WC2Pd4AZv7Hiq8XqLo5YU');
  const docText = doc.getBody().getText();
  const subject = '【テスト】【重要なお知らせ】プライバシーポリシーの改定について'; //メールの件名
  const options
   = {from:"××××@maruzenjunkudo.co.jp", name: '丸善ジュンク堂書店オンラインイベント'}; //送信元メールアドレス
  for(let i = 2; i < lastRow; i++){
    const lastName = values[i][0]; //姓
    const firstName = values[i][1]; //名
    const mailAddress = values[i][2]; //アドレス
    const body = docText
      .replace('{姓}',lastName)
      .replace('{名}',firstName);
    GmailApp.sendEmail(mailAddress, subject, body, options);
  }
}

I would like to know what needs to be corrected.

Clark Lind

unread,
Mar 29, 2022, 7:55:32 AM3/29/22
to Google Apps Script Community
Hello! When you run this script, does it send the first one as expected, and fail on the second email attempt? 
I don't see anything wrong with your code. You can remove the 4th line as it is not used anywhere for anything ( sheet.getLastRow(); ).
Maybe try changing the 'const's in the for loop to "let". Otherwise, I don't see any problems.

Stephen Barker

unread,
Mar 29, 2022, 8:31:57 AM3/29/22
to Google Apps Script Community
Appscript is a zero indexed arrays same as javascript. Your "values" are looking in a 100X3 grid row 1-100 and column A-C. In your for loop you are starting at row 3 (index 2), and column A (index 0) so you would loop thru on the first pass would be:
lastName      = values[2][0] //A3
firstname      = values[2][1] //B3
mailAddress = values[2][1] //C3 
with your first pass of i=2 the script can't find an email in C3 and errors out but if you had one in C3 it would then go to the second pass like:
lastName      = values[3][0] //A4
firstname      = values[3][1] //B4
mailAddress = values[3][1] //C4
And on and on until your last row (100) 

here is an updated code that starts at [0][0] cell A1,B1,C1 and only loops threw data in cells vs a hard coded stop, you could wrap the sendEmail in a .tryCatch but this should get you started or you can add an email starting at C3.

function sendMail(){
  const sheet = SpreadsheetApp.getActiveSheet();
  const lastRow = sheet.getLastRow() -1; //-1 for zero index
  const values = sheet.getRange(1, 1, lastRow, 3).getValues();
  const doc = DocumentApp.openById('1ZuxxUgDmNCXVImHSnEs95fNdUPwFSmLlAlD3WprU-5c');
  const docText = doc.getBody().getText();
  const subject = '【テスト】【重要なお知らせ】プライバシーポリシーの改定について'; //メールの件名
  const options
   = {from: "××××@maruzenjunkudo.co.jp", name: '丸善ジュンク堂書店オンラインイベント'}; //送信元メールアドレス
  for(let i = 0; i < lastRow; i++){ //Start at top of sheet

STORES MJ

unread,
Mar 31, 2022, 8:37:36 PM3/31/22
to Google Apps Script Community
We managed to solve the problem.
Thanks to all of you for your kindness!
2022年3月29日火曜日 21:31:57 UTC+9 stephen...@myoneclay.net:
Reply all
Reply to author
Forward
0 new messages