简体   繁体   English

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

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

I've got an simple automated email messaging api up and running. 我有一个简单的自动电子邮件消息api启动并运行。 However, I want the "body" of the message to be something more complicated than just unformatted text. 但是,我希望消息的“正文”比没有格式化的文本更复杂。 My code uses data from google sheets and is as follows: 我的代码使用来自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
  }))
}

With the above code, I tried emailing myself and do not get an email. 使用上面的代码,我尝试通过电子邮件发送自己,但没有收到电子邮件。 If i delete the "var tempMsg..." and and replace the final property of MailApp.SendEmail (tempMsg.getBody()) to any string - it does work. 如果我删除“var tempMsg ...”并将MailApp.SendEmail(tempMsg.getBody())的最终属性替换为任何字符串 - 它确实有效。

The idea is to use the body of a draft email in my Gmail drafts folder as the body of the new automated email. 我们的想法是在我的Gmail草稿文件夹中使用草稿电子邮件的正文作为新自动电子邮件的正文。

I went through the api and tried different methods like GmailApp.getDraft(id). 我浏览了api并尝试了不同的方法,如GmailApp.getDraft(id)。

Thank you for your help. 谢谢您的帮助。

I've made some progress. 我取得了一些进展。 I've managed to learn how to add html/css styling to the body of my email and it works. 我已经设法学习如何在我的电子邮件正文中添加html / css样式并且它有效。 This doesn't answer the question exactly but the result is what i wanted. 这不能完全回答问题,但结果是我想要的。

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.

相关问题 如何使用电子表格中的 Google App 脚本生成 Gmail 草案包含正文中的表格 - How to Generate Gmail draft using Google App Script from Spreadsheet contains Table in the Body 如何从 Google AppScript 通过 AWS SES 发送 email - How to send email via AWS SES from Google AppScript 如何使用 GMAIL API 从 Google Apps 脚本发送电子邮件? - How to send the email from Google Apps Script using GMAIL API? 如何使用谷歌应用程序脚本发送草稿电子邮件 - How to send a draft email using google apps script 格式化从Google电子表格发送的自动电子邮件 - Formatting the automated email send from google spreadsheet 从Google电子表格发送自动电子邮件? - Send an automated email from google spreadsheet? 如何使用Google Appscript删除(日历或Gmail)任务? - How to delete a (calendar or gmail) task using google appscript? 如何使用Google AppScript登录Gmail进行HTTP请求 - How to login into Gmail to make an HTTP request using Google AppScript 如何获取 Google 表单字段值并在 Appscript 中使用它来发送电子邮件 - How to get Google Form field value and use it in Appscript to send an email MailApp AppScript:如何使用 MailApp 将 email 发送到线程? - MailApp AppScript: How to send an email to a thread using MailApp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM