dotmail
Advanced tools
Comparing version 0.0.1 to 0.0.2
39
index.js
'use strict'; | ||
var doT = require('dot'), | ||
@@ -12,31 +13,27 @@ emailjs = require('emailjs'); | ||
} | ||
if (obj.length === undefined) { | ||
return true; | ||
} | ||
return false; | ||
return obj.length === undefined; | ||
}; | ||
var getCompiled = function (template) { | ||
var compiled = {}, | ||
var fns = {}, | ||
i; | ||
for (i in template) { | ||
compiled[i] = doT.template( template[i] ); | ||
fns[i] = doT.template( template[i] ); | ||
} | ||
return compiled; | ||
return fns; | ||
}; | ||
var Template = function (template) { | ||
var self = this; | ||
this.src = template; | ||
this.compiled = getCompiled( template ); | ||
this.getMessage = function (data) { | ||
var msg = {}, | ||
i; | ||
for (i in template) { | ||
msg[i] = self.compiled[i]( data ); | ||
} | ||
return msg; | ||
}; | ||
}; | ||
Template.prototype.getMessage = function (data) { | ||
var msg = {}, | ||
i; | ||
for (i in this.src) { | ||
msg[i] = this.compiled[i]( data ); | ||
} | ||
return msg; | ||
}; | ||
@@ -62,10 +59,10 @@ var dotmail = {}; | ||
if (typeof template === 'string') { | ||
template = templates[template].getMessage; | ||
template = templates[template]; | ||
} else if (!isObject( template )) { | ||
return callback( 'invalid template' ); | ||
} else { | ||
template = new Template( template ).getMessage; | ||
template = new Template( template ); | ||
} | ||
data = data || {}; | ||
var message = template( data ); | ||
var message = template.getMessage( data ); | ||
@@ -91,6 +88,2 @@ account.server.send( message, callback ); | ||
dotmail.getTemplate = function (key) { | ||
return templates[key].src; | ||
}; | ||
dotmail.removeTemplate = function (key) { | ||
@@ -97,0 +90,0 @@ delete templates[key]; |
{ | ||
"name": "dotmail", | ||
"version": "0.0.1", | ||
"description": "Send emails with doT templates", | ||
"version": "0.0.2", | ||
"description": "Send emails with templates", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
dotmail | ||
======= | ||
Send emails with doT templates. Works with SMTP | ||
Send emails with templates. | ||
Template engine: **[doT.js](https://github.com/olado/doT)** | ||
Email driver: **[emailjs](https://github.com/eleith/emailjs#emailserverconnectoptions)** | ||
Installation | ||
@@ -17,5 +21,4 @@ ------------ | ||
```js | ||
var dotmail = require('./index.js'); | ||
var dotmail = require('dotmail'); | ||
@@ -29,2 +32,3 @@ var account = { | ||
// you can write doT templates in template fields | ||
var template = { | ||
@@ -37,2 +41,9 @@ text: "You have {{=it.messages.length}} messages", | ||
// add an email account and connect it to its SMTP server | ||
dotmail.addAccount( 'main', account ); | ||
// add an email template with its key | ||
dotmail.addTemplate('weekly', template ); | ||
// data to render the template | ||
var data = { | ||
@@ -48,6 +59,2 @@ username: 'Neo', | ||
dotmail.addTemplate('weekly', template ); | ||
dotmail.addAccount( 'main', account ); | ||
dotmail.send( 'main', 'weekly', data, function (err, msg) { | ||
@@ -58,2 +65,67 @@ console.log( err || msg ); | ||
API | ||
--- | ||
### dotmail.send( account, template, data, callback ) | ||
Email server connection options | ||
------------------------------- | ||
[Same as **emailjs**](https://github.com/eleith/emailjs#emailserverconnectoptions). | ||
Message and attachments options | ||
------------------------------- | ||
Same as **emailjs**: | ||
**Message:** | ||
``` | ||
// headers is an object ('from' and 'to' are required) | ||
// returns a Message object | ||
// you can actually pass more message headers than listed, the below are just the | ||
// most common ones you would want to use | ||
headers = | ||
{ | ||
text // text of the email | ||
from // sender of the format (address or name <address> or "name" <address>) | ||
to // recipients (same format as above), multiple recipients are separated by a comma | ||
cc // carbon copied recipients (same format as above) | ||
bcc // blind carbon copied recipients (same format as above) | ||
subject // string subject of the email | ||
attachment // one attachment or array of attachments | ||
} | ||
``` | ||
**Attachments** | ||
``` | ||
// can be called multiple times, each adding a new attachment | ||
// options is an object with the following possible keys: | ||
options = | ||
{ | ||
// one of these fields is required | ||
path // string to where the file is located | ||
data // string of the data you want to attach | ||
stream // binary stream that will provide attachment data (make sure it is in the paused state) | ||
// better performance for binary streams is achieved if buffer.length % (76*6) == 0 | ||
// current max size of buffer must be no larger than Message.BUFFERSIZE | ||
// optionally these fields are also accepted | ||
type // string of the file mime type | ||
name // name to give the file as perceived by the recipient | ||
alternative // if true, will be attached inline as an alternative (also defaults type='text/html') | ||
inline // if true, will be attached inline | ||
encoded // set this to true if the data is already base64 encoded, (avoid this if possible) | ||
headers // object containing header=>value pairs for inclusion in this attachment's header | ||
related // an array of attachments that you want to be related to the parent attachment | ||
} | ||
``` | ||
<br><br> | ||
@@ -60,0 +132,0 @@ |
7784
133
77