mailer.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* vim: set ts=2: */
  2. // Core
  3. var fs = require( 'fs' );
  4. // Amazon
  5. var aws = require( 'aws-lib' );
  6. // Templating
  7. var ejs = require( 'ejs' );
  8. function buildPath( template ) {
  9. return './emails/' + template + '.ejs';
  10. }
  11. function Mailer( accessKey, secretKey ) {
  12. this.accessKey = accessKey;
  13. this.secretKey = secretKey;
  14. this.client = aws.createSESClient( this.accessKey, this.secretKey );
  15. }
  16. module.exports = Mailer;
  17. Mailer.prototype.send = function( msg, callback ) {
  18. if( ! msg.template && ! msg.body ) {
  19. callback( new Error( 'Invalid message specification!' ) );
  20. }
  21. if( msg.template ) {
  22. var templatePath = buildPath( msg.template );
  23. var data = fs.readFileSync( templatePath, 'utf8' );
  24. msg.body = ejs.render( data, { locals: msg.locals } ); // .replace( /\\n/g, '\n' );
  25. }
  26. var params = {
  27. 'Destination.ToAddresses.member.1' : msg.to,
  28. 'Message.Body.Html.Charset' : 'UTF-8',
  29. 'Message.Body.Html.Data' : msg.body,
  30. 'Message.Subject.Charset' : 'UTF-8',
  31. 'Message.Subject.Data' : msg.subject,
  32. 'Source' : 'FinalsClub.org <info@finalsclub.org>'
  33. };
  34. this.client.call( 'SendEmail', params, function( result ) {
  35. console.log( result );
  36. if( result.Error ) {
  37. callback( result.Error );
  38. } else {
  39. callback( null, result );
  40. }
  41. });
  42. }