Skip to content

Instantly share code, notes, and snippets.

@maisnamraju
Last active October 10, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maisnamraju/4a3efbaf6531f68c7a6dd8c7fb32bf6b to your computer and use it in GitHub Desktop.
Save maisnamraju/4a3efbaf6531f68c7a6dd8c7fb32bf6b to your computer and use it in GitHub Desktop.
Nodemailer with Mailgun to send email templates
"use strict";
let nodemailer = require('nodemailer');
let jade = require('jade');
let mailgunTransport = require('nodemailer-mailgun-transport');
let nodeTemplates = require('email-templates');
let path = require('path');
let nconf = require('nconf');
let co = require('co');
let Account = require('../models/account.js');
nconf.argv().env().file({file: __dirname + '/../config.json'});
class Mailer {
constructor() {
let config = nconf.get('mail');
this.mailer = nodemailer.createTransport(mailgunTransport(config));
this.sender = config.email;
this.site = nconf.get('url');
}
sendMail(config) {
this.mailer.sendMail(config, function(error, response) {
if (error) {
console.log(error);
} else {
console.log('Message sent');
}
});
}
sendResetPassword(token) {
let resetTemplate = path.resolve(__dirname, '..', 'mail-templates','reset-password.jade');
// not sure if this is good practice but this function call doesn't return anything as it is sending email on it's own;
Account
.findOne({ _id: token.account_id })
.then((user) => {
let msg = {
link : this.site + '/reset-password/' + token.code,
email: user.attributes.email
}
let html = jade.compileFile(resetTemplate);
html = html(msg);
let config = {
to: user.attributes.email,
from: this.sender,
subject: 'Password Reset',
html : html
};
this.sendMail(config);
});
}
};
module.exports = Mailer;
doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"
head
meta(content='text/html; charset=utf-8', http-equiv='Content-Type')
body(style='padding: 0; width: 100% !important; -webkit-text-size-adjust: 100%; margin: 0; -ms-text-size-adjust: 100%;', marginheight='0', marginwidth='0')
center
table(cellpadding='8', cellspacing='0', style='*width: 540px; padding: 0; width: 100% !important; background: #ffffff; margin: 0; background-color: #ffffff;', border='0')
tr
td(valign='top')
table(cellpadding='0', cellspacing='0', style='border-radius: 6px; -webkit-border-radius: 6px; border: 1px #c0c0c0 solid; -moz-border-radius: 6px;', border='0', align='center')
tr
td(colspan='3', height='6')
tr(style='line-height: 0px;')
td(width='100%', style='font-size: 0px;', align='center', height='1')
img(width='40px', style='max-height: 110px; width: 70px; *width: 70px; *height: 110px;', alt='', src='http://skeleton-app.jit.su/img/Octocat.png')
tr
td
table(cellpadding='0', cellspacing='0', style='line-height: 25px;', border='0', align='center')
tr
td(colspan='3', height='30')
tr
td(width='36')
td(width='454', align='left', style='color: #444444; border-collapse: collapse; font-size: 11pt; font-family: proxima_nova, \'Open Sans\', \'Lucida Grande\', \'Segoe UI\', Arial, Verdana, \'Lucida Sans Unicode\', Tahoma, \'Sans Serif\'; max-width: 454px;', valign='top')
//- -----------------------------------------------------------------------------------
//- Only Edit Here
//- -----------------------------------------------------------------------------------
p Hello #{email}!
p
| It seems you have requested for a password reset
p
| To perform the reset, please click on the link
a(href!='#{link}') Click Here
| .
td(width='36')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment