0

I'm getting the error when I run the code below:

Failed to send email: no recipient

The code is embedded in a google spreadsheet project linked to a google form. The error rises on the MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf}); call, even if the value of the email variable is correct. Moreover, the recipient receives the email but then the code doesn't continue. Code provided below:

// When Form Gets submitted
function onFormSubmit(e) {

//Get information from form and set as variables

var cognome = e.values[1].toUpperCase();
var nome = e.values[2].toUpperCase();
....
var email = e.values[9].toString();

// Prepare a pdf document. Save and close the temporary document
copyDoc.saveAndClose();

// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

// Attach PDF and send the email
var subject = "Subject";
var body = '....';

Logger.log(email);
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});



// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true); 

}
ross
  • 2,684
  • 2
  • 13
  • 22
  • Try to have an `if` statement before sending the mail. That could be some blank rows in the spreadsheet. The `if` statement will prevent the mail from sending when it is empty. check [this SO post](https://stackoverflow.com/questions/54819561/failed-to-send-email-no-recipient) for sample implementation. – MαπμQμαπkγVπ.0 Jun 05 '19 at 08:58
  • Values come from a Google Form submission through the `e` object. Then, I gather the `email` value at the beginning of the script, and it is certanly not null. In fact the Logger prints the correct value and the mail arrives to the recipient. But then the script fails and last instruction is never reached. The strange thing is that this script was working until some days ago. –  Jun 05 '19 at 10:00

1 Answers1

0

The thing is to not have more empty cells after the last email, this generate null entries and you get the warning.

Removing empty rows may solve the problem.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 11 '21 at 22:31