Permalink
Browse files

added views

  • Loading branch information...
1 parent 4d409c2 commit 347461e9766e17f3293af5724a350377602a7ea3 @vadimdemedes committed Jul 19, 2012
Showing with 114 additions and 18 deletions.
  1. +23 −0 Readme.md
  2. +31 −4 lib/mailman.coffee
  3. +48 −8 lib/mailman.js
  4. +3 −2 package.json
  5. +6 −4 test/mailman.test.coffee
  6. +3 −0 test/views/notifier.eco
View
@@ -36,6 +36,29 @@ mail.deliver (err, response) ->
# email sent
```
+## Views
+
+If your emails contain dynamic information, why not to use all those template engines you love?
+
+```coffee-script
+Mailman.viewsPath = "#{ __dirname }/views"
+
+class Notifier extends Mailman.Model
+ from: 'support@newapp.com'
+ subject: 'NewApp launched!'
+ view: 'notifier' # Mailman will auto-guess the extension.
+ # This field is optional, class' name will be taken by default.
+
+Notifier = Mailman.setup Notifier
+
+mail = new Notifier
+mail.to = 'recipient@gmail.com'
+mail.name = 'Steve'
+mail.surname = 'Jobs'
+mail.deliver (err, response) ->
+ # email sent
+```
+
# Tests
Put in your auth credentials into **test/mailman.test.coffee** and run `mocha` in Terminal.
View
@@ -1,6 +1,15 @@
Mailer = require 'nodemailer'
+Templato = require 'templato'
+fs = require 'fs'
+{exec} = require 'child_process'
+
+extractExtension = (s) ->
+ parts = s.replace(/^.*[\/\\]/g, '').split '.'
+ parts[1..(parts.length - 1)].join '.'
class Mailman
+ @viewsPath: ''
+
@connect: (params) ->
transport = if params.service.toLowerCase() is 'ses' then 'SES' else 'SMTP'
params.auth = user: params.user, pass: params.password
@@ -9,6 +18,7 @@ class Mailman
@setup: (model) ->
model::transport = @transport
+ model::className = model.name
model
class Mailman.Model
@@ -17,12 +27,29 @@ class Mailman.Model
@generateTextFromHTML = yes
deliver: (callback) ->
- keys = ['from', 'to', 'cc', 'bcc', 'replyTo', 'subject', 'text', 'html', 'headers', 'attachments', 'encoding']
+ keys = ['from', 'to', 'cc', 'bcc', 'replyTo', 'subject', 'text', 'html', 'headers', 'attachments', 'encoding', 'className', 'template']
params = {}
- for key in keys
- params[key] = @[key] if @[key] and @[key].length > 0
+ variables = {}
+
+ for key of @
+ if 'string' is typeof @[key]
+ if -1 is keys.indexOf(key)
+ variables[key] = @[key]
+ else params[key] = @[key]
+
+ next = =>
+ params.html = @template.render variables
+ @transport.sendMail params, callback
- @transport.sendMail params, callback
+ if not (@text or @html)
+ if not @template
+ exec "find #{ Mailman.viewsPath } -iname '#{ @view or @className.toLowerCase() }*' -exec echo {} \\;", (err, stdout) =>
+ stdout = stdout.trim()
+ fs.readFile stdout, 'utf-8', (err, source) =>
+ @template = new Templato
+ @template.set engine: extractExtension(stdout), template: source
+ next()
+ else next()
send: -> @deliver.apply @, arguments
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,15 +1,16 @@
{
"name": "mailman",
"description": "Send emails in a comfortable way via models.",
- "version": "0.1.0",
+ "version": "0.1.1",
"author": "Vadim Demedes <sbioko@gmail.com>",
"license": "MIT",
"index": "./index.js",
"engine": {
"node": ">= 0.4"
},
"dependencies": {
- "nodemailer": "latest"
+ "nodemailer": "latest",
+ "templato": "0.1.0"
},
"devDependencies": {
"should": "latest"
View
@@ -6,18 +6,20 @@ 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'
-
+ Mailman.connect host: 'smtp.server.com', service: 'smtp', port: 587, user: 'user', password: 'password', ssl: no
+
+ Mailman.viewsPath = "#{ __dirname }/views"
class Notifier extends Mailman.Model
from: 'test@test.com'
subject: 'We launched!'
- text: 'We launched, sign up right now!'
+ view: 'notifier'
Notifier = Mailman.setup Notifier
mail = new Notifier
- mail.to = 'test@test2.com'
+ mail.to = 'test@gmail.com'
+ mail.name = 'Steve'
mail.deliver (err, response) ->
should.not.exist err
done()
View
@@ -0,0 +1,3 @@
+Hey, <%= @name %><br>
+
+<i>You rock!</i>

0 comments on commit 347461e

Please sign in to comment.