53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
const nodemailer = require("nodemailer");
|
|
const mailValidator = require("email-validator");
|
|
|
|
var app = null
|
|
|
|
|
|
exports.init = function(express) {
|
|
app = express
|
|
|
|
return this;
|
|
}
|
|
|
|
exports.mail = function(data) {
|
|
const account = {
|
|
user: app.get('CONF').mail.smtp.auth.user,
|
|
pass: app.get('CONF').mail.smtp.auth.pass
|
|
}
|
|
|
|
let transporter = nodemailer.createTransport({
|
|
host: app.get('CONF').mail.smtp.host,
|
|
port: app.get('CONF').mail.smtp.port,
|
|
secure: app.get('CONF').mail.smtp.secure, // true for 465, false for other ports
|
|
auth: {
|
|
user: account.user, // generated ethereal user
|
|
pass: account.pass // generated ethereal password
|
|
}
|
|
});
|
|
|
|
// setup email data with unicode symbols
|
|
let mailOptions = {
|
|
from: extractFrom(data), // sender address
|
|
to: app.get('CONF').mail.request.to, // comma separated list of receivers
|
|
subject: "Equipment Anfrage", // Subject line
|
|
text: "Hello world?", // plain text body
|
|
html: "<b>Hello world?</b>" // html body
|
|
};
|
|
|
|
let info = transporter.sendMail(mailOptions)
|
|
|
|
app.get('LOG').log("Mailer | Message sent: %s", info.messageId);
|
|
|
|
return info
|
|
}
|
|
|
|
function extractFrom(data) {
|
|
if(mailValidator.validate(data.contact)) {
|
|
var name = data.name && data.name.length > 0 ? data.name : "👻"
|
|
var mail = data.contact
|
|
return `"${name}" ${mail}`
|
|
} else {
|
|
return `"👻" ${app.get('CONF').mail.from}`
|
|
}
|
|
} |