Skip to content

Commit

Permalink
implements sending registration confirmation emails with mandrill #85 &
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 17, 2015
1 parent ef4f331 commit 22c3bf6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
58 changes: 33 additions & 25 deletions api/lib/email_welcome.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var nodemailer = require('nodemailer');
var fs = require('fs');
var path = require('path');
var templatedir = __dirname +'/../email_templates/';
Expand All @@ -7,30 +6,39 @@ var textpath = path.resolve(templatedir + 'welcome_text.txt');
var template = fs.readFileSync(htmlpath, 'utf8')
var textonly = fs.readFileSync(textpath, 'utf8')
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: process.env.GMAIL_PASSWORD
}
});
var mandrill = require('mandrill-api');
var mandrill_client = new mandrill.Mandrill(process.env.MANDRILL_APIKEY);

// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails
module.exports = function email(person, callback) {
var message = {
"html": template,
"text": textonly,
"subject": "Welcome to DWYL",
"from_email": "[email protected]",
"from_name": "Hello from DWYL",
"to": [{
"email": person.email,
// "name": "FirstName", // not using this for now.
"type": "to"
}],
"headers": {
"Reply-To": "[email protected]"
},
"important": false,
"track_opens": true,
"track_clicks": true,
"tags": [
"registration"
],
};

var email = function(person, callback){
var mailOptions = {
from: '#dwyl do what you love! <[email protected]>', // sender
to: person.email, // list of receivers
subject: 'Welcome to dwyl!', // Subject line
text: textonly, // plaintext body
html: template
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
// console.log(error, info);
callback(error, info)
});
}
mandrill_client.messages.send({"message": message}, function(result) {
console.log(result);
return callback(result);
}, function(error) {
// Mandrill returns the error as an object with name and message keys
console.log('MANDRILL ERROR: ' + error.name + ' - ' + error.message);
// A mandrill error occurred: Unknown_Subaccount - No subaccount exists with the id 'customer-123'
});

module.exports = email;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"joi": "^6.1.0",
"jsonwebtoken": "^5.0.0",
"lout": "^6.2.1",
"mandrill-api": "^1.0.45",
"nodemailer": "^1.3.4",
"perma": "^2.0.1",
"sad": "^1.0.7"
Expand Down

0 comments on commit 22c3bf6

Please sign in to comment.