Reputation: 329
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
Reputation: 2920
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:
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
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:
sendEmail
object
as a single argumentSince 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
References
Upvotes: 0
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