@nestjs-modules/mailer 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.env ADDED
@@ -0,0 +1,2 @@
1
+ EMAIL_ID=example@example.com
2
+ EMAIL_PASS=your-email-password
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [2.0.1](https://github.com/nest-modules/mailer/compare/v2.0.0...v2.0.1) (2024-04-30)
6
+
5
7
  ## [2.0.0](https://github.com/nest-modules/mailer/compare/v1.11.2...v2.0.0) (2024-04-27)
6
8
 
7
9
 
@@ -0,0 +1,8 @@
1
+ import { MailerOptions } from '../interfaces/mailer-options.interface';
2
+ import { TemplateAdapter } from '../interfaces/template-adapter.interface';
3
+ import { Liquid } from 'liquidjs';
4
+ export declare class LiquidAdapter implements TemplateAdapter {
5
+ private config;
6
+ constructor(config?: Partial<Liquid['options']>);
7
+ compile(mail: any, callback: any, mailerOptions: MailerOptions): void;
8
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LiquidAdapter = void 0;
4
+ const path = require("path");
5
+ const lodash_1 = require("lodash");
6
+ const liquidjs_1 = require("liquidjs");
7
+ class LiquidAdapter {
8
+ constructor(config) {
9
+ Object.assign(this.config, config);
10
+ }
11
+ compile(mail, callback, mailerOptions) {
12
+ const { context, template } = mail.data;
13
+ const templateExt = path.extname(template) || '.liquid';
14
+ const templateName = path.basename(template, path.extname(template));
15
+ const templateDir = path.isAbsolute(template)
16
+ ? path.dirname(template)
17
+ : path.join((0, lodash_1.get)(mailerOptions, 'template.dir', ''), path.dirname(template));
18
+ const engine = new liquidjs_1.Liquid(Object.assign({ extname: templateExt, root: templateDir }, this.config.globals));
19
+ engine.renderFile(templateName, context).then((body) => {
20
+ mail.data.html = body;
21
+ return callback();
22
+ }).catch((err) => {
23
+ return callback(err);
24
+ });
25
+ }
26
+ }
27
+ exports.LiquidAdapter = LiquidAdapter;
@@ -4,7 +4,7 @@ exports.MjmlAdapter = void 0;
4
4
  const handlebars_adapter_1 = require("./handlebars.adapter");
5
5
  const ejs_adapter_1 = require("./ejs.adapter");
6
6
  const pug_adapter_1 = require("./pug.adapter");
7
- const mjml2html = require("mjml");
7
+ const mjml_1 = require("mjml");
8
8
  class MjmlAdapter {
9
9
  constructor(engine, config, others) {
10
10
  this.engine = engine;
@@ -26,7 +26,7 @@ class MjmlAdapter {
26
26
  compile(mail, callback, mailerOptions) {
27
27
  var _a;
28
28
  (_a = this === null || this === void 0 ? void 0 : this.engine) === null || _a === void 0 ? void 0 : _a.compile(mail, () => {
29
- mail.data.html = mjml2html(mail.data.html).html;
29
+ mail.data.html = (0, mjml_1.default)(mail.data.html).html;
30
30
  callback();
31
31
  }, mailerOptions);
32
32
  }
@@ -31,5 +31,6 @@ export interface MailerOptions {
31
31
  app: string | string[];
32
32
  };
33
33
  }>;
34
+ verifyTransporters?: boolean;
34
35
  }
35
36
  export {};
@@ -12,7 +12,11 @@ export declare class MailerService {
12
12
  private initTemplateAdapter;
13
13
  private readonly mailerLogger;
14
14
  constructor(mailerOptions: MailerOptions, transportFactory: IMailerTransportFactory);
15
+ private validateTransportOptions;
16
+ private createTransporter;
17
+ private setupTransporters;
15
18
  private verifyTransporter;
19
+ verifyAllTransporters(): Promise<boolean>;
16
20
  sendMail(sendMailOptions: ISendMailOptions): Promise<SentMessageInfo>;
17
21
  addTransporter(transporterName: string, config: string | smtpTransport | smtpTransport.Options): string;
18
22
  }
@@ -5,7 +5,6 @@ exports.MailerService = void 0;
5
5
  const tslib_1 = require("tslib");
6
6
  const lodash_1 = require("lodash");
7
7
  const common_1 = require("@nestjs/common");
8
- const previewEmail = require("preview-email");
9
8
  const mailer_constant_1 = require("./constants/mailer.constant");
10
9
  const mailer_transport_factory_1 = require("./mailer-transport.factory");
11
10
  let MailerService = MailerService_1 = class MailerService {
@@ -17,11 +16,24 @@ let MailerService = MailerService_1 = class MailerService {
17
16
  }
18
17
  return templateAdapter.compile(mail, callback, this.mailerOptions);
19
18
  });
19
+ let previewEmail;
20
+ try {
21
+ previewEmail = require('preview-email');
22
+ }
23
+ catch (err) {
24
+ this.mailerLogger.warn('preview-email is not installed. This is an optional dependency. Install it if you want to preview emails in the development environment. You can install it using npm (npm install preview-email), yarn (yarn add preview-email), or pnpm (pnpm add preview-email).');
25
+ }
20
26
  if (this.mailerOptions.preview) {
21
27
  transporter.use('stream', (mail, callback) => {
22
- return previewEmail(mail.data, this.mailerOptions.preview)
23
- .then(() => callback())
24
- .catch(callback);
28
+ if (typeof previewEmail !== 'undefined') {
29
+ return previewEmail(mail.data, this.mailerOptions.preview)
30
+ .then(() => callback())
31
+ .catch(callback);
32
+ }
33
+ else {
34
+ this.mailerLogger.warn('previewEmail is not available. Skipping preview.');
35
+ return callback();
36
+ }
25
37
  });
26
38
  }
27
39
  }
@@ -34,11 +46,7 @@ let MailerService = MailerService_1 = class MailerService {
34
46
  if (!transportFactory) {
35
47
  this.transportFactory = new mailer_transport_factory_1.MailerTransportFactory(mailerOptions);
36
48
  }
37
- if ((!mailerOptions.transport ||
38
- Object.keys(mailerOptions.transport).length <= 0) &&
39
- !mailerOptions.transports) {
40
- throw new Error('Make sure to provide a nodemailer transport configuration object, connection url or a transport plugin instance.');
41
- }
49
+ this.validateTransportOptions();
42
50
  this.templateAdapter = (0, lodash_1.get)(this.mailerOptions, 'template.adapter');
43
51
  if (this.mailerOptions.preview) {
44
52
  const defaults = { open: { wait: false } };
@@ -47,25 +55,51 @@ let MailerService = MailerService_1 = class MailerService {
47
55
  ? defaults
48
56
  : (0, lodash_1.defaultsDeep)(this.mailerOptions.preview, defaults);
49
57
  }
50
- if (mailerOptions.transports) {
51
- Object.keys(mailerOptions.transports).forEach((name) => {
52
- const transporter = this.transportFactory.createTransport(this.mailerOptions.transports[name]);
58
+ this.setupTransporters();
59
+ }
60
+ validateTransportOptions() {
61
+ if ((!this.mailerOptions.transport ||
62
+ Object.keys(this.mailerOptions.transport).length <= 0) &&
63
+ !this.mailerOptions.transports) {
64
+ throw new Error('Make sure to provide a nodemailer transport configuration object, connection url or a transport plugin instance.');
65
+ }
66
+ }
67
+ createTransporter(config, name) {
68
+ const transporter = this.transportFactory.createTransport(config);
69
+ if (this.mailerOptions.verifyTransporters)
70
+ this.verifyTransporter(transporter, name);
71
+ this.initTemplateAdapter(this.templateAdapter, transporter);
72
+ return transporter;
73
+ }
74
+ setupTransporters() {
75
+ if (this.mailerOptions.transports) {
76
+ Object.keys(this.mailerOptions.transports).forEach((name) => {
77
+ const transporter = this.createTransporter(this.mailerOptions.transports[name], name);
53
78
  this.transporters.set(name, transporter);
54
- this.verifyTransporter(transporter, name);
55
- this.initTemplateAdapter(this.templateAdapter, transporter);
56
79
  });
57
80
  }
58
- if (mailerOptions.transport) {
59
- this.transporter = this.transportFactory.createTransport();
60
- this.verifyTransporter(this.transporter);
61
- this.initTemplateAdapter(this.templateAdapter, this.transporter);
81
+ if (this.mailerOptions.transport) {
82
+ this.transporter = this.createTransporter(this.mailerOptions.transport);
62
83
  }
63
84
  }
64
85
  verifyTransporter(transporter, name) {
65
86
  const transporterName = name ? ` '${name}'` : '';
66
- transporter.verify()
67
- .then(() => this.mailerLogger.error(`Transporter${transporterName} is ready`))
68
- .catch((error) => this.mailerLogger.error(`Error occurred while verifying the transporter${transporterName}}: ${error.message}`));
87
+ if (!transporter.verify)
88
+ return;
89
+ Promise.resolve(transporter.verify())
90
+ .then(() => this.mailerLogger.debug(`Transporter${transporterName} is ready`))
91
+ .catch((error) => this.mailerLogger.error(`Error occurred while verifying the transporter${transporterName}: ${error.message}`));
92
+ }
93
+ verifyAllTransporters() {
94
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
95
+ const transporters = [...this.transporters.values(), this.transporter];
96
+ const transportersVerified = yield Promise.all(transporters.map(transporter => {
97
+ if (!transporter.verify)
98
+ return true;
99
+ return Promise.resolve(transporter.verify()).then(() => true).catch(() => false);
100
+ }));
101
+ return transportersVerified.every(verified => verified);
102
+ });
69
103
  }
70
104
  sendMail(sendMailOptions) {
71
105
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs-modules/mailer",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "private": false,
5
5
  "description": "NestJS - a mailer module (@mailer)",
6
6
  "keywords": [