joy
joy

Reputation: 3707

HTML Template and bind data to send as email in Nodejs

I am using Nodejs at server and using nodemailer npm module to send the HTML email content to user. I need to use template to send email. I don't want to build HTML string in server side as string processing. I want to use some template engine so that I can separate my HTML markup at separate file and then bind my data to create HTML string which I can send as email.

Following is my email template

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

Following is my method which use nodemailer to send email //Process ser forgot credential request

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

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

How I can build HTML content by reading the template and binding the data? Please advice.

Upvotes: 0

Views: 1519

Answers (1)

veggiesaurus
veggiesaurus

Reputation: 651

You could use something like Jade or underscore to do this, and that's probably the right way to go if you need to use this functionality a lot. If there are only a few variables, why not use

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

Upvotes: 2

Related Questions