Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

init

  • Loading branch information...
commit 4d409c291b5ab7558e022d8e72a03a61c390f988 0 parents
@vdemedes authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
45 Readme.md
@@ -0,0 +1,45 @@
+# Mailman
+
+Send emails in a comfortable way via models.
+
+# Installation
+
+`npm install mailman`
+
+# Usage
+
+## Connecting
+
+```coffee-script
+Mailman = require 'mailman'
+
+Mailman.connect service: 'smtp', host: 'smtp.gmail.com', user: 'test@gmail.com', password: 'test', ssl: yes
+```
+
+## Defining
+
+```coffee-script
+class Notifier extends Mailman.Model
+ from: 'support@newapp.com'
+ subject: 'NewApp launched!'
+ text: 'Yes yes yes, you heard it.'
+
+Notifier = Mailman.setup Notifier # this is required
+```
+
+## Sending
+
+```coffee-script
+mail = new Notifier
+mail.to = 'recipient@gmail.com'
+mail.deliver (err, response) ->
+ # email sent
+```
+
+# Tests
+
+Put in your auth credentials into **test/mailman.test.coffee** and run `mocha` in Terminal.
+
+# License
+
+MIT.
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/mailman');
29 lib/mailman.coffee
@@ -0,0 +1,29 @@
+Mailer = require 'nodemailer'
+
+class Mailman
+ @connect: (params) ->
+ transport = if params.service.toLowerCase() is 'ses' then 'SES' else 'SMTP'
+ params.auth = user: params.user, pass: params.password
+ params.secureConnection = !! params.ssl
+ @transport = Mailer.createTransport transport, params
+
+ @setup: (model) ->
+ model::transport = @transport
+ model
+
+class Mailman.Model
+ constructor: ->
+ @attachments = []
+ @generateTextFromHTML = yes
+
+ deliver: (callback) ->
+ keys = ['from', 'to', 'cc', 'bcc', 'replyTo', 'subject', 'text', 'html', 'headers', 'attachments', 'encoding']
+ params = {}
+ for key in keys
+ params[key] = @[key] if @[key] and @[key].length > 0
+
+ @transport.sendMail params, callback
+
+ send: -> @deliver.apply @, arguments
+
+module.exports = Mailman
58 lib/mailman.js
@@ -0,0 +1,58 @@
+// Generated by CoffeeScript 1.3.3
+var Mailer, Mailman;
+
+Mailer = require('nodemailer');
+
+Mailman = (function() {
+
+ function Mailman() {}
+
+ Mailman.connect = function(params) {
+ var transport;
+ transport = params.service.toLowerCase() === 'ses' ? 'SES' : 'SMTP';
+ params.auth = {
+ user: params.user,
+ pass: params.password
+ };
+ params.secureConnection = !!params.ssl;
+ return this.transport = Mailer.createTransport(transport, params);
+ };
+
+ Mailman.setup = function(model) {
+ model.prototype.transport = this.transport;
+ return model;
+ };
+
+ return Mailman;
+
+})();
+
+Mailman.Model = (function() {
+
+ function Model() {
+ this.attachments = [];
+ this.generateTextFromHTML = true;
+ }
+
+ Model.prototype.deliver = function(callback) {
+ var key, keys, params, _i, _len;
+ keys = ['from', 'to', 'cc', 'bcc', 'replyTo', 'subject', 'text', 'html', 'headers', 'attachments', 'encoding'];
+ params = {};
+ for (_i = 0, _len = keys.length; _i < _len; _i++) {
+ key = keys[_i];
+ if (this[key] && this[key].length > 0) {
+ params[key] = this[key];
+ }
+ }
+ return this.transport.sendMail(params, callback);
+ };
+
+ Model.prototype.send = function() {
+ return this.deliver.apply(this, arguments);
+ };
+
+ return Model;
+
+})();
+
+module.exports = Mailman;
17 package.json
@@ -0,0 +1,17 @@
+{
+ "name": "mailman",
+ "description": "Send emails in a comfortable way via models.",
+ "version": "0.1.0",
+ "author": "Vadim Demedes <sbioko@gmail.com>",
+ "license": "MIT",
+ "index": "./index.js",
+ "engine": {
+ "node": ">= 0.4"
+ },
+ "dependencies": {
+ "nodemailer": "latest"
+ },
+ "devDependencies": {
+ "should": "latest"
+ }
+}
24 test/mailman.test.coffee
@@ -0,0 +1,24 @@
+Mailman = require '../'
+
+should = require 'should'
+
+describe 'Mailman', ->
+ describe 'Transports', ->
+ describe 'SMTP', ->
+ it 'should send out an email', (done) ->
+ Mailman.connect host: 'smtp.sendgrid.net', service: 'smtp', port: 587, user: 'user', password: 'password'
+
+
+ class Notifier extends Mailman.Model
+ from: 'test@test.com'
+ subject: 'We launched!'
+ text: 'We launched, sign up right now!'
+
+ Notifier = Mailman.setup Notifier
+
+ mail = new Notifier
+ mail.to = 'test@test2.com'
+ mail.deliver (err, response) ->
+ should.not.exist err
+ done()
+
1  test/mocha.opts
@@ -0,0 +1 @@
+--compilers coffee:coffee-script --timeout 20000
Please sign in to comment.
Something went wrong with that request. Please try again.