Browse Source

feat(email-plugin): Enable logging for SMTP transport

Integrates the native Nodemailer SMTP logging functionality with the configured VendureLogger.

Closes #369
Michael Bromley 5 years ago
parent
commit
5ed6c24a09

+ 1 - 0
packages/email-plugin/src/constants.ts

@@ -1 +1,2 @@
 export const EMAIL_PLUGIN_OPTIONS = Symbol('EMAIL_PLUGIN_OPTIONS');
+export const loggerCtx = 'EmailPlugin';

+ 39 - 0
packages/email-plugin/src/email-sender.ts

@@ -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 = {
@@ -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;
@@ -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 - 0
packages/email-plugin/src/types.ts

@@ -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;
 }
 
 /**