提问人:Firas 提问时间:11/10/2023 最后编辑:Rory McCrossanFiras 更新时间:11/11/2023 访问量:61
仅当设置了抄送时才发送电子邮件
Email only sends if a cc is set
问:
我正在尝试让我的脚本发送电子邮件。电子邮件有邮件到和抄送,但如果抄送丢失,电子邮件将不会发送。并非我的所有行都有抄送电子邮件。我想发送带抄送和不带抄送的电子邮件。请帮忙,我尝试了一切,没有任何效果。
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;
}
}
}
}
}
}
答:
0赞
TheWizEd
11/10/2023
#1
试试这个:
var message = {
to: EmailAddress,
subject: subjectline,
body: messageBody,
//replyTo:currentemail,
name: "mfz scriptssCHANGE"
}
if( EmailAddressCC !== "" ) message.cc = EmailAddressCC;
0赞
Cooper
11/11/2023
#2
试试这个:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Tasks");
const vs = sh.getRange(3, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
const dt = new Date();
const templatetext = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
vs.forEach((r, i) => {
var eMAIL_SENT = 'EMAIL_SENT';
var emailAddress = r[11];
const email = emailAddress;
var emailAddressCC = r[12];
var currenttask = r[2];
var emailSent = r[6];
var emailSentCC = r[7];
var daysleft = r[5];
var date = new Date(r[4]);
var formattedDate = Utilities.formatDate(date, "GMT+4", 'E, MMM dd yyyy');
var subjectline = "Task Reminder: " + currenttask;
var messageBody = templatetext.replace("{name}", emailSent).replace("{title}", currenttask).replace("{task}", currenttask).replace("{date}", formattedDate).replace("{#}", daysleft)
var templatetext = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
var message = { to: emailAddress, subject: subjectline, body: messageBody, cc: emailAddressCC, name: "mfz scriptssCHANGE" }
if (r[8] !== true) {
if (emailAddress && emailSent !== eMAIL_SENT) {
try {
MailApp.sendEmail(message);
} catch (error) {
console.log(error.message, error.stack);
continue;
}
}
}
});
}
评论