Vivian Roberts
Vivian Roberts

Reputation: 117

Send email to multiple recepients in apps script

I'm trying to send an email to multiple email addresses that can be found in a single cell separated by commas. I have done this before with gmail.app and it worked perfectly but I am trying to do it now using MailApp and I get an error saying the email is invalid.

Here is the code: 
function mail() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
  var responses = ss.getSheetByName("referencias");
  var mail = ss.getSheetByName("MAILS");
  var mailok = mail.getRange(1,1).getValues();
  var active_range = responses.getActiveRange();
  var cambio = responses.getRange(active_range.getRowIndex(), 5).getValue();
  var nuevo = responses.getRange(3, 11).getValue();
  var cancelados = responses.getRange(3, 12).getValue();
  var fecha =responses.getRange(3, 8).getValue();
  var date = Utilities.formatDate(fecha, "GMT+2", "dd/MM/YYYY")
  var message = {
    to: "email here",
    subject: "CAMBIOS REFERENCIAS Y DROPS: Resumen refes canceladas/añadidas",
    body:"Los siguientes modelos fueron modificados en el Master Doc ayer fecha " +date +".\n\n" + "Refes añadidas:" + nuevo + "\n\nRefes canceladas:"+ cancelados+ "\n\nCualquier consulta podéis contestar a este mail."+"\n\n Además, encontraréis adjunto un PDF con una tabla resumen de los cambios de drops."+"\n\nArchivo: https://docs.google.com/spreadsheets/d//edit#gid=1098522138",
    attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Tabla")]
  }
  MailApp.sendEmail(message);
}

How can I send this email to many recipients at the same time? This email will be sent automatically everyday and ideally I would like it to be sent in a thread, however I have to fix this first before I try to do that. If there is any missing information or confusion just let me know!

Upvotes: 0

Views: 122

Answers (1)

Iamblichus
Iamblichus

Reputation: 19309

Issue:

  • Range.getValues() returns a 2D array, even if the range is a single cell.
  • If you want to return the value of a single cell, consider using Range.getValue() instead.

Solutions:

As suggested in comments, either change this line:

to: "email here",

To this one:

to: mailok[0][0],

Or, alternatively, these ones:

var mailok = mail.getRange(1,1).getValues();
// ...
to: "email here",

To these ones:

var mailok = mail.getRange(1,1).getValue();
// ...
to: mailok,

Upvotes: 1

Related Questions