簡體   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