user13708028
user13708028

Reputation: 329

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"

Upvotes: 0

Views: 211

Answers (3)

Mateo Randwolf
Mateo Randwolf

Reputation: 2920

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. :)

Upvotes: 0

0Valt
0Valt

Reputation: 10345

This is a supplementary answer:

you need to learn to understand the error messages - they are your best friends when debugging, but they are not always intuitive. In your case the error states: Cannot find method sendEmail(object). Try to parse it as you would a template string, the message says:

  1. Cannot find method
  2. Method searched is sendEmail
  3. It is passed an instance of object as a single argument

Since there is obviously a method called sendEmail (after making sure there are no typos), you can safely eliminate first two parts. That leaves us with the "instance of object" - a natural question then would be "is it allowed to pass an object to this method?". If you look at the docs, it is not.

But you could avoid all of this in the first place. There is an blog post I find very useful - 6 deadly sins of GAS Add-on development. The first one strikes an apple - you will be much better off not using the script editor and install an IDE or editor that supports autocomplete (be it VS Code, Netbeans or others).

That being said, repeating the other answers, the method signature is structured this way:

 sendEmail( recipient, subject,  body,   options )
________________________________________________
  name        arg0       arg1    arg2    [arg3]

Note that the first three arguments are mandatory and only the fourth one is optional. If you look at reference docs again, options is where all of the advanced parameters reside.

Notes

  1. On curly brackets: I assume you know this, but just in case: this is an object literal (initializer) notation that is used to instantiate an object. You don't have to wrap all of parameters in a single object if a method signature says otherwise (I think you are on the right track, though - it is good to declare your APIs with a single config parameter, just don't violate the public contract).

References

  1. sendEmail with options spec
  2. sendEmail without options spec
  3. MailApp method sendEmail spec

Upvotes: 0

user13338210
user13338210

Reputation:

Modify this code for your requirements.

Code is copied from Official documentation. The correct way to do this is:

function myFunction() {
// The code below will send an email with the current date and time.
var now = new Date();
GmailApp.sendEmail("[email protected]", "current time", "The time is: " + now.toString()); 
}

Edit: HTML body sample as requested:

function myFunction() {
// The code below will send an email with the current date and time.
var now = new Date();
GmailApp.sendEmail("[email protected]", "current time", "The time is: " + now.toString(),{htmlBody: '<h1>hello</h1>'});
  }

Upvotes: 1

Related Questions