Last active
December 26, 2015 02:29
-
-
Save JasonPunyon/7078710 to your computer and use it in GitHub Desktop.
redis-queued, underscore-templated, markdown-formatted, juice-styled e-mails in not that many lines of code.
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
{ | |
"name": "philip-j-fry", | |
"version": "0.1.0", | |
"author": "Jason Punyon <jason.punyon@gmail.com>", | |
"description": "delivery boy", | |
"contributors": [ | |
{ | |
"name": "Jason Punyon", | |
"email": "jason.punyon@gmail.com" | |
} | |
], | |
"dependencies": { | |
"redis": ">0.0.0", | |
"nodemailer": ">0.0.0", | |
"markdown": ">0.0.0", | |
"underscore": ">0.0.0", | |
"juice": ">0.0.0" | |
}, | |
"private": true, | |
"jshintConfig": { | |
"node": true | |
} | |
} |
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
var redis = require('redis'), | |
redis_client = redis.createClient(6379, 'localhost'), | |
markdown = require('markdown').markdown, | |
_ = require('underscore'), | |
juice = require('juice'); | |
_.templateSettings = { | |
interpolate: /\{\{=(.+?)\}\}/g | |
}; | |
//The templates | |
var templates = { | |
en: { | |
test: "**This is some markdown right here...** *Don't you love it?*\n#{{= name }}\nThis is from the templates object." | |
}, | |
de: { | |
test: "**Dies ist ein markdown direkt hier...**\n#{{= name }}" | |
} | |
}; | |
//The stylesheets | |
var stylesheets = { | |
test: "h1 { color: red; }" | |
}; | |
//The Mail. | |
var nodemailer = require('nodemailer'); | |
var smtpTransport = nodemailer.createTransport("SMTP", { | |
service: "Gmail", | |
auth: { | |
user: '<put in a user name here>', | |
pass: '<put in a password here>' | |
} | |
}); | |
var sendMail = function(message) { | |
smtpTransport.sendMail(message, function(error, response) { | |
if (error) { | |
console.log(error); | |
} else { | |
console.log("Message sent: " + response.message); | |
} | |
}); | |
}; | |
//The Mail pump. | |
var events = require('events'), | |
mailMessageEmitter = new events.EventEmitter(); | |
//Start listening to the redis queue. | |
var pumpMailMessage = function() { | |
redis_client.blpop('mail-messages', 0, function(err, data) { | |
mailMessageEmitter.emit('mail-message', data); | |
pumpMailMessage(); | |
}); | |
}; | |
pumpMailMessage(); | |
mailMessageEmitter.on('mail-message', function(data_json) { | |
var data = JSON.parse(data_json[1]); | |
var filledInTemplate = _.template(templates[data.language][data.template_name], JSON.parse(data.model)); | |
data.html = markdown.toHTML(filledInTemplate); | |
data.html = juice.inlineContent(data.html, stylesheets[data.template_name]); | |
data.text = data.filledInTemplate; | |
sendMail(data); | |
}); | |
console.log('Delivery boy Philip J. Fry, reporting for duty.'); |
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
void Main() | |
{ | |
var client = new BookSleeve.RedisConnection("localhost"); | |
client.Open(); | |
client.Lists.AddLast(0, "mail-messages", new { | |
to = "recipient", | |
@from = "sender", | |
subject = "Who loves markdown?", | |
template_name = "test", | |
language = "de", | |
model = new { name = "Bob Loblaw muhfuh." }.ToJson() | |
}.ToJson()); | |
client.Close(false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment