authelia/server/src/lib/notifiers/MailSender.ts
Clement Michaud 78f6028c1b Improve logging format for clarity
Previously, logs were not very friendly and it was hard to track
a request because of the lack of request ID.
Now every log message comes with a header containing: method, path
request ID, session ID, IP of the user, date.

Moreover, the configurations displayed in the logs have their secrets
hidden from this commit.
2017-10-08 22:33:50 +02:00

42 lines
1.4 KiB
TypeScript

import { IMailSender } from "./IMailSender";
import Nodemailer = require("nodemailer");
import NodemailerDirectTransport = require("nodemailer-direct-transport");
import NodemailerSmtpTransport = require("nodemailer-smtp-transport");
import BluebirdPromise = require("bluebird");
export class MailSender implements IMailSender {
private transporter: Nodemailer.Transporter;
constructor(options: NodemailerDirectTransport.DirectOptions |
NodemailerSmtpTransport.SmtpOptions, nodemailer: typeof Nodemailer) {
this.transporter = nodemailer.createTransport(options);
}
verify(): BluebirdPromise<void> {
const that = this;
return new BluebirdPromise(function (resolve, reject) {
that.transporter.verify(function (error: Error, success: any) {
if (error) {
reject(new Error("Unable to connect to SMTP server. \
Please check the service is running and your credentials are correct."));
return;
}
resolve();
});
});
}
send(mailOptions: Nodemailer.SendMailOptions): BluebirdPromise<void> {
const that = this;
return new BluebirdPromise(function (resolve, reject) {
that.transporter.sendMail(mailOptions, (error: Error,
data: Nodemailer.SentMessageInfo) => {
if (error) {
reject(new Error("Error while sending email: " + error.message));
return;
}
resolve();
});
});
}
}