From 67fca4a6533dd4dfc13fbc5e25d8eb22d76186f3 Mon Sep 17 00:00:00 2001 From: Nachiket G Kallapur <48771791+nachiketkallapur@users.noreply.github.com> Date: Fri, 2 Oct 2020 14:37:29 +0530 Subject: [PATCH] Update README.md --- README.md | 69 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 84715a5..ed2d744 100644 --- a/README.md +++ b/README.md @@ -7,23 +7,58 @@ engine to generate html emails. npm install nodemailer-express-handlebars ``` # Usage -```javascript -//reference the plugin -var hbs = require('nodemailer-express-handlebars'); -//attach the plugin to the nodemailer transporter -transporter.use('compile', hbs(options)); -//send mail with options -var mail = { - from: 'from@domain.com', - to: 'to@domain.com', - subject: 'Test', - template: 'email', - context: { - name: 'Name' - } -} -transporter.sendMail(mail); -``` + + + const nodemailer = require("nodemailer"); + + + const hbs = require("nodemailer-express-handlebars"); + + + const path = require("path"); + + + var transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: "your_email", + pass: "your_password", + }, + }); + + const handlebarOptions = { + viewEngine: { + extName: ".handlebars", + partialsDir: path.resolve(__dirname, "templateViews"), + defaultLayout: false, + }, + viewPath: path.resolve(__dirname, "templateViews"), + extName: ".handlebars", + }; + + transporter.use( + "compile", + hbs(handlebarOptions), + ); + + var mailOptions = { + from: "your_email", + to: "to_email", + subject: "your_subject", + template: "main", + context: { + var: var_value + }, + }; + + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + console.log(error); + } else { + console.log("Email sent: " + info.response); + } + }); + ## Plugin Options The plugin expects the following options: * __viewEngine (required)__ either the express-handlebars view engine instance or [options for the view engine](https://github.com/ericf/express-handlebars#configuration-and-defaults)