Jelajahi Sumber

fix(email-plugin): Fix Dynamic smtp config require server restart to reflect changes (#3282)

Ankish Kumar 6 bulan lalu
induk
melakukan
d6d967dd8d
1 mengubah file dengan 17 tambahan dan 4 penghapusan
  1. 17 4
      packages/email-plugin/src/sender/nodemailer-email-sender.ts

+ 17 - 4
packages/email-plugin/src/sender/nodemailer-email-sender.ts

@@ -40,7 +40,14 @@ export class NodemailerEmailSender implements EmailSender {
     private _smtpTransport: Mail | undefined;
     private _sendMailTransport: Mail | undefined;
     private _sesTransport: Mail | undefined;
-
+    private isNew(transport: Mail, options: EmailTransportOptions, ){
+        try {
+        return  JSON.stringify(transport?.options) !== JSON.stringify(options)
+        } catch (error:any) {
+            Logger.error( format(error.message), loggerCtx);
+            return false;
+        }
+    }
     async send(email: EmailDetails, options: EmailTransportOptions) {
         switch (options.type) {
             case 'none':
@@ -76,7 +83,9 @@ export class NodemailerEmailSender implements EmailSender {
     }
 
     private getSmtpTransport(options: SMTPTransportOptions) {
-        if (!this._smtpTransport) {
+        if (!this._smtpTransport ||
+            this.isNew(this._smtpTransport, options)
+        ) {
             (options as any).logger = options.logging ? this.createLogger() : false;
             this._smtpTransport = createTransport(options);
         }
@@ -84,14 +93,18 @@ export class NodemailerEmailSender implements EmailSender {
     }
 
     private getSesTransport(options: SESTransportOptions) {
-        if (!this._sesTransport) {
+        if (!this._sesTransport ||
+           this.isNew(this._sesTransport,options)
+        ) {
             this._sesTransport = createTransport(options);
         }
         return this._sesTransport;
     }
 
     private getSendMailTransport(options: SendmailTransportOptions) {
-        if (!this._sendMailTransport) {
+        if (!this._sendMailTransport ||
+           this.isNew(this._sendMailTransport, { sendmail: true, ...options } as any)
+        ) {
             this._sendMailTransport = createTransport({ sendmail: true, ...options });
         }
         return this._sendMailTransport;