From 10fdbade665079938e95c1fa8e5e776013dd5a44 Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Sun, 27 Dec 2020 17:28:49 +0100 Subject: [PATCH] Added registration mail sending method --- api/smtp/config.js | 14 ++++++++++ api/smtp/mails.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 api/smtp/config.js create mode 100644 api/smtp/mails.js diff --git a/api/smtp/config.js b/api/smtp/config.js new file mode 100644 index 0000000..520c05c --- /dev/null +++ b/api/smtp/config.js @@ -0,0 +1,14 @@ +const nodemailer = require('nodemailer'); + +const transport = nodemailer.createTransport({ + host: "smtp.mailtrap.io", + port: 2525, + auth: { + user: process.env.SMTP_USER, + pass: process.env.SMTP_PASS, + } +}); + +module.exports = { + transport, +}; \ No newline at end of file diff --git a/api/smtp/mails.js b/api/smtp/mails.js new file mode 100644 index 0000000..6f638bd --- /dev/null +++ b/api/smtp/mails.js @@ -0,0 +1,69 @@ +const smtp = require('./config'); +const fs = require('fs'); +const handlebars = require('handlebars'); + +// Sender address for mail service +const sender = 'tymos@auracle.io'; + +// Fetches a HTML template file for parsing +const getTemplateFile = (path) => { + return new Promise((resolve, reject) => { + fs.readFile(path, {encoding: 'utf-8'}, (err, html) => { + if (err) { + reject(err); + } else { + resolve(html); + } + }) + }) +}; + +/** + * SEND REGISTRATION MAIL FUNCTION + * @param {Object} data + * - user + * - name + * - mail + * - token + */ +const sendRegistrationMail = (data) => { + getTemplateFile(__dirname + '/templates/template-sign-up.html') + .then(html => { + let template = handlebars.compile(html); + let template_parsed = template(data); + + let message = { + from: sender, + to: data.user.mail, + subject: 'Inscription Auracle.io', + html: template_parsed, + }; + + smtp.transport.sendMail(message, (err, info) => { + if (err) { + throw err; + } else { + console.log(info); + } + }); + }) + .catch(err => { + console.log(err); + }); +} + +const sendBanEmail = (date) => { + return null; +} + +sendRegistrationMail({ + user: { + name: 'Alexis', + mail: '35.alexis.pele@gmail.com', + token: '1fa98900-485f-11eb-b378-0242ac130002' + } +}); + +module.exports = { + sendRegistrationMail, +} \ No newline at end of file