Update logging for smtp emails (#3536)

This commit is contained in:
martmull 2024-01-22 16:06:10 +01:00 committed by GitHub
parent f1b3d1537a
commit e45a825a3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 4 deletions

View File

@ -1,9 +1,12 @@
import { Logger } from '@nestjs/common';
import { createTransport, Transporter, SendMailOptions } from 'nodemailer';
import SMTPConnection from 'nodemailer/lib/smtp-connection';
import { EmailDriver } from 'src/integrations/email/drivers/interfaces/email-driver.interface';
export class SmtpDriver implements EmailDriver {
private readonly logger = new Logger(SmtpDriver.name);
private transport: Transporter;
constructor(options: SMTPConnection.Options) {
@ -11,6 +14,13 @@ export class SmtpDriver implements EmailDriver {
}
async send(sendMailOptions: SendMailOptions): Promise<void> {
await this.transport.sendMail(sendMailOptions);
this.transport
.sendMail(sendMailOptions)
.then(() =>
this.logger.log(`Email to '${sendMailOptions.to}' successfully sent`),
)
.catch((err) =>
this.logger.error(`sending email to '${sendMailOptions.to}': ${err}`),
);
}
}

View File

@ -1,4 +1,4 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { SendMailOptions } from 'nodemailer';
@ -8,11 +8,9 @@ import { EmailSenderService } from 'src/integrations/email/email-sender.service'
@Injectable()
export class EmailSenderJob implements MessageQueueJob<SendMailOptions> {
private readonly logger = new Logger(EmailSenderJob.name);
constructor(private readonly emailSenderService: EmailSenderService) {}
async handle(data: SendMailOptions): Promise<void> {
await this.emailSenderService.send(data);
this.logger.log(`Email to ${data.to} sent`);
}
}