Commit d5f0445e authored by Janik Münzenberger's avatar Janik Münzenberger
Browse files

Email configuration

parent b96b6b81
const nodemailer = require('nodemailer');
const logger = require('winston');
const winston = require('winston');
const errors = require('../lib/errors');
var transport;
createTransport();
async function createTransport() {
try {
const user = process.env.EMAIL_USER;
const password = process.env.EMAIL_PASSWORD;
if (user && password) {
transport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: user,
pass: password
}
});
} else {
winston.warn('No user gmail user defined in environment variables. User ethereal mail test account');
let account = await nodemailer.createTestAccount();
return nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
}
} catch (err) {
winston.error(err);
}
}
function email(req, res) {
let sender = req.body.sender;
......@@ -10,41 +44,28 @@ function email(req, res) {
let msg = req.body.message;
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount().then(account => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
// create a setup message option
let mailOptions = {
from: process.env.EMAIL_USER || sender,
to: receiver,
subject: subject,
text: msg
};
// create a setup message option
let mailOptions = {
from: sender,
to: receiver,
subject: subject,
text: msg
};
// send the E-mail with sendMail using the defined transporter
transporter.sendMail(mailOptions)
.then(info => res.send({
// send the E-mail with sendMail using the defined transporter
transport.sendMail(mailOptions)
.then(info => {
res.send({
success: true,
data: info
}));
}).catch(err => {
winston.error(err);
errors.sendError(err);
});
});
}).catch(err => {
winston.error(err);
errors.sendError(err);
});
}
module.exports = {
email: email,
};
email
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment