salix/print/core/smtp.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-01-22 08:55:35 +00:00
const nodemailer = require('nodemailer');
2019-10-29 06:46:44 +00:00
const config = require('./config');
const db = require('./database');
2019-01-22 08:55:35 +00:00
module.exports = {
init() {
if (!this.transporter)
this.transporter = nodemailer.createTransport(config.smtp);
},
send(options) {
options.from = `${config.app.senderName} <${config.app.senderEmail}>`;
2019-01-22 08:55:35 +00:00
2019-09-30 11:02:11 +00:00
if (process.env.NODE_ENV !== 'production') {
2019-10-29 06:46:44 +00:00
if (!config.smtp.auth.user)
2019-09-30 11:02:11 +00:00
return Promise.resolve(true);
options.to = config.app.senderEmail;
2019-09-30 11:02:11 +00:00
}
2019-01-22 08:55:35 +00:00
let error;
return this.transporter.sendMail(options).catch(err => {
error = err;
throw err;
}).finally(async() => {
await db.rawSql(`
INSERT INTO vn.mail (sender, replyTo, sent, subject, body, status)
VALUES (:recipient, :sender, 1, :subject, :body, :status)`, {
2020-05-20 14:23:53 +00:00
sender: options.replyTo,
recipient: options.to,
subject: options.subject,
body: options.text || options.html,
status: error && error.message || 'Sent'
});
});
2019-09-30 11:02:11 +00:00
}
2019-01-22 08:55:35 +00:00
};