-
-
Save davisford/475ec17bf99bfe3d3bf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* MailWrapper - An extremely simple nodemailer wrapper library that makes it easy to send emails using Express and Jade templates. Originally | |
* developed to practice and learn how to write modules for node. | |
* | |
* Copyright(c) 2011 Tom Shaw <[email protected]> | |
* MIT Licensed | |
*/ | |
var mailer = require('./lib/mailer'); | |
/** | |
* Simple example in an express controller. | |
*/ | |
app.get('/', loadUser, function(req, res) { | |
res.render('index.jade', { | |
locals: { title: 'Welcome To Node Rocks!', currentUser: req.currentUser } | |
}); | |
//mailer.sendRegistration(req.currentUser); | |
mailer.sendThankyou(req.currentUser); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* MailWrapper - An extremely simple nodemailer wrapper library that makes it easy to send emails using Express and Jade templates. Originally | |
* developed to practice and learn how to write modules for node. | |
* | |
* Copyright(c) 2011 Tom Shaw <[email protected]> | |
* MIT Licensed | |
*/ | |
/* | |
* Node mailer default message settings. | |
*/ | |
exports.message = { | |
_from: "[email protected]" | |
, _reply_to: "[email protected]" | |
} | |
/* | |
* Node mailer SMTP connection settings. | |
*/ | |
exports.server = { | |
_ssl: true | |
, _host: "smtp.gmail.com" | |
, _port: 465 | |
, _use_authentication: "login" | |
, _username: "[email protected]" | |
, _password: "yourpass" | |
, _debug: false | |
} | |
/* | |
* Jade template path; | |
*/ | |
exports.templatePath = __dirname + '/../views/emails/'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* MailWrapper - An extremely simple nodemailer wrapper library that makes it easy to send emails using Express and Jade templates. Originally | |
* developed to practice and learn how to write modules for node. | |
* | |
* Copyright(c) 2011 Tom Shaw <[email protected]> | |
* MIT Licensed | |
*/ | |
/** | |
* Module dependencies. | |
*/ | |
var nodemailer = require('nodemailer'), | |
jade = require('jade'), | |
path = require('path'), | |
config = require('../config/mailer'); | |
/** | |
* Mailer pseudo class. | |
* | |
* @param obj user | |
* | |
*/ | |
var Mailer = function Mailer(user) { | |
if (user) this.merge(this, { _userdata: user }); | |
if (config.message) this.merge(this, config.message); | |
if (config.server) this.merge(this, config.server); | |
nodemailer.SMTP = this.smtp; | |
this._path = config.templatePath; | |
}; | |
/** | |
* Mailer prototype. | |
*/ | |
Mailer.prototype = { | |
/** | |
* Merges object properties. | |
*/ | |
merge: function(a, b) { | |
if (a && b) { | |
for (var k in b) { | |
a[k] = b[k]; | |
} | |
} | |
return a; | |
}, | |
/** | |
* Sets user data. | |
* | |
* @deprecated | |
*/ | |
set userdata(user) { | |
this._userdata = user; | |
}, | |
/** | |
* @returns User data fetched from persistence fed in thru the object constructor. | |
*/ | |
get userdata() { | |
return this._userdata; | |
}, | |
/** | |
* @returns Returns nodemailer required SMTP data. | |
*/ | |
get smtp() { | |
return { | |
ssl: this._ssl | |
, host: this._host | |
, port: this._port | |
, domain: this._domain | |
, use_authentication: this._use_authentication | |
, user: this._username | |
, pass: this._password | |
, debug: this._debug | |
} | |
}, | |
/** | |
* Generates some basic email instructions for nodemailer. | |
* | |
* _to: - Who the email is being sent to. | |
* _sender: - Who the email is being sent from. | |
* _subject: - The defauly email subject. | |
* _reply_to: - The email address to reply to. | |
* | |
* @returns Creates and returns an array of default email data. | |
*/ | |
get data() { | |
return { | |
to: this.userdata.email | |
, sender: this._from | |
, subject: this._subject | |
, reply_to: this._reply_to | |
} | |
}, | |
/** | |
* Sends the actual email message. | |
*/ | |
send: function() { | |
var data = this.data; | |
var userdata = this.userdata; | |
var debug = this._debug; | |
console.log(this._path + this._template); | |
jade.renderFile(this._path + this._template, { locals: { user: userdata } }, function(err, file) { | |
if(err) console.log(err); | |
//data.html = file; | |
data.body = file; | |
nodemailer.send_mail(data, function(error, success) { | |
if(debug) { | |
if (error) { | |
console.log(error); | |
} else if (success) { | |
console.log('[SUCCESSFULL EMAIL SENT TO]: ' + userdata.email); | |
} | |
} | |
}); | |
}); | |
}, | |
/** | |
* Returns objects properties in JSON format. | |
*/ | |
toJSON: function() { | |
return this.data; | |
} | |
}; | |
/** | |
* Exposed methods. | |
*/ | |
/** | |
* Sends a thank you message. | |
*/ | |
module.exports.sendThankyou = function(user) { | |
var mailer = new Mailer(user); | |
mailer._subject = 'Thank you for your purchase!'; | |
mailer._template = 'thankyou.jade'; | |
mailer.send(); | |
return mailer; | |
} | |
/** | |
* Send a registration email confirmation message. | |
*/ | |
module.exports.sendConfirmation = function(user) { | |
var mailer = new Mailer(user); | |
mailer._subject = 'Web Site Email Confirmation'; | |
mailer._template = 'confirmation.jade'; | |
mailer.send(); | |
return mailer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment