@tomei/mailer 0.4.0 → 0.5.2

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.
Files changed (67) hide show
  1. package/Jenkinsfile +6 -1
  2. package/__tests__/unit/mailer/mailer.spec.ts +182 -43
  3. package/dist/__tests__/unit/mailer/mailer.spec.js +162 -45
  4. package/dist/__tests__/unit/mailer/mailer.spec.js.map +1 -1
  5. package/dist/index.d.ts +0 -2
  6. package/dist/index.js +0 -3
  7. package/dist/index.js.map +1 -1
  8. package/dist/src/enum/email-status.enum.d.ts +4 -0
  9. package/dist/src/enum/email-status.enum.js +9 -0
  10. package/dist/src/enum/email-status.enum.js.map +1 -0
  11. package/dist/src/enum/index.d.ts +1 -0
  12. package/dist/src/enum/index.js +6 -0
  13. package/dist/src/enum/index.js.map +1 -0
  14. package/dist/src/index.d.ts +3 -3
  15. package/dist/src/index.js +17 -4
  16. package/dist/src/index.js.map +1 -1
  17. package/dist/src/interfaces/index.d.ts +3 -2
  18. package/dist/src/{mailer/dto/logOptions.d.ts → interfaces/log-transaction-options.interface.d.ts} +2 -2
  19. package/dist/src/interfaces/{mail-configuration.interface.js → log-transaction-options.interface.js} +1 -1
  20. package/dist/src/interfaces/log-transaction-options.interface.js.map +1 -0
  21. package/dist/src/interfaces/mail-config.interface.d.ts +8 -0
  22. package/dist/{interfaces/mail-configuration.interface.js → src/interfaces/mail-config.interface.js} +1 -1
  23. package/dist/src/interfaces/mail-config.interface.js.map +1 -0
  24. package/dist/src/interfaces/mail-log.interface.d.ts +11 -0
  25. package/dist/src/{mailer/dto/logOptions.js → interfaces/mail-log.interface.js} +1 -1
  26. package/dist/src/interfaces/mail-log.interface.js.map +1 -0
  27. package/dist/src/mailer/index.d.ts +2 -1
  28. package/dist/src/mailer/index.js +5 -15
  29. package/dist/src/mailer/index.js.map +1 -1
  30. package/dist/src/mailer/mailer.base.d.ts +9 -0
  31. package/dist/src/mailer/mailer.base.js +113 -0
  32. package/dist/src/mailer/mailer.base.js.map +1 -0
  33. package/dist/src/mailer/smtp-mailer.d.ts +7 -0
  34. package/dist/src/mailer/smtp-mailer.js +53 -0
  35. package/dist/src/mailer/smtp-mailer.js.map +1 -0
  36. package/dist/tsconfig.tsbuildinfo +1 -1
  37. package/index.ts +1 -0
  38. package/package.json +9 -7
  39. package/src/enum/email-status.enum.ts +4 -0
  40. package/src/enum/index.ts +1 -0
  41. package/src/index.ts +3 -4
  42. package/src/interfaces/index.ts +3 -3
  43. package/src/{mailer/dto/logOptions.ts → interfaces/log-transaction-options.interface.ts} +18 -18
  44. package/src/interfaces/mail-config.interface.ts +8 -0
  45. package/src/interfaces/mail-log.interface.ts +12 -0
  46. package/src/mailer/index.ts +2 -1
  47. package/src/mailer/mailer.base.ts +110 -0
  48. package/src/mailer/smtp-mailer.ts +57 -0
  49. package/dist/interfaces/index.d.ts +0 -1
  50. package/dist/interfaces/index.js +0 -18
  51. package/dist/interfaces/index.js.map +0 -1
  52. package/dist/interfaces/mail-configuration.interface.d.ts +0 -9
  53. package/dist/interfaces/mail-configuration.interface.js.map +0 -1
  54. package/dist/mailer/index.d.ts +0 -1
  55. package/dist/mailer/index.js +0 -18
  56. package/dist/mailer/index.js.map +0 -1
  57. package/dist/mailer/mailer.d.ts +0 -8
  58. package/dist/mailer/mailer.js +0 -55
  59. package/dist/mailer/mailer.js.map +0 -1
  60. package/dist/src/interfaces/mail-configuration.interface.d.ts +0 -9
  61. package/dist/src/interfaces/mail-configuration.interface.js.map +0 -1
  62. package/dist/src/mailer/dto/logOptions.js.map +0 -1
  63. package/dist/src/mailer/mailer.d.ts +0 -11
  64. package/dist/src/mailer/mailer.js +0 -99
  65. package/dist/src/mailer/mailer.js.map +0 -1
  66. package/src/interfaces/mail-configuration.interface.ts +0 -9
  67. package/src/mailer/mailer.ts +0 -105
@@ -0,0 +1,110 @@
1
+ import { IMailLog } from 'src/interfaces/mail-log.interface';
2
+ import { ILogTransactionOption } from '../interfaces/log-transaction-options.interface';
3
+ import { ISendMailConfig } from '../interfaces/mail-config.interface';
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
+ import { EmailStatus } from '../enum/email-status.enum';
7
+ import { ComponentConfig } from '@tomei/config';
8
+
9
+ export abstract class MailerBase {
10
+ abstract sendMail(options: any): Promise<any>;
11
+
12
+ async send(options: ISendMailConfig): Promise<any> {
13
+ // start timer
14
+ const start = Date.now();
15
+ let status: EmailStatus;
16
+
17
+ // send email
18
+ try {
19
+ await this.sendMail(options);
20
+ status = EmailStatus.Sent;
21
+ } catch (error) {
22
+ status = EmailStatus.Failed;
23
+ } finally {
24
+ const duration = Date.now() - start;
25
+ const isLogEnabled = ComponentConfig.getComponentConfigValue(
26
+ '@tomei/mailer',
27
+ 'isLogEnabled',
28
+ );
29
+ if (isLogEnabled) {
30
+ MailerBase.log({
31
+ duration,
32
+ from: options.from,
33
+ to:
34
+ typeof options.to === 'string' ? options.to : options.to.join(','),
35
+ cc:
36
+ typeof options.cc === 'string'
37
+ ? options.cc
38
+ : options.cc
39
+ ? options.cc.join(',')
40
+ : '',
41
+ subject: options.subject,
42
+ rawContent: options.html,
43
+ date: new Date(),
44
+ status: status,
45
+ });
46
+ }
47
+ }
48
+ }
49
+
50
+ async logTransaction(logOptions: ILogTransactionOption): Promise<void> {
51
+ const startTime = new Date().getTime();
52
+ let endTime: number;
53
+ const options: any = {
54
+ from: logOptions.options.from,
55
+ to: logOptions.options.to,
56
+ subject: logOptions.options.subject,
57
+ html: `<div>${JSON.stringify(logOptions.logData)}</div>`,
58
+ cc: '',
59
+ };
60
+ let result: EmailStatus;
61
+
62
+ try {
63
+ await this.sendMail(options);
64
+ result = EmailStatus.Sent;
65
+ } catch (error) {
66
+ //Retry once
67
+ try {
68
+ console.info('Retry to send mail');
69
+ await this.sendMail(options);
70
+ } catch (retryError) {
71
+ result = EmailStatus.Failed;
72
+ console.error(retryError);
73
+ }
74
+ } finally {
75
+ endTime = new Date().getTime();
76
+ const duration = endTime - startTime;
77
+ const mailLogOptions: IMailLog = {
78
+ duration,
79
+ from: options.from,
80
+ to: options.to,
81
+ cc: '',
82
+ subject: options.subject,
83
+ rawContent: options.html,
84
+ date: new Date(),
85
+ status: result,
86
+ };
87
+ MailerBase.log(mailLogOptions);
88
+ }
89
+ }
90
+
91
+ static log(mailLogOptions: IMailLog): void {
92
+ try {
93
+ const logFolderPath = path.join(process.cwd(), 'mail-log');
94
+
95
+ if (!fs.existsSync(logFolderPath)) {
96
+ fs.mkdirSync(logFolderPath);
97
+ }
98
+
99
+ const currentDate = new Date();
100
+ const formattedDate = currentDate.toISOString().slice(0, 10);
101
+ const logFileName = `${formattedDate}.log`;
102
+ const logFilePath = path.join(logFolderPath, logFileName);
103
+ const stringData = JSON.stringify(mailLogOptions);
104
+
105
+ fs.appendFileSync(logFilePath, '\n' + stringData);
106
+ } catch (error) {
107
+ throw new Error('Error occurred while logging mail');
108
+ }
109
+ }
110
+ }
@@ -0,0 +1,57 @@
1
+ import { ComponentConfig } from '@tomei/config';
2
+ import { MailerBase } from './mailer.base';
3
+ import * as nodemailer from 'nodemailer';
4
+ import { ISendMailConfig } from 'src/interfaces/mail-config.interface';
5
+
6
+ export class SMTPMailer extends MailerBase {
7
+ async sendMail(options: ISendMailConfig): Promise<void> {
8
+ try {
9
+ const host = ComponentConfig.getComponentConfigValue(
10
+ '@tomei/mailer',
11
+ 'host',
12
+ );
13
+ const port = ComponentConfig.getComponentConfigValue(
14
+ '@tomei/mailer',
15
+ 'port',
16
+ );
17
+ const secure = ComponentConfig.getComponentConfigValue(
18
+ '@tomei/mailer',
19
+ 'secure',
20
+ );
21
+ const user = ComponentConfig.getComponentConfigValue(
22
+ '@tomei/mailer',
23
+ 'user',
24
+ );
25
+ const pass = ComponentConfig.getComponentConfigValue(
26
+ '@tomei/mailer',
27
+ 'pass',
28
+ );
29
+
30
+ const transportConfig = {
31
+ host,
32
+ port,
33
+ secure,
34
+ auth: {
35
+ user,
36
+ pass,
37
+ },
38
+ };
39
+
40
+ const transporter = nodemailer.createTransport(transportConfig);
41
+
42
+ await transporter.sendMail({
43
+ from: options.from,
44
+ to: options.to,
45
+ subject: options.subject,
46
+ html: options.html,
47
+ cc: options.cc,
48
+ attachments: options.attachments,
49
+ });
50
+ } catch (error) {
51
+ console.log(error);
52
+ throw error;
53
+ }
54
+ }
55
+ }
56
+
57
+ export { nodemailer };
@@ -1 +0,0 @@
1
- export * from './mail-configuration.interface';
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./mail-configuration.interface"), exports);
18
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iEAA8C"}
@@ -1,9 +0,0 @@
1
- export interface MailConfig {
2
- host: string;
3
- port: number;
4
- secure: boolean;
5
- auth?: {
6
- user: string;
7
- pass: string;
8
- };
9
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"mail-configuration.interface.js","sourceRoot":"","sources":["../../src/interfaces/mail-configuration.interface.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- export * from './mailer';
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./mailer"), exports);
18
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/mailer/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB"}
@@ -1,8 +0,0 @@
1
- import { MailConfig } from "../interfaces";
2
- export default class Mailer {
3
- private mailer;
4
- constructor(mailer: any, config: MailConfig);
5
- private configureMailer;
6
- sendMail(from: string, to: string, subject: string, text: string): Promise<void>;
7
- private logError;
8
- }
@@ -1,55 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- class Mailer {
13
- constructor(mailer, config) {
14
- this.mailer = mailer;
15
- this.configureMailer(config);
16
- }
17
- configureMailer(config) {
18
- this.mailer.configure({
19
- host: config.host,
20
- port: config.port,
21
- secure: config.secure,
22
- auth: config.auth,
23
- });
24
- }
25
- sendMail(from, to, subject, text) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- try {
28
- yield this.mailer.sendMail({
29
- from,
30
- to,
31
- subject,
32
- text,
33
- });
34
- }
35
- catch (error) {
36
- try {
37
- yield this.mailer.sendMail({
38
- from,
39
- to,
40
- subject,
41
- text,
42
- });
43
- }
44
- catch (retryError) {
45
- this.logError(retryError);
46
- }
47
- }
48
- });
49
- }
50
- logError(error) {
51
- console.error('Error occurred while sending mail:', error);
52
- }
53
- }
54
- exports.default = Mailer;
55
- //# sourceMappingURL=mailer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mailer.js","sourceRoot":"","sources":["../../src/mailer/mailer.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA,MAAqB,MAAM;IAGzB,YAAY,MAAW,EAAE,MAAiB;QACxC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,CAAA;IAC9B,CAAC;IAEO,eAAe,CAAC,MAAkB;QASxC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;YACpB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,IAAI,EAAE,MAAM,CAAC,IAAI;SAClB,CAAC,CAAC;IACL,CAAC;IACK,QAAQ,CACZ,IAAY,EACZ,EAAU,EACV,OAAe,EACf,IAAY;;YAEZ,IAAI;gBACF,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC;oBACzB,IAAI;oBACJ,EAAE;oBACF,OAAO;oBACP,IAAI;iBACL,CAAC,CAAC;aACJ;YAAC,OAAO,KAAK,EAAE;gBAEd,IAAI;oBACF,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC;wBACzB,IAAI;wBACJ,EAAE;wBACF,OAAO;wBACP,IAAI;qBACL,CAAC,CAAC;iBACJ;gBAAC,OAAO,UAAU,EAAE;oBACnB,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;iBAC3B;aACF;QACH,CAAC;KAAA;IAEO,QAAQ,CAAC,KAAY;QAG3B,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;IAC7D,CAAC;CAEF;AA1DD,yBA0DC"}
@@ -1,9 +0,0 @@
1
- export interface MailConfig {
2
- host: string;
3
- port: number;
4
- secure: boolean;
5
- auth?: {
6
- user: string;
7
- pass: string;
8
- };
9
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"mail-configuration.interface.js","sourceRoot":"","sources":["../../../src/interfaces/mail-configuration.interface.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"logOptions.js","sourceRoot":"","sources":["../../../../src/mailer/dto/logOptions.ts"],"names":[],"mappings":""}
@@ -1,11 +0,0 @@
1
- import { MailConfig } from '../interfaces';
2
- import { logOptions } from './dto/logOptions';
3
- export default class Mailer {
4
- private mailer;
5
- constructor(mailer: any, config: MailConfig);
6
- private configureMailer;
7
- sendMail(options: any): Promise<void>;
8
- verify(cb: Function): void;
9
- logTransaction(logOptions: logOptions): Promise<void>;
10
- private logError;
11
- }
@@ -1,99 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const fs = require("fs");
13
- const path = require("path");
14
- class Mailer {
15
- constructor(mailer, config) {
16
- this.mailer = mailer;
17
- this.configureMailer(config);
18
- }
19
- configureMailer(config) {
20
- if (typeof this.mailer.createTransport === 'function') {
21
- console.log('inside #1');
22
- this.mailer = this.mailer.createTransport({
23
- host: config.host,
24
- port: config.port,
25
- secure: config.secure,
26
- auth: config.auth,
27
- tls: {
28
- rejectUnauthorized: true,
29
- minVersion: 'TLSv1.2',
30
- },
31
- });
32
- }
33
- else if (typeof this.mailer.transporter === 'object') {
34
- console.log('inside #2');
35
- }
36
- else if (typeof this.mailer.configure === 'function') {
37
- console.log('inside #3');
38
- this.mailer.configure(config);
39
- }
40
- else {
41
- throw new Error('Invalid mailer library. Unable to configure the mailer.');
42
- }
43
- }
44
- sendMail(options) {
45
- return __awaiter(this, void 0, void 0, function* () {
46
- try {
47
- yield this.mailer.sendMail(options);
48
- }
49
- catch (error) {
50
- try {
51
- console.info('Retry to send mail');
52
- yield this.mailer.sendMail(options);
53
- }
54
- catch (retryError) {
55
- this.logError(retryError);
56
- }
57
- }
58
- });
59
- }
60
- verify(cb) {
61
- this.mailer.verify(cb);
62
- }
63
- logTransaction(logOptions) {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- const options = {
66
- from: logOptions.options.from,
67
- to: logOptions.options.to,
68
- subject: logOptions.options.subject,
69
- html: `<div>${JSON.stringify(logOptions.logData)}</div>`,
70
- };
71
- try {
72
- yield this.mailer.sendMail(options);
73
- }
74
- catch (error) {
75
- try {
76
- console.info('Retry to send mail');
77
- yield this.mailer.sendMail(options);
78
- }
79
- catch (retryError) {
80
- this.logError(retryError);
81
- }
82
- }
83
- });
84
- }
85
- logError(error) {
86
- const logFolderPath = path.join(process.cwd(), 'mailerLog');
87
- const currentDate = new Date();
88
- const formattedDate = currentDate.toISOString().slice(0, 10);
89
- const logFileName = `${formattedDate}.log`;
90
- const logFilePath = path.join(logFolderPath, logFileName);
91
- if (!fs.existsSync(logFolderPath)) {
92
- fs.mkdirSync(logFolderPath);
93
- }
94
- fs.appendFileSync(logFilePath, `Error occurred while sending mail, reason: "${error}"\n`);
95
- throw error;
96
- }
97
- }
98
- exports.default = Mailer;
99
- //# sourceMappingURL=mailer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mailer.js","sourceRoot":"","sources":["../../../src/mailer/mailer.ts"],"names":[],"mappings":"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"}
@@ -1,9 +0,0 @@
1
- export interface MailConfig {
2
- host: string;
3
- port: number;
4
- secure: boolean;
5
- auth?: {
6
- user: string;
7
- pass: string;
8
- };
9
- }
@@ -1,105 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- import { MailConfig } from '../interfaces';
4
- import { logOptions } from './dto/logOptions';
5
- import { generateLogTransactionHTML } from './helpers/helpers';
6
-
7
- export default class Mailer {
8
- private mailer: any;
9
-
10
- constructor(mailer: any, config: MailConfig) {
11
- this.mailer = mailer;
12
- this.configureMailer(config);
13
- }
14
-
15
- private configureMailer(config: MailConfig): void {
16
- // Setting up the configuration options for the mailer instance
17
- // based on the provided configuration object
18
-
19
- if (typeof this.mailer.createTransport === 'function') {
20
- console.log('inside #1')
21
- /* nodemailer configuration */
22
- this.mailer = this.mailer.createTransport({
23
- host: config.host,
24
- port: config.port,
25
- secure: config.secure,
26
- auth: config.auth,
27
- tls: {
28
- rejectUnauthorized: true,
29
- minVersion: 'TLSv1.2',
30
- },
31
- });
32
- } else if (typeof this.mailer.transporter === 'object') {
33
- console.log('inside #2')
34
- /* @nestjs/mailer configuration*/
35
- //do nothing
36
- } else if (typeof this.mailer.configure === 'function') {
37
- console.log('inside #3')
38
- this.mailer.configure(config);
39
- } else {
40
- throw new Error(
41
- 'Invalid mailer library. Unable to configure the mailer.',
42
- );
43
- }
44
- }
45
-
46
- async sendMail(options: any): Promise<void> {
47
- try {
48
- await this.mailer.sendMail(options);
49
- } catch (error) {
50
- //Retry once
51
- try {
52
- console.info('Retry to send mail');
53
- await this.mailer.sendMail(options);
54
- } catch (retryError) {
55
- this.logError(retryError);
56
- }
57
- }
58
- }
59
-
60
- public verify(cb: Function): void {
61
- this.mailer.verify(cb);
62
- }
63
-
64
- async logTransaction(logOptions: logOptions): Promise<void> {
65
- const options: any = {
66
- from: logOptions.options.from,
67
- to: logOptions.options.to,
68
- subject: logOptions.options.subject,
69
- html: `<div>${JSON.stringify(logOptions.logData)}</div>`,
70
- };
71
-
72
- try {
73
- await this.mailer.sendMail(options);
74
- } catch (error) {
75
- //Retry once
76
- try {
77
- console.info('Retry to send mail');
78
- await this.mailer.sendMail(options);
79
- } catch (retryError) {
80
- this.logError(retryError);
81
- }
82
- }
83
- }
84
-
85
- private logError(error: Error): void {
86
- const logFolderPath = path.join(process.cwd(), 'mailerLog');
87
-
88
- const currentDate = new Date();
89
- const formattedDate = currentDate.toISOString().slice(0, 10);
90
- const logFileName = `${formattedDate}.log`;
91
-
92
- const logFilePath = path.join(logFolderPath, logFileName);
93
-
94
- if (!fs.existsSync(logFolderPath)) {
95
- fs.mkdirSync(logFolderPath);
96
- }
97
-
98
- fs.appendFileSync(
99
- logFilePath,
100
- `Error occurred while sending mail, reason: "${error}"\n`,
101
- );
102
-
103
- throw error;
104
- }
105
- }