Search code examples
google-apps-scriptgmail-api

Cannot find method sendEmail(object)


While trying to write a script on Google Sheets, I'm getting an error:

Cannot find method sendEmail(object)

for the following code. As you can understand, it's an introduction email to a new client, presenting to the client his account rep.

  var clientname = spreadsheet.getRange('C2').getValue();
  var clientemail = spreadsheet.getRange('P2').getValue();
  var repname = spreadsheet.getRange('H2').getValue();
  var repmobile = spreadsheet.getRange('AA2').getValue();
  var repemail = spreadsheet.getRange('AB2').getValue();

     GmailApp.sendEmail({
        to: clientemail,
        subject: EMAIL_SUBJECT,
        htmlBody: createEmailBody(clientname, repname, repmobile, repemail),
        name: 'myname', 
        from: '[email protected]', 
        replyTo: repemail
      });

Thanks to the great help from you guys, I have now changed the code to:

GmailApp.sendEmail({
    recipient: clientemail, 
    subject: email_subject, 
    htmlBody: createEmailBody(clientname.toString(), repname.toString(), repmobile.toString(), repemail.toString()),
    {name: 'myname',
    from: '[email protected]', 
    replyTo: repemail}
  });

Now the error is:

Invalid property ID

on the line which starts with "{name"


Solution

  • Solution

    You are not using the sendEmail method correctly as you are passing an object as a single parameter instead of the right parameters. According to the documentation linked above, your parameters should be the following:

    • recipient (String)
    • Subject (String)
    • Body (String)
    • Options (Object)

    Therefore, your method would eventually be like this:

    GmailApp.sendEmail(clientemail, email_subject,createEmailBody(clientname.toString(), repname.toString(), repmobile.toString(), repemail.toString()),{
        name: 'myname',
        from: '[email protected]', 
        replyTo: repemail}
      );

    I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)