2015-07-13 64 views
0

我在服務器上使用Nodejs並使用nodemailer npm模塊將HTML電子郵件內容發送給用戶。我需要使用模板來發送電子郵件。我不想在服務器端構建HTML字符串作爲字符串處理。我想使用一些模板引擎,這樣我就可以在單獨的文件中分離我的HTML標記,然後綁定我的數據以創建可以作爲電子郵件發送的HTML字符串。HTML模板和綁定數據在Nodejs中以電子郵件形式發送

以下是我的電子郵件模板

<div> 
    <img src="https://localhost:8443/img/application/forgotpassword/forgot-password-bg.jpg" alt="Forgot your myFitCode App password?"/> 
    <div> 
     Dear Mr. $name, 
    </div> 
    <div> 
     Greetings from myFitCode App. 
    </div> 
    <div> 
     We always endeavor to make health and fitness simpler and easier for our customers. 
    </div> 
    <div> 
     Your temporary password is created as below. 
    </div> 
    <div> 
     $temppassword 
    </div> 
    <div> 
     Please note; this is temporary password to activate your account. You can not use this password to login into App. You must have to click following link and create new password to continue to access App. 
    </div> 
    <div> 
     <a href="https://localhost:8443/img/application/forgotpassword/forgot-password-bg.jpg">Create New Password</a> 
    </div> 
    <div> 
     Sincerely, 
    </div> 
    <div>The myFitCode Team</div>  
</div> 

以下是我的方法,該方法使用nodemailer發送電子郵件 //處理SER忘記證書請求

exports.sendEmail = function (to, subject, content) { 

    Email.transporter.sendMail({ 
     from: '[email protected]', 
     to: to, 
     subject: subject, 
     html: content 
    }); 
}; 

如何建立HTML內容通過閱讀模板和綁定數據? 請指教。

回答

2

可能使用類似Jade或下劃線來做到這一點,如果你需要使用這個功能,這可能是正確的選擇。如果只有幾個變量,爲什麼不使用

var personalisedHtml = templateHtml.replace("$name", customerName).replace("$temppassword", temppassword) 
+0

謝謝,我用「fs」模塊讀取模板文件爲字符串,然後對鍵進行簡單的替換操作。 – joy

相關問題