fipamo/brain/api/mail/mailer.js

82 lines
2.5 KiB
JavaScript

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var mg = require('nodemailer-mailgun-transport');
const fs = require('fs-extra');
const pug = require('pug');
router.post('/', function(req, res, next)
{
fs.readJson('config/site-settings.json').then(settings =>
{
let transport = ''
switch (settings.email.active)
{
case "option-smtp":
var auth = {
host: settings.email.smtp.domain,
port: 587,
secure: false,
auth:
{
type:"login",
email: settings.email.smtp,
password: settings.email.smtp.password
}
}
transport = nodemailer.createTransport((auth));
break;
case "option-mg":
var auth = {
auth:
{
api_key: settings.email.mailgun['api-key'],
domain: settings.email.mailgun.domain
}
}
transport = nodemailer.createTransport(mg(auth));
break
}
let render = pug.compileFile('themes/dash/email/base.pug');
let html = render(
{
title: settings.title,
header: "a note from " + settings.title,
content: req.body.content,
footer: "powered by fipamo"
})
transport.sendMail(
{
from: 'control@playvico.us',
to: 'are0h@protonmail.com', // An array if you have multiple recipients.
subject: 'Hey beautiful',
//You can use "html:" to send HTML email content. It's magic!
html: html
//You can use "text:" to send plain-text content. It's oldschool!
//text: 'Mailgun rocks, pow pow!'
}, function(err, info)
{
if (err)
{
console.log(err)
res.json(
{
message: "MAIL ERROR",
desc: err
});
}
else
{
console.log(info)
res.json(
{
message: "MAIL SENT",
desc: info
});
}
});
}).catch(err =>
{
console.error(err)
})
});
module.exports = router;