0

function ifstatement() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("Product Form");
  const ws2 = ss.getSheetByName("Email details");

  var Avals = ws.getRange("c1:c").getValues();
  var lr = Avals.filter(String).length;

  Logger.log(lr);

  var recipient = ws.getRange(lr,2).getValue();
  var sub = ws.getRange(lr,4).getValue();
  var mailTemp = ws.getRange(lr,5).getValue();
  var impfield1 = ws.getRange(lr,6).getValue();
  var impfield2 = ws.getRange(lr,8).getValue();
  var impfield3 = ws.getRange(lr,10).getValue();
  var fieldvalue1 = ws.getRange(lr,7).getValue();
  var fieldvalue2 = ws.getRange(lr,9).getValue();
  var fieldvalue3 = ws.getRange(lr,11).getValue();
  var heading = ws.getRange(lr,12).getValue();
  var subheading = ws.getRange(lr,13).getValue();
  var body = ws.getRange(lr,14).getValue();
  var footer = ws.getRange(lr,15).getValue();
  var attach = ws.getRange(lr,17).getValue();
  var tomail = ws.getRange(lr,16).getValue();
  var file1 = attach.split(",").map(url => DriveApp.getFileById(url.trim().split("=")[1]).getBlob());

  var AvalsWealth = ws2.getRange("b1:b").getValues();
  var AvalsInsurance = ws2.getRange("c1:c").getValues();
  var AvalsTeam = ws2.getRange("e1:e").getValues();
  var lrWealth = AvalsWealth.filter(String).length;
  var lrInsurance = AvalsInsurance.filter(String).length;
  var lrTeam = AvalsTeam.filter(String).length;
  
  Logger.log(mailTemp);
  Logger.log(heading);

  if(tomail=="Wealth RMs"){
    var bccmail = ws2.getRange(2,2,lrWealth).getValues().toString();
  } else if(tomail=="Self"){
    var bccmail = recipient;
  } else if(tomail=="Insurance RMs"){
    var bccmail = ws2.getRange(2,3,lrInsurance).getValues().toString();
  } else if(tomail=="All India"){
    var bccmail = ws2.getRange(2,4,lrTeam).getValues().toString();
  }

  Logger.log(bccmail)

  if(mailTemp=="HTML1"){
    const htmlTemplate = HtmlService.createTemplateFromFile("HTML1");
    htmlTemplate.heading = heading;
    htmlTemplate.subheading = subheading;
    htmlTemplate.body = body;
    htmlTemplate.footer = footer;
    htmlTemplate.impfield1 = impfield1;
    htmlTemplate.impfield2 = impfield2;
    htmlTemplate.impfield3 = impfield3;
    htmlTemplate.fieldvalue1 = fieldvalue1;
    htmlTemplate.fieldvalue2 = fieldvalue2;
    htmlTemplate.fieldvalue3 = fieldvalue3;

    const htmlforemail = htmlTemplate.evaluate().getContent();

    GmailApp.sendEmail('',
    ""+ sub + "",
    '',
    {htmlBody: htmlforemail,
    bcc: bccmail,
    attachments: file1}
    ) } else if(mailTemp=="HTML2"){
          const htmlTemplate = HtmlService.createTemplateFromFile("HTML2");
          htmlTemplate.heading = heading;
          htmlTemplate.subheading = subheading;
          htmlTemplate.body = body;
          htmlTemplate.footer = footer;
          htmlTemplate.impfield1 = impfield1;
          htmlTemplate.impfield2 = impfield2;
          htmlTemplate.impfield3 = impfield3;
          htmlTemplate.fieldvalue1 = fieldvalue1;
          htmlTemplate.fieldvalue2 = fieldvalue2;
          htmlTemplate.fieldvalue3 = fieldvalue3;

          const htmlforemail = htmlTemplate.evaluate().getContent();

          GmailApp.sendEmail('',
          ""+ sub + "",
          '',
          {htmlBody: htmlforemail,
          bcc: bccmail,
          attachments: file1}
          ) } else if(mailTemp=="HTML3"){
            const htmlTemplate = HtmlService.createTemplateFromFile("AUM Annual awards");
            htmlTemplate.heading = heading;
            htmlTemplate.subheading = subheading;
            htmlTemplate.body = body;
            htmlTemplate.footer = footer;
            htmlTemplate.impfield1 = impfield1;
            htmlTemplate.impfield2 = impfield2;
            htmlTemplate.impfield3 = impfield3;
            htmlTemplate.fieldvalue1 = fieldvalue1;
            htmlTemplate.fieldvalue2 = fieldvalue2;
            htmlTemplate.fieldvalue3 = fieldvalue3;

            const htmlforemail = htmlTemplate.evaluate().getContent();

            GmailApp.sendEmail('',
            ""+ sub + "",
            '',
            {htmlBody: htmlforemail,
            bcc: bccmail}
            ) } else if(mailTemp=="HTML4"){
          const htmlTemplate = HtmlService.createTemplateFromFile("HTML4");
          htmlTemplate.heading = heading;
          htmlTemplate.subheading = subheading;
          htmlTemplate.body = body;
          htmlTemplate.footer = footer;
          htmlTemplate.impfield1 = impfield1;
          htmlTemplate.impfield2 = impfield2;
          htmlTemplate.impfield3 = impfield3;
          htmlTemplate.fieldvalue1 = fieldvalue1;
          htmlTemplate.fieldvalue2 = fieldvalue2;
          htmlTemplate.fieldvalue3 = fieldvalue3;

          const htmlforemail = htmlTemplate.evaluate().getContent();

          GmailApp.sendEmail('mayank.agarwal@aumcap.com',
          ""+ sub + "",
          '',
          {htmlBody: htmlforemail,
          bcc: bccmail,
          attachments: file1}
          ) }
}

我有一个小型设置,其中有一个 Google 表格、Google Appscript 和 Google 表单。用户需要在 Google 表单中输入数据——一旦工作表(与表单响应链接)接收到更新的行——就会触发一个脚本,该脚本从行中获取数据并将其输入工作表并将邮件发送到选定的集合的用户。

现在我面临的问题是我需要将此邮件发送给大约 100-150 人,但是 Googel appscript 不允许我一次向 30 多个参与者发送邮件。

我知道我需要一个电子邮件服务,但是,我找不到任何好的解决方案,我可以在其中存储我的模板,而用户只需填写数据字段并发送邮件。也许是因为我对这个技术领域很陌生,所以这就是原因。

谁能指导我为我的目的需要使用什么设置?谢谢!

4

1 回答 1

0

您需要做的第一件事是将密件抄送列表保留为数组,直到有必要为止。这将允许我们从中制作块。这意味着.toString()在分配结束时删除bccmail

然后,我们需要一个自定义函数来批量发送电子邮件。我正在调用该函数sendMassEmail

function sendMassEmail(bccList, subject, plainBody, htmlBody, attachments) {
  for (let bcc of chunks(bccList, 30)) {
    GmailApp.sendEmail(
      '',
      subject,
      plainBody,
      {
        htmlBody,
        bcc: bcc.join(','),
        attachments,
      },
    )
  }
}

Chunks 是一个功能,它以块的形式获取数组的元素。对于这种情况,我最喜欢的实现是使用生成器

我保留了纯文本正文,因为如果您发送它,生成它很重要,因为它显示在收件箱上的大多数客户端中。此外,他们中的一些人只能阅读这些(想想智能手表)。

最后一个参数 ( attachments) 是可选的。

这是有关如何使用它的示例:

sendMassEmail(
  bccmail,
  sub.toString(),
  '', // Should probably change it
  htmlforemail,
  file1,
)

请注意,使用它toString()比连接成空字符串更好。

参考

于 2021-08-10T11:15:59.917 回答