Skip to content

Instantly share code, notes, and snippets.

@knowthen
Last active August 29, 2015 14:13
Show Gist options
  • Save knowthen/dd81237f0a998a18aef1 to your computer and use it in GitHub Desktop.
Save knowthen/dd81237f0a998a18aef1 to your computer and use it in GitHub Desktop.
// @pdxiii a nodejs solution using koajs.
// If your new to nodejs you should checkout Koajs.
// You can avoid callbacks in koajs, which makes life alot easier
// if your using Express or other, solution would be similar just using callbacks
// not really an emberjs guy, couldn't give you the details on how to do
// an HTTP Post but should be trivial to lookup
var koa = require('koa'),
route = require('koa-router'),
nodemailer = require('nodemailer'),
parse = require('co-body'),
thunkify = require('thunkify'),
app = koa();
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'userpass'
}
});
// converts a function expecting a callback to a thunk so you can use
// JavaScript Generators. Wouldn't be neccesary if it returned a promise
var sendMail = thunkify(transporter.sendMail);
app.use(route(app));
// route handler HTTP POST /email
app.post('/email', function *(){
// grab posted data
var email = yield parse(this);
// object with info nodemailer is expecting
var mailOptions = {
from: email.from,
to: 'James <[email protected]>',
subject: 'Contact Form',
text: email.message
};
var result = yield sendMail(mailOptions);
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment