-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implements sending registration confirmation emails with mandrill #85 &
- Loading branch information
Showing
2 changed files
with
34 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'; | ||
|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters