Added registration mail sending method

This commit is contained in:
Alexis
2020-12-27 17:28:49 +01:00
parent bdfdb1d21e
commit 10fdbade66
2 changed files with 83 additions and 0 deletions

14
api/smtp/config.js Normal file
View File

@@ -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,
};

69
api/smtp/mails.js Normal file
View File

@@ -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,
}