Skip to content

Commit

Permalink
Method for creating dynamically additional transporters
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkracik committed Jul 11, 2022
1 parent 49da597 commit 110294b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/mailer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import { TemplateAdapter } from './interfaces/template-adapter.interface';
import { ISendMailOptions } from './interfaces/send-mail-options.interface';
import { MailerTransportFactory as IMailerTransportFactory } from './interfaces/mailer-transport-factory.interface';
import { MailerTransportFactory } from './mailer-transport.factory';
import SMTPTransport = require('nodemailer/lib/smtp-transport');

@Injectable()
export class MailerService {
private transporter!: Transporter;
private transporters = new Map<string, Transporter>();

private templateAdapter: TemplateAdapter;
private initTemplateAdapter(
templateAdapter: TemplateAdapter,
transporter: Transporter,
Expand Down Expand Up @@ -65,7 +66,7 @@ export class MailerService {
}

/** Adapter setup **/
const templateAdapter: TemplateAdapter = get(
this.templateAdapter = get(
this.mailerOptions,
'template.adapter',
);
Expand All @@ -91,14 +92,14 @@ export class MailerService {
this.mailerOptions.transports![name],
),
);
this.initTemplateAdapter(templateAdapter, this.transporters.get(name)!);
this.initTemplateAdapter(this.templateAdapter, this.transporters.get(name)!);
});
}

/** Transporter setup **/
if (mailerOptions.transport) {
this.transporter = this.transportFactory.createTransport();
this.initTemplateAdapter(templateAdapter, this.transporter);
this.initTemplateAdapter(this.templateAdapter, this.transporter);
}
}

Expand Down Expand Up @@ -126,4 +127,13 @@ export class MailerService {
}
}
}

addTransporter(transporterName: string, config: string | SMTPTransport | SMTPTransport.Options): string {
this.transporters.set(
transporterName,
this.transportFactory.createTransport(config),
);
this.initTemplateAdapter(this.templateAdapter, this.transporters.get(transporterName)!);
return transporterName;
}
}

0 comments on commit 110294b

Please sign in to comment.