From c1271d4d1bbaf470b3009cc3bd0eccc99e40a8a5 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Mon, 14 Nov 2016 13:31:47 -0700 Subject: [PATCH] mailer module v1 --- .editorconfig | 2 +- package.json | 1 + services/mailer.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 services/mailer.js diff --git a/.editorconfig b/.editorconfig index 9c378bd19..986314661 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,7 +1,7 @@ root = true [*] -indent_style = tab +indent_style = space end_of_line = lf charset = utf-8 trim_trailing_whitespace = true diff --git a/package.json b/package.json index db7117b47..110a7c47d 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "mongoose": "^4.6.5", "morgan": "^1.7.0", "nodemailer": "^2.6.4", + "nodemailer-sendgrid-transport": "^0.2.0", "prompt": "^1.0.0", "uuid": "^2.0.3" }, diff --git a/services/mailer.js b/services/mailer.js new file mode 100644 index 000000000..ec1dd5f5e --- /dev/null +++ b/services/mailer.js @@ -0,0 +1,52 @@ +const nodemailer = require('nodemailer'); +const sgTransport = require('nodemailer-sendgrid-transport'); +const options = { + auth: { + api_key: process.env.TALK_SENDGRID_APIKEY + } +}; + +const transporter = nodemailer.createTransport(sgTransport(options)); + +transporter.sendMail({ + from: 'support@mrdavis.com', + to: 'riley.davis@gmail.com', + subject: 'this is only a test', + text: 'this is the body of the email maybe?', + html: ` + + + + + + + + +
foobar
rileydavis
` +}); + +const mailer = { + /** + * sendSimple + * + * @param {Object} {from, to, subject, text = '', html = ''} + * @returns + */ + sendSimple ({from, to, subject, text = '', html = ''}) { + return new Promise((resolve, reject) => { + if (!from) { + reject('sendSimple requires a from address'); + } + if (!to) { + reject('sendSimple requires a comma-separated list of "to" addresses'); + } + if (!subject) { + reject('sendSimple requires a subject for the email'); + } + + return resolve(transporter.sendMail({from, to, subject, text, html})); + }); + } +}; + +module.exports = mailer;