[英]Assigning HTML content to a var in Google Spreadsheet Script
我正在嘗試通過Google 電子表格中的腳本執行自動郵件,以一次向我的所有客戶發送郵件。 但是每次我嘗試運行腳本時,它都會說我錯誤地將 HTML 內容分配給了一個變量。
分配值的腳本代碼是:
function sendMail(){
var name = 1;
var email = 2;
var userEmail = 3;
var password = 4;
var emailTemp = HtmlService.createTemplateFromFile("MailApp");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Copy of OneDrive Cloud Storage");
var data = ws.getRange("B2:E" + ws.getLastRow()).getValues();
data.forEach(function(row){
emailTemp.name = row[name];
emailTemp.userEmail = row[userEmail];
emailTemp.password = row[password];
emailTemp.email = row[email];
var htmlMessage = emailTemp.evaluate().getContent();
GmailApp.sendEmail(
row[email],
"OneDrive 5TB Storage",
"Your email doesn't support HTML.",
{name: "AutoMail by Shovon", htmlBody: htmlMessage}
);
});
}
出現錯誤: var htmlMessage = emailTemp.evaluate().getContent();
錯誤消息說:
我還附上了 HTML 代碼:
<html>
<head>
<base target="_top">
</head>
<body>
<p>Dear <b> <?= name ?>, </b> </p>
<p>I received your <b>OneDrive 5TB Cloud Storage</b> request yesterday. I'm really very sorry for being so late as I had some personal things to do.</p>
<p>But the good news is I've successfully created the following <b>OneDrive Business Account</b> for you: </p>
<p> User email:<b> <u><? = userEmail?> </u> </b> <br>
Password: <b> <?= password?> </b> </p>
<p> Please login to this <b> Microsoft</b> account as soon as possible and set up 2 steps verification method by using your own email address(e.g. <b> <u> <?= email ?> </u> </b>).</p>
<p>To login <b> <u> <a href = "https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1590813490&rver=7.0.6738.0&wp=MBI_SSL&wreply=https:%2F%2Faccount.microsoft.com%2Fauth%2Fcomplete-signin%3Fru%3Dhttps%253A%252F%252Faccount.microsoft.com%252F%253Frefp%253Dsignedout-index%2526refd%253Dwww.bing.com&lc=1033&id=292666&lw=1&fl=easi2">click here</a></u></b>.</p>
<p>Yours,<br>
<b>Md. Shahariar Shovon</b></p>
</body>
</html>
請幫我解決這個問題。
你有一個錯字。
<? = userEmail?>
應該
<?= userEmail?>
所以應用程序腳本嘗試評估 javascript 代碼“= userMail”而不是簡單的“userMail”,這顯然是無效的
您不需要 HtmlService,因為“HTML 服務讓您可以為 web 頁面提供服務,這些頁面可以與服務器端應用程序腳本功能交互。” 這就是它的用例。
因此,只需使用 JavaScript 創建 HTML,然后將其放入主體中。 例如,在你的for
循環中描繪這個。
const infoObj = {
name: row[name],
email: row[userEmail]
}
const emailBody = createEmailBody( infoObj );
const params = {
name: "AutoMail by Shovon",
htmlBody: emailBody.html
}
GmailApp.sendEmail(infoObj.email, subject, emailBody.plain, params);
// here you can create your html body
function createEmailBody( info ) {
return {
plain: `Hi ${info.name}, this is your ${info.email}`,
html: `<div>Hi ${info.name}, this is your email ${info.email} </div>`
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.