Madhurjya
Madhurjya

Reputation: 507

Weird behaviour of Mailapp.sendEmail in Google apps script

I have been sending emails successfully through Google apps script triggered through submission in a Google sheet. Lately (about a week back), the email sender starts behaving very weird.

If I use the following format, my emails get bounced

MailApp.sendEmail(email1, subject, message,{cc:email2,attachments:[file.next()]});

If I use the following form, the email does NOT get delivered neither does it get bounced

MailApp.sendEmail(email, subject, message);

If I use the following format, the recipient gets the message as shown

MailApp.sendEmail(email,subject,{htmlBody: message});

Revecied message

[object Object]

and the rest of the stuff blank!

I'm at my wits' end as to how to go about. Any help or a pointer will be of immense help. Regards

Madhurjya

Following is the app script, which is attached to the Google sheet. Once I run the function sendPasswd() from sheet, it gets some vital parameters from the sheet data and then send the message to the person (through the variable email2)

function sendPasswd() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var dataRange = sheet.getDataRange()
  var data = dataRange.getValues();
  var subject = "Example Subject";
  var message = "";
  var i;
  var file = DriveApp.getFilesByName("some_file.pdf");
  for (i in data) {
    var row = data[i];
    if (i == 0) continue; // Skip the first row
    if (row[4] == "Sent") continue;
    var first = row[0]; var last = row[1]; var email = row[2];
    var passwd = row[3]; var email2 = row[7];
    if (row[15] == "some condition") {
      message = "Dear <b>"+first+" "+last+"</b>,<br><br>"+
      "This is to inform you that your .... ";
      MailApp.sendEmail(email2, subject, message);
      //MailApp.sendEmail(email2,subject,{htmlBody: "message"});
      //MailApp.sendEmail(email2, subject, message,{cc:"[email protected]",attachments:[file.next()]});         
    }
  }
}

The surprising fact is that from the same account similar emails are being sent and are NOT affected!

Madhurjya

Upvotes: 0

Views: 992

Answers (2)

You could also use this way (its really more practical):

const email '[email protected]'
const cc ='[email protected]'
const bcc = '[email protected]'

MailApp.sendEmail({
              to:email,
              cc: cc,
              bcc: bcc,
              subject: 'Whatever you want, even template strings' ,
              htmlBody: `

               You could just create your HTML IN HERE (using template strings)
               Or create before and add here as variable as well.
              
              `
            })

Upvotes: 0

vector
vector

Reputation: 1022

This might be happening because you're using htmlbody which is part of options as third argument whereas in sendEmail(recipient, subject, body, options) of class MailApp, body should be third argument, that's is the reason of getting [object Object].

Try following modification:-

MailApp.sendEmail(email2,subject,"",{htmlBody: message});

Reference:

sendEmail

Upvotes: 1

Related Questions