Last active
July 19, 2018 20:36
-
-
Save pepesenaris/63378b3b0748f97bd820f26b9f75b367 to your computer and use it in GitHub Desktop.
Express mailer
This file contains hidden or 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
| //server/mailer.js | |
| var path = require("path"); | |
| var templatesDir = path.resolve(__dirname, "views/mailer"); | |
| var Email = require("email-templates"); | |
| const mailjet = require("node-mailjet").connect( | |
| process.env.MJ_APIKEY_PUBLIC, | |
| process.env.MJ_APIKEY_PRIVATE | |
| ); | |
| const sendEmail = (messageInfo, text, html) => { | |
| return mailjet.post("send", { version: "v3.1" }).request({ | |
| Messages: [ | |
| { | |
| From: { Email: messageInfo.fromEmail, Name: messageInfo.fromName }, | |
| To: [ { Email: messageInfo.email } ], | |
| Subject: messageInfo.subject, | |
| TextPart: text, | |
| HTMLPart: html | |
| } | |
| ] | |
| }); | |
| }; | |
| exports.sendOne = function(templateName, messageInfo, locals) { | |
| const email = new Email({ | |
| views: { root: templatesDir, options: { extension: "ejs" } } | |
| }); | |
| return Promise.all([ | |
| email.render(`${templateName}/html`, locals), | |
| email.render(`${templateName}/text`, locals) | |
| ]) | |
| .then(([html, text]) => { | |
| return sendEmail(messageInfo, text, html); | |
| }) | |
| .catch(console.error); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment