[英]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.