如何在我的Google脚本中添加电子邮件抄送

时间:2019-07-10 15:25:09

标签: javascript email google-apps-script

我有一个已经继承的Google脚本,我希望对其进行更新以反映当前的要求。

当前,它使用Google表格中的数据向参与者和赞助者发送2封电子邮件。我尝试将其合并,以便“现在为每个文档发送电子邮件”部分向参与者和cc的赞助者发送电子邮件。

我已经从以下地方更新了代码:

var sendTo = row[EMAIL]; 

收件人:

var sendTo = row[EMAIL] + " , " + sponsors; 

并删除第'2节。无法解决cc查询的“个人案例”。

我尝试修改现有代码以添加https://developers.google.com/apps-script/reference/mail/mail-app中所述的其他参数,但似乎无法使其在下面的代码中正常工作。

// Now email for each doc
  var email = getAppraiseeEmail();
  if(email && email.body) {
    email.body = email.body.replaceAll('%doctitle1%', bTitle)
     /*.replaceAll('%doctitle2%', pTitle)
     .replaceAll('%doctitle3%', dTitle)
     .replaceAll('%doctitle4%', dpTitle)
     .replaceAll('%link1%', bFile.getUrl())
     .replaceAll('%link2%', pFile.getUrl())
     .replaceAll('%link3%', dFile.getUrl())
     .replaceAll('%link4%', dpFile.getUrl())*/
     .replaceAll('%link5%', child.getUrl())
     .replaceAll('%name%', row[NAME]);

    var sendTo = row[EMAIL];
    if(sendTo && sendTo!=='') {
      email.recipient = sendTo;
      sendAnEmail(email);
    }
  } else {
    throw Error('missing email configuration (Business Case)');
  }


// 2. Personal Case
  if(sponsors.length > 0) {
    var emailRev = getSponsorEmail();
    if(emailRev && emailRev.body) {
      emailRev.body = emailRev.body.replaceAll('%doctitle1%', bTitle)
      /*.replaceAll('%doctitle2%', pTitle)
      .replaceAll('%doctitle3%', dTitle)
      .replaceAll('%doctitle4%', dpTitle)
      .replaceAll('%link1%', bFile.getUrl())
      .replaceAll('%link2%', pFile.getUrl())
      .replaceAll('%link3%', dFile.getUrl())
      .replaceAll('%link4%', dpFile.getUrl()) */
      .replaceAll('%link5%', child.getUrl())
      .replaceAll('%name%', row[NAME]);

      var sendTo = sponsors.join(',');
      if(sendTo && sendTo!=='') {
        emailRev.recipient = sendTo;
        sendAnEmail(emailRev);
      }
    } else {
      throw Error('missing email configuration (sponsor)');
    }
  } // end if re sponsors

  row[NOTES] = 'Files created & shared, ' + timestamp;
} catch(e) {
  console.error(e);
  row[NOTES] = 'An error occurred (' + e + '), ' + timestamp;
}

function getAppraiseeEmail() {
var email = {};
email.body = getConfig('email01');
email.subject = getConfig('subject01');
return email;
}

function getSponsorEmail() {
var email = {};
email.body = getConfig('email02');
email.subject = getConfig('subject02');
return email;
}

function sendAnEmail(email) {
var options = { noReply: true, htmlBody: email.body };
MailApp.sendEmail(email.recipient, email.subject, null , options);
}

理想情况下,应从“现在为每个文档发送电子邮件”部分中删除电子邮件,并抄送“ 2”部分中的赞助者。个人案例”。

谢谢!

UPDATE 15/07/19

我已将代码更新为以下代码,现在可以使用:

// Now email for each doc
   var email = getAppraiseeEmail();
  if(email && email.body) {
    email.body = email.body.replaceAll('%doctitle1%', bTitle)
                           .replaceAll('%link5%', child.getUrl())
                           .replaceAll('%name%', row[NAME]);

    var CC = row[SPONSORS]
    var sendTo = row[EMAIL]
    if(sendTo && sendTo!=='') {
      email.recipientTO = sendTo;
      email.CC = CC;
      sendAnEmail(email); 
    }
  } else {
    throw Error('missing email configuration (Business Case)');
  }

 row[NOTES] = 'Files created & shared, ' + timestamp;
} catch(e) {
  console.error(e);
  row[NOTES] = 'An error occurred (' + e + '), ' + timestamp;
}

function getAppraiseeEmail() {
var email = {};
email.body = getConfig('email01');
email.subject = getConfig('subject01');
return email;
}

function sendAnEmail(email) {
var options = { noReply: true, htmlBody: email.body, cc: email.CC };
MailApp.sendEmail(email.recipientTO, email.subject, null , options);
}

0 个答案:

没有答案