I’m trying to make my script send an email. The email has mail to and cc but the email wont send if the cc is missing. Not all my rows have a cc email. I want to send the email both with and without cc. Please help, I tried everything nothing works.
function myFunction() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tasks").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var now = new Date();
for (var i = 3; i <= lr; i++) {
var checkboxRange = ss.getRange(i, 9).getValue();
var EMAIL_SENT = 'EMAIL_SENT';
var EMAIL_SENTCC = 'EMAIL_SENTCC';
var EmailAddress = ss.getRange(i, 12).getValue();
const email = EmailAddress;
var EmailAddressCC = ss.getRange(i, 13).getValue();
const emailcc = EmailAddressCC;
var currenttask = ss.getRange(i, 3).getValue();
var EmailSent = ss.getRange(i, 7).getValue();
var EmailSentCC = ss.getRange(i, 8).getValue();
var daysleft = ss.getRange(i, 6).getValue();
//var duedate = ss.getRange(i,5).getValue().getDate().getMonth();
var date = new Date(ss.getRange(i, 5).getValue());
var formattedDate = Utilities.formatDate(date, "GMT+4", 'E, MMM dd yyyy');
var templatetext = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
var emailtext = templatetext.replace()
var subjectline = "Task Reminder: " + currenttask;
var messageBody = templatetext.replace("{name}", EmailSent).replace("{title}", currenttask).replace("{task}", currenttask).replace("{date}", formattedDate).replace("{#}", daysleft)
Logger.log(messageBody);
var templatetext = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
var emailtext = templatetext.replace()
var message = {
to: EmailAddress,
subject: subjectline,
body: messageBody,
cc: EmailAddressCC,
//replyTo:currentemail,
name: "mfz scriptssCHANGE"
}
if (ss.getRange(i, 9).getValue() !== true) {
if (EmailAddress && EmailSent !== EMAIL_SENT) {
if (EmailAddressCC && EmailSentCC !== EMAIL_SENTCC) {
// let (ss.getRange(i,13).getValue()!==Error)
// if the check box is (true) it will not send an email to that task
// MailApp.sendEmail(EmailAddress,subjectline,messageBody);
try {
// MailApp.sendEmail(EmailAddress, EmailAddressCC, subjectline, messageBody,);
MailApp.sendEmail(message);
} catch (error) {
console.log(error.message, error.stack);
continue;
}
}
}
}
}
}
2
Answers
Try this:
Try this: