4 var fs = require( 'fs' );
7 var aws = require( 'aws-lib' );
10 var ejs = require( 'ejs' );
12 function buildPath( template ) {
13 return './emails/' + template + '.ejs';
16 function Mailer( accessKey, secretKey ) {
17 this.accessKey = accessKey;
18 this.secretKey = secretKey;
20 this.client = aws.createSESClient( this.accessKey, this.secretKey );
23 module.exports = Mailer;
25 Mailer.prototype.send = function( msg, callback ) {
26 if( ! msg.template && ! msg.body ) {
27 callback( new Error( 'Invalid message specification!' ) );
31 var templatePath = buildPath( msg.template );
33 var data = fs.readFileSync( templatePath, 'utf8' );
35 msg.body = ejs.render( data, { locals: msg.locals } ); // .replace( /\\n/g, '\n' );
39 'Destination.ToAddresses.member.1' : msg.to,
40 'Message.Body.Html.Charset' : 'UTF-8',
41 'Message.Body.Html.Data' : msg.body,
42 'Message.Subject.Charset' : 'UTF-8',
43 'Message.Subject.Data' : msg.subject,
44 'Source' : 'FinalsClub.org <info@finalsclub.org>'
47 this.client.call( 'SendEmail', params, function( result ) {
48 console.log( result );
51 callback( result.Error );
53 callback( null, result );