Skip to content

Instantly share code, notes, and snippets.

@gr2m
Last active December 17, 2015 05:19
Show Gist options
  • Save gr2m/5557208 to your computer and use it in GitHub Desktop.
Save gr2m/5557208 to your computer and use it in GitHub Desktop.
This is dreamcode for the Hoodie (http://hood.ie) Email worker, doing the backend magic behind hoodie.email.send( options ). I'm not a Node person, just dreaming out loud ;-)
function Worker(app) {
this.app = app;
app.on('add:$email', this.sendEmail.bind(this) );
}
Worker.prototype.sendEmail = function(email) {
// check if email has been configured
var emailConfig = app.config.get("email")
if (! emailConfig) {
email.$error = "Email has not yet been configured for " app.name;
this.app.save(email);
return
}
// validate
var error = this.validate(email)
if(error) {
this.app.warn( error );
email.$error = error;
this.app.save(email);
return
}
// there should be an app wide method to send emails
this.app.sendEmail( email, emailConfig, function(error) {
if (error) {
email.$error = error;
this.app.save(email);
return;
}
email.sentAt = new Date()
this.app.remove( email )
}.bind(this) ))
};
Worker.prototype.validate = function(email) {
// do some validation, return error if there is one,
// return nothing if we're good
return
};
module.exports = function (app) {
return new Worker(app);
};
{
"to": "joe@example.com",
"subject": "Hello, world!",
"text": "Bla bla bla"
}
{
"to": "joe@example3.com",
"subject": "Hello, world!",
"text": "Bla bla bla",
"sentAt": "2013-01-01T20:00:00",
"_deleted": true
}
{
"to": "joe@example3.com",
"subject": "Hello, world!",
"text": "Bla bla bla",
"$error": "Email could not be sent"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment