繁体   English   中英

使用Google Appscript - 如何使用位于Gmail中的草稿模板中的自定义正文发送自动电子邮件?

[英]With Google Appscript - How to send a automated email using a custom body from a draft template located in Gmail?

我有一个简单的自动电子邮件消息api启动并运行。 但是,我希望消息的“正文”比没有格式化的文本更复杂。 我的代码使用来自Google工作表的数据,如下所示:

function doGet(e)
{
  var ss = SpreadsheetApp.openById('SpreadsheetID');
  var sheet = ss.getSheetByName("msg");
  var rows = sheet.getDataRange().getValues();
  Logger.log(e.parameter.name);
  var headings = rows[0].map(String.toLowerCase);
  Logger.log(headings);
  var holder = [];
  for(x in headings)
  {
    Logger.log(headings[x]);
    var output = (headings[x] in e.parameters) ? e.parameter[headings[x]] : 'none';
    if (headings[x] == 'id'){ output = getRandom();}
    if (headings[x] == 'timestamp'){ output = new Date();}
    holder.push(output);
  }
  Logger.log("The holder contains the following: " + holder);
  Logger.log(holder.length);
  sheet.appendRow(holder);

  var lRow = sheet.getLastRow();

  Logger.log('HEADING IS: ' + headings[1] + ' ' + lRow + ' ' + sheet.getRange(lRow, 2).getValue());

  var tempMsg = GmailApp.getDraft()[0].getMessage();

  MailApp.sendEmail(sheet.getRange(lRow, 2).getValue(),'mo@example.co.uk', 'Thank you for signing up!!', tempMsg.getBody());

  return ContentService.createTextOutput(JSON.stringify({
    'status' : 'success',
    'lastRow' : lRow ,
    'INFO' : holder
  }))
}

使用上面的代码,我尝试通过电子邮件发送自己,但没有收到电子邮件。 如果我删除“var tempMsg ...”并将MailApp.SendEmail(tempMsg.getBody())的最终属性替换为任何字符串 - 它确实有效。

我们的想法是在我的Gmail草稿文件夹中使用草稿电子邮件的正文作为新自动电子邮件的正文。

我浏览了api并尝试了不同的方法,如GmailApp.getDraft(id)。

谢谢您的帮助。

我取得了一些进展。 我已经设法学习如何在我的电子邮件正文中添加html / css样式并且它有效。 这不能完全回答问题,但结果是我想要的。

var htmlmsg = "<h1 style='text-align: center;'>This is the heading</h1><br/><p>Body Text</p>"; MailApp.sendEmail(sheet.getRange(lRow, 2).getValue(), 'Thank you for signing up!!', htmlmsg, {htmlBody:htmlmsg});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM