![](/img/trans.png)
[英]How to send html email line by line using google apps script and google sheet data
[英]Google Apps Script - How to send HTML email from Sheet?
我正在嘗試編寫代碼以從工作表發送 html 電子郵件。 我在 GAS html 文件中有一個模板。 Gmail API 已啟用。
我確實設法以這種方式發送了一封電子郵件,並替換了占位符。 但是,我只是發送 html 文件中的所有文本、標簽和所有內容。
function htmlTry() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sample");
var lastRow = spreadsheet.getLastRow();
var name = spreadsheet.getRange(lastRow, 3).getValue();
var problem = spreadsheet.getRange(lastRow, 6).getValue();
if(problem == "Yes") {
var htmlBody = HtmlService.createHtmlOutputFromFile('htmlFile').getContent()
.replace("#name", name)
.replace("?name", name)
.replace("/name", name);
MailApp.sendEmail ("sample@email.com", "Problem: " + name, htmlBody);
}
}
我還提供了 html 文件的示例。 這正是電子郵件所說的,只是它丟失了縮進。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Problem</title>
<style>
body,table,thead,tbody,tr,td,img {
padding: 0;
background-color: #23A5F3;
}
.wrapper {
padding-left: 10px;
padding-right: 10px;
}
h1,h2,h3,h4,h5,h6,p {
font-family: Trebuchet MS, Arial, sans-serif;
color:#9CD6FA;
}
p,a,li {
font-family: Trebuchet MS, Arial, sans-serif;
color:#000000;
}
</style>
</head>
<body style="background-color:#FFFFFF;">
<table width="100%">
<tbody>
<tr>
<td class="wrapper" width="600" align="center">
<table class="section header" cellpadding="0" cellspacing="0" width="600">
<tr>
<td class="column">
<table>
<tbody>
<tr>
<td align="left" style="background-color: #FFFFFF;">
<p style="text-align:justify;">Lorem ipsum</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
感謝Irvin Jay G.解決了這個問題,他花時間解釋了為什么我應該完全按照他們的建議去做,而不是自己思考。 希望你有美好的幾天。
解決方案:
您需要使用名為
htmlBody
的高級參數,按照 MailApp 的sendEmail(recipient, subject, body, options)方法,在 html 視圖中顯示文件,而不是在電子郵件消息中顯示純 html 代碼。
我能夠復制您的腳本並在我的測試電子郵件帳戶中獲得此結果:
這是使用htmlBody
高級參數調整后的腳本:
function htmlTry() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sample");
var lastRow = spreadsheet.getLastRow();
var name = spreadsheet.getRange(lastRow, 3).getValue();
var problem = spreadsheet.getRange(lastRow, 6).getValue();
if(problem == "Yes") {
var htmlBody = HtmlService.createHtmlOutputFromFile('htmlFile').getContent()
.replace("#name", name)
.replace("?name", name)
.replace("/name", name);
MailApp.sendEmail("sample@email.com", 'Problem: '+ name, htmlBody, {
htmlBody: htmlBody
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.