Created
March 12, 2014 03:48
-
-
Save chyld/9500500 to your computer and use it in GitHub Desktop.
send email from node.js
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
'use strict'; | |
var request = require('request'); | |
var fs = require('fs'); | |
var jade = require('jade'); | |
exports.sendWelcome = function(data, fn){ | |
send({from:'[email protected]', to:data.to, subject:'Welcome to AirBnB', template:'welcome'}, fn); | |
}; | |
function send(data, fn){ | |
if(data.to.match(/@nomail.com/g)){fn(); return;} | |
var key = process.env.MAILGUN; | |
var url = 'https://api:' + key + '@api.mailgun.net/v2/chyld.io/messages'; | |
var post = request.post(url, function(err, response, body){ | |
fn(err, body); | |
}); | |
var form = post.form(); | |
form.append('from', data.from); | |
form.append('to', data.to); | |
form.append('subject', data.subject); | |
form.append('html', compileJade(data)); | |
} | |
function compileJade(data){ | |
var template = __dirname + '/../views/email/' + data.template + '.jade'; | |
var original = fs.readFileSync(template, 'utf8'); | |
var partial = jade.compile(original); | |
var output = partial(data); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment