使用Gmail草稿作为HTML模板

时间:2019-04-04 14:27:48

标签: html google-apps-script gmail

我正在尝试使用Gmail草稿中的HTML正文,然后使用HTML模板类替换某些文本。

我可以从草稿中获取HTML正文,然后从该草稿中创建并发送新电子邮件

function myFunction() {
  //Get the Draft as html
  var draft = GmailApp.search('subject:Test Email label:draft', 0, 1);
  var html = draft[0].getMessages()[0].getBody()

  //Set the template file as a variable
  var body = HtmlService.createTemplate(html);

  body.first = 'Tester';

  //evaluate the html, make substitutions, and get the html body for the email
  var email_html = body.getCode();
  Logger.log(email_html)

  //Send the email(s)
  MailApp.sendEmail({
  to: 'test@gmail.com',
  subject: 'Sample Email from Draft',
  htmlBody: email_html,
  });
}

问题: 在日志中,要替换的HTML中的变量显示为 <?= myVar?&gt

当我使用validate()方法时,替换不会发生。

有人知道如何从Gmail草稿中读取HTML正文并将其用作模板吗?

1 个答案:

答案 0 :(得分:0)

我能够通过使用.replace()方法而不是.evaluate()。getContent()解决问题。

我试图替换从电子邮件草稿中获取的HTML中的转义字符,但是当我运行评估方法时,它抛出了有关丢失的错误;在代码中。我用头撞墙碰了一下,然后决定采取另一种方法。

以下代码实现了目标,并将标题行值用作电子邮件草稿中的合并字段标识符。

我敢肯定,有一种更快的方法来设置正则表达式并完成替换,但这是可行的,并且运行时似乎也不成问题。

如果有人有更干净的方法删除代码中的重复元素,请告诉我!

这里是一个链接,用于复制带有嵌入式脚本的Google表格。 Email from Draft Google Sheet

目标是使最终用户能够从工作表合并到电子邮件中,而无需使用被要求购买前限制为50封电子邮件的附加组件。这仅受Gmail配额限制。

function EmailFromDraft() {
  var sheet = SpreadsheetApp.getActiveSheet()
  var temp = []

  //get the header row values
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();

  //get the values for all other rows in the sheet
  data = sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()).getValues();

  //Set the replacement variables from the header row, wrap them in braces to match the draft email and put into array
  for (var i in headers) {
    var row = headers[i];
    for (var i in row) {
      var item = '{' + row[i] + '}';
      temp.push(item);
    }
  }

  //Get the Draft as html

  //Prompt user for Draft Subject
  var ui = SpreadsheetApp.getUi();
  var response = ui.prompt('Template Choice', 'Please enter the Subject of your Email Template. (Must have the Drafts Label.).', ui.ButtonSet.OK_CANCEL);
  var subject = response.getResponseText()

  //var subject = 'Drug Test Email'
  var search = 'subject:' + subject + ' label:draft'
  Logger.log(search)
  var draft = GmailApp.search(search, 0, 1);
  var html = draft[0].getMessages()[0].getBody();

  //Reg Expression Samples//
  //var re = /{first}/gi;
  //var str = '{first} are round, and {first} are juicy.';
  //var newstr = str.replace(re, 'oranges');
  //Logger.log(newstr);

  //iterate through the rows in the sheet, make replacements, send the email
  for (var i in data){
    row = data[i];
    //Format each header item as a regular expression
    var item0 = new RegExp(temp[0], 'g');
    var item1 = new RegExp(temp[1], 'g');
    var item2 = new RegExp(temp[2], 'g');
    var item3 = new RegExp(temp[3], 'g');
    var item4 = new RegExp(temp[4], 'g');
    var item5 = new RegExp(temp[5], 'g');
    var item6 = new RegExp(temp[6], 'g');
    var item7 = new RegExp(temp[7], 'g');
    var item8 = new RegExp(temp[8], 'g');
    var item9 = new RegExp(temp[9], 'g');
    var item10 = new RegExp(temp[10], 'g');
    var item11 = new RegExp(temp[11], 'g');
    var item12 = new RegExp(temp[12], 'g');

    //Set the email address variable. This is the only inflexible column
    var email = row[0];

    //Make the replacements
    var emailHtml = html.replace(item0, row[0]).replace(item1, row[1]).replace(item2, row[2]).replace(item3, row[3]).replace(item4, row[4]).replace(item5, row[5]).replace(item6, row[6]).replace(item7, row[7]).replace(item8, row[8]).replace(item9, row[9]).replace(item10, row[10]).replace(item11, row[11]).replace(item12, row[12]);
    //var html_final = html.replace(item3, row[3]);

    //Send the email(s)
    MailApp.sendEmail({
      to: email,
      subject: subject,
      htmlBody: emailHtml,
    });
   }
}