Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- * 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 <tom@tomshaw.info>
- * 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;
- }
Add Comment
Please, Sign In to add comment