Skip to content

Commit

Permalink
feat(email-plugin): Enable logging for SMTP transport
Browse files Browse the repository at this point in the history
Integrates the native Nodemailer SMTP logging functionality with the configured VendureLogger.

Closes #369
  • Loading branch information
michaelbromley committed Jun 9, 2020
1 parent 1a9ac07 commit 5ed6c24
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/email-plugin/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const EMAIL_PLUGIN_OPTIONS = Symbol('EMAIL_PLUGIN_OPTIONS');
export const loggerCtx = 'EmailPlugin';
39 changes: 39 additions & 0 deletions packages/email-plugin/src/email-sender.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { normalizeString } from '@vendure/common/lib/normalize-string';
import { assertNever } from '@vendure/common/lib/shared-utils';
import { Logger } from '@vendure/core';
import fs from 'fs-extra';
import { createTransport } from 'nodemailer';
import { default as Mail } from 'nodemailer/lib/mailer';
import SendmailTransport from 'nodemailer/lib/sendmail-transport';
import { LoggerLevel } from 'nodemailer/lib/shared';
import SMTPTransport from 'nodemailer/lib/smtp-transport';
import path from 'path';
import { Stream } from 'stream';
import { format } from 'util';

import { loggerCtx } from './constants';
import { EmailDetails, EmailTransportOptions, SendmailTransportOptions, SMTPTransportOptions } from './types';

export type StreamTransportInfo = {
Expand Down Expand Up @@ -59,6 +63,7 @@ export class EmailSender {

private getSmtpTransport(options: SMTPTransportOptions) {
if (!this._smtpTransport) {
(options as any).logger = options.logging ? this.createLogger() : false;
this._smtpTransport = createTransport(options);
}
return this._smtpTransport;
Expand Down Expand Up @@ -110,4 +115,38 @@ export class EmailSender {
});
});
}

/**
* Adapts the VendureLogger to work with the bunyan-compatible logger format
* used by Nodemailer.
*/
private createLogger() {
function formatError(args: [object, string, ...string[]]) {
const [ctx, message, ...params] = args;
return format(message, ...params);
}
return {
level(level: LoggerLevel) {
/* noop */
},
trace(...params: any) {
Logger.debug(formatError(params), loggerCtx);
},
debug(...params: any) {
Logger.verbose(formatError(params), loggerCtx);
},
info(...params: any) {
Logger.info(formatError(params), loggerCtx);
},
warn(...params: any) {
Logger.warn(formatError(params), loggerCtx);
},
error(...params: any) {
Logger.error(formatError(params), loggerCtx);
},
fatal(...params: any) {
Logger.error(formatError(params), loggerCtx);
},
};
}
}
22 changes: 22 additions & 0 deletions packages/email-plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ export interface SMTPTransportOptions {
* Defines preferred authentication method, e.g. ‘PLAIN’
*/
authMethod?: string;
/**
* @description
* If true, uses the configured {@link VendureLogger} to log messages from Nodemailer as it interacts with
* the SMTP server.
*
* @default false
*/
logging?: boolean;
/**
* @description
* If set to true, then logs SMTP traffic without message content.
*
* @default false
*/
transactionLog?: boolean;
/**
* @description
* If set to true, then logs SMTP traffic and message content, otherwise logs only transaction events.
*
* @default false
*/
debug?: boolean;
}

/**
Expand Down

0 comments on commit 5ed6c24

Please sign in to comment.