Why "Failed to send email: no recipient"? - using MailApp

3,005 views
Skip to first unread message

Lili Park

unread,
May 21, 2019, 8:42:06 PM5/21/19
to Google Apps Script Community
I have a script developed for when a user submits a form.  Its an app script from the spreadsheet attached to form responses.
In the script I send back an acknowledgement email.
Just recently, I have started receiving some error message from Google Script (on some, but not all responses):


Your script, blahblahblah, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.

Start: 5/21/19 4:57 PM
Function: sendResponseToPlayer
Error Message: Failed to send email: no recipient (line 97, file "blahblahblah")
Trigger: formSubmit
End: 5/21/19 4:57 PM

Sincerely,
Google Apps Script


Also recognize, that in my "Sent Mail" box, there is an email sent to the respondent that the script sends email to.

Why would these errors start creeping up?
Why would I get the email in my Sent Mail box if there was an error?
What do I need to change in the script?

Thanks in advance for any insight provided.

function sendResponseToPlayer(e) {

 
var values = e.namedValues;
   
 
var pfname = values['First Name'].toString();
 
var plname = values['Last Name'].toString();
 
var pyn = values['Playing?'].toString();
 
var pem = values['Email Address'].toString();
   
 
// Acknowledge their response
 
var subject = 'Player Response ';
   
 
var hbody = '';
  hbody
+= "<p>Thank you, " + pfname + ". You have answered \"" + pyn + "\" to playing this week.</p>";
   
 
// Add document text
 
var body = DocumentApp.openByUrl('https://docs.google.com/document/d/123abc/edit').getBody();
 
var bodyTextElement = body.editAsText();
 
var bodyString = bodyTextElement.getText();
 
  hbody
+= "<p>" + bodyString + "</p>";
 
 
// Add spreadsheet text
 
var sheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123/edit#gid=0');
 
var values = sheet.getActiveSheet().getDataRange().getDisplayValues();

 
for (var i = 0; i < values.length; i++) { // length of first dimension of 2d array
   
for (var j = 0; j < values[i].length; j++) { // length of dimension of specific row
        hbody
+= values[i][j] + "    ";
   
}
      hbody
+= "<br>";
 
}
 
  hbody
+= "<p>Please see attachments.</p>";
   
 
var fid = '789xyz';
 
var file = DriveApp.getFileById(fid);
   
 
var fcal = 'xyz789';
 
var calfile = DriveApp.getFileById(fcal);

 
var replem = 'm...@gmail.com';

         
MailApp.sendEmail({
            to
: pem,
            replyTo
: replem,
            subject
: subject,
            htmlBody
: hbody,
            attachments
: [file.getAs(MimeType.PDF),calfile.getAs(MimeType.PDF)],
            name
: 'me'
       
});
}


Martin Hawksey

unread,
May 22, 2019, 4:46:21 AM5/22/19
to google-apps-sc...@googlegroups.com
Hi Lili - you might start seeing an error if the form field for the email address has been renamed. Currently your script is looking for a field called 'Email Address'. Has this changed? 

Alan Wells

unread,
May 22, 2019, 8:05:36 AM5/22/19
to Google Apps Script Community
The error message might be wrong.  It does happen.  There may be an error, but the error might not actually be that the email wasn't sent.  You'll need to break your script into more parts, and test things; like is "file" truthy?

var filesToAttach = [];

if (file) {
  file = file.getAs(MimeType.PDF);
  filesToAttach.push(file);
} else {
  throw new Error('There is a problem with file');
}

try{
          MailApp.sendEmail({
            to
: pem,
            replyTo
: replem,
            subject
: subject,
            htmlBody
: hbody,

            attachments
:
filesToAttach,
            name
: 'me'
       
});
} catch(e){
 
  MailApp.sendEmail({
            to
: pem,
            subject: subject,
              htmlBody:'There was an error'
})
}

Lili Park

unread,
May 22, 2019, 11:57:53 AM5/22/19
to Google Apps Script Community
Here's some more info:
  • The script hasn't been changed in 4 weeks.
  • The attachments haven't changed in 4 weeks.
  • I did make a copy of the responses spreadsheet a few days ago, and have deleted that copy
  • the error is now occurring with each form respondent
  • the respondents are not receiving the email
I have checked the limitations on Google Scripts emails and seem to be well within bounds. 
Total attachment size 150MB.  Number of emails sent per day <75.  Number of emails sent per month does not seem to have a limitation.

Martin Hawksey

unread,
May 22, 2019, 12:02:28 PM5/22/19
to google-apps-sc...@googlegroups.com
...but has the Google Form changed?

you might start seeing an error if the form field for the email address has been renamed. Currently your script is looking for a field called 'Email Address'. Has this changed?   
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f9721ca2-5331-4ce2-b45a-05bc53bd6721%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Martin Hawksey

Latest tweet (see how):

"It seems clear that the waves of lawsuits breaking on the shores of the new surveillance fortress are unlikely to alter the behavior of surveillance capitalists." (Zuboff 2015) https://t.co/fTEXuRQK9t

— Billy Meinke-Lau (@billymeinke) May 6, 2019

Lili Park

unread,
May 22, 2019, 12:09:25 PM5/22/19
to Google Apps Script Community
I change the title for the form every week to include a new date.
The fields of the form do not change;  have not changed since inception 4 weeks ago.

Martin Hawksey

unread,
May 24, 2019, 5:01:11 AM5/24/19
to google-apps-sc...@googlegroups.com
... my next guess would have been people are just not filling in the Email Address form field but I guessing that unlikely if it's happening all the time. Without see the Google Form and entire script it's hard to say what is going wrong here. I replicated your form and added the code to the sheet here https://docs.google.com/spreadsheets/d/1hnb6DjpLFcPvQxbY0Xq3IYqRKI89MF3W9QejmoLtVxU/edit#gid=1627632407 commenting out some of the attachment stuff and it worked fine.


Lili Park

unread,
May 24, 2019, 5:26:29 PM5/24/19
to Google Apps Script Community
In the Sent Mail folder, the email that is sent to the recipient clearly shows the recipients email address.  Plus, the spreadsheet of responses also has all the respondents information.

I've modified the script code to extract the email address and other response information directly from the spreadsheet, instead of from the event namedValues.  Just to try something different.

I cannot reproduce the error by myself.  I've studded the code with try...catch statements.  When the script runs again this week, maybe I'll be able to capture more information.

I'll post results as script gets executed over next several days.

Lili Park

unread,
May 24, 2019, 5:40:04 PM5/24/19
to Google Apps Script Community
I also just removed the add-on "Form Builder for Sheets" as it was not used.

Lili Park

unread,
May 24, 2019, 5:47:34 PM5/24/19
to Google Apps Script Community
Here's a couple screenshots of what is happening.


tempsnip1.png
tempsnip2.png

Martin Hawksey

unread,
May 25, 2019, 3:58:38 AM5/25/19
to google-apps-sc...@googlegroups.com
From the log it looks like the script is being executed twice in quick succession but only the first execution has the form values. 

Going by the SO comments on the following thread it looks like you are not the only one having this problem https://stackoverflow.com/q/55188211/1027723

As it appears to be an issue with the form submit you could check if form values are returned and if not break ... or you might want to keep monitoring the error message until it's resolved

Lili Park

unread,
May 25, 2019, 10:16:55 AM5/25/19
to Google Apps Script Community
Yes, I see now that I'm not alone.

I am trying the 'get last row in spreadsheet' workaround.
Reply all
Reply to author
Forward
0 new messages