@tomei/mailer 0.5.21 → 0.6.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.
- package/.gitlab-ci.yml +16 -16
- package/.husky/commit-msg +15 -15
- package/.husky/pre-commit +8 -5
- package/__tests__/unit/mailer/mailer.spec.ts +2 -2
- package/dist/__tests__/unit/mailer/mailer.spec.js +2 -2
- package/dist/__tests__/unit/mailer/mailer.spec.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eslint.config.mjs +58 -58
- package/package.json +47 -47
- package/sonar-project.properties +12 -12
- package/src/enum/email-status.enum.ts +4 -4
- package/src/enum/index.ts +1 -1
- package/src/interfaces/log-transaction-options.interface.ts +18 -18
- package/src/interfaces/mail-config.interface.ts +8 -8
- package/src/interfaces/mail-log.interface.ts +12 -12
- package/src/mailer/index.ts +2 -2
- package/src/mailer/mailer.base.ts +110 -110
- package/src/mailer/smtp-mailer.ts +94 -94
package/eslint.config.mjs
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import eslintPlugin from "@typescript-eslint/eslint-plugin";
|
|
2
|
-
import parser from "@typescript-eslint/parser";
|
|
3
|
-
import importPlugin from 'eslint-plugin-import'
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import { fileURLToPath } from 'url';
|
|
6
|
-
|
|
7
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
-
const __dirname = path.dirname(__filename);
|
|
9
|
-
|
|
10
|
-
export default [
|
|
11
|
-
{
|
|
12
|
-
languageOptions: {
|
|
13
|
-
parser: parser,
|
|
14
|
-
ecmaVersion: "latest", // Allows modern ECMAScript features
|
|
15
|
-
sourceType: "module", // Allows for the use of imports
|
|
16
|
-
parserOptions: {
|
|
17
|
-
tsconfigRootDir: __dirname,
|
|
18
|
-
project: './tsconfig.json'
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
plugins: {
|
|
22
|
-
"@typescript-eslint": eslintPlugin,
|
|
23
|
-
import: importPlugin
|
|
24
|
-
},
|
|
25
|
-
rules: {
|
|
26
|
-
"no-console": "off",
|
|
27
|
-
"@typescript-eslint/no-explicit-any": "off",
|
|
28
|
-
"@typescript-eslint/no-var-requires": "off",
|
|
29
|
-
"@typescript-eslint/explicit-module-boundary-types": "off",
|
|
30
|
-
"import/prefer-default-export": "off",
|
|
31
|
-
"@typescript-eslint/no-unused-vars": "warn",
|
|
32
|
-
"no-useless-catch": "off",
|
|
33
|
-
"@typescript-eslint/no-non-null-assertion": "off",
|
|
34
|
-
"@typescript-eslint/no-empty-function": "off",
|
|
35
|
-
"@typescript-eslint/no-empty-interface": "off",
|
|
36
|
-
"@typescript-eslint/no-inferrable-types": "off",
|
|
37
|
-
"@typescript-eslint/no-namespace": "off",
|
|
38
|
-
"@typescript-eslint/no-use-before-define": "off",
|
|
39
|
-
"@typescript-eslint/no-unsafe-assignment": "off",
|
|
40
|
-
"@typescript-eslint/no-unsafe-call": "off",
|
|
41
|
-
"@typescript-eslint/no-unsafe-member-access": "off",
|
|
42
|
-
"@typescript-eslint/no-unsafe-return": "off",
|
|
43
|
-
"@typescript-eslint/restrict-template-expressions": "off",
|
|
44
|
-
"no-useless-escape": "off",
|
|
45
|
-
"import/no-relative-parent-imports": "error", // Enforce relative imports only
|
|
46
|
-
"import/no-absolute-path": "error", // Block absolute imports outside aliases
|
|
47
|
-
"import/no-unresolved": ["error", { commonjs: true }],
|
|
48
|
-
},
|
|
49
|
-
settings: {
|
|
50
|
-
"import/resolver": {
|
|
51
|
-
typescript: {
|
|
52
|
-
alwaysTryTypes: true,
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
ignores: ["node_modules", "dist/**/*", "eslint.config.mjs"],
|
|
57
|
-
},
|
|
58
|
-
];
|
|
1
|
+
import eslintPlugin from "@typescript-eslint/eslint-plugin";
|
|
2
|
+
import parser from "@typescript-eslint/parser";
|
|
3
|
+
import importPlugin from 'eslint-plugin-import'
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
|
|
10
|
+
export default [
|
|
11
|
+
{
|
|
12
|
+
languageOptions: {
|
|
13
|
+
parser: parser,
|
|
14
|
+
ecmaVersion: "latest", // Allows modern ECMAScript features
|
|
15
|
+
sourceType: "module", // Allows for the use of imports
|
|
16
|
+
parserOptions: {
|
|
17
|
+
tsconfigRootDir: __dirname,
|
|
18
|
+
project: './tsconfig.json'
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
plugins: {
|
|
22
|
+
"@typescript-eslint": eslintPlugin,
|
|
23
|
+
import: importPlugin
|
|
24
|
+
},
|
|
25
|
+
rules: {
|
|
26
|
+
"no-console": "off",
|
|
27
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
28
|
+
"@typescript-eslint/no-var-requires": "off",
|
|
29
|
+
"@typescript-eslint/explicit-module-boundary-types": "off",
|
|
30
|
+
"import/prefer-default-export": "off",
|
|
31
|
+
"@typescript-eslint/no-unused-vars": "warn",
|
|
32
|
+
"no-useless-catch": "off",
|
|
33
|
+
"@typescript-eslint/no-non-null-assertion": "off",
|
|
34
|
+
"@typescript-eslint/no-empty-function": "off",
|
|
35
|
+
"@typescript-eslint/no-empty-interface": "off",
|
|
36
|
+
"@typescript-eslint/no-inferrable-types": "off",
|
|
37
|
+
"@typescript-eslint/no-namespace": "off",
|
|
38
|
+
"@typescript-eslint/no-use-before-define": "off",
|
|
39
|
+
"@typescript-eslint/no-unsafe-assignment": "off",
|
|
40
|
+
"@typescript-eslint/no-unsafe-call": "off",
|
|
41
|
+
"@typescript-eslint/no-unsafe-member-access": "off",
|
|
42
|
+
"@typescript-eslint/no-unsafe-return": "off",
|
|
43
|
+
"@typescript-eslint/restrict-template-expressions": "off",
|
|
44
|
+
"no-useless-escape": "off",
|
|
45
|
+
"import/no-relative-parent-imports": "error", // Enforce relative imports only
|
|
46
|
+
"import/no-absolute-path": "error", // Block absolute imports outside aliases
|
|
47
|
+
"import/no-unresolved": ["error", { commonjs: true }],
|
|
48
|
+
},
|
|
49
|
+
settings: {
|
|
50
|
+
"import/resolver": {
|
|
51
|
+
typescript: {
|
|
52
|
+
alwaysTryTypes: true,
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
ignores: ["node_modules", "dist/**/*", "eslint.config.mjs"],
|
|
57
|
+
},
|
|
58
|
+
];
|
package/package.json
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@tomei/mailer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Tomei Mailer Package",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"start:dev": "tsc -w",
|
|
8
|
-
"build": "
|
|
9
|
-
"format": "prettier --write \"src/**/*.ts\"",
|
|
10
|
-
"lint": "npx eslint . --fix",
|
|
11
|
-
"test": "jest --forceExit --detectOpenHandles"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"@commitlint/
|
|
18
|
-
"@
|
|
19
|
-
"@
|
|
20
|
-
"@
|
|
21
|
-
"@types/
|
|
22
|
-
"@types/
|
|
23
|
-
"@
|
|
24
|
-
"@typescript-eslint/
|
|
25
|
-
"eslint": "^
|
|
26
|
-
"eslint
|
|
27
|
-
"eslint-
|
|
28
|
-
"eslint-plugin-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"ts-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"typescript
|
|
39
|
-
},
|
|
40
|
-
"publishConfig": {
|
|
41
|
-
"access": "public"
|
|
42
|
-
},
|
|
43
|
-
"peerDependencies": {
|
|
44
|
-
"@tomei/config": "^0.3.
|
|
45
|
-
"nodemailer": "^
|
|
46
|
-
}
|
|
47
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@tomei/mailer",
|
|
3
|
+
"version": "0.6.2",
|
|
4
|
+
"description": "Tomei Mailer Package",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start:dev": "tsc -w",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
10
|
+
"lint": "npx eslint . --fix",
|
|
11
|
+
"test": "jest --forceExit --detectOpenHandles",
|
|
12
|
+
"prepare": "husky"
|
|
13
|
+
},
|
|
14
|
+
"author": "Tomei",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@commitlint/cli": "^19.8.1",
|
|
18
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
19
|
+
"@eslint/js": "^9.35.0",
|
|
20
|
+
"@tsconfig/node18": "^18.2.4",
|
|
21
|
+
"@types/jest": "^30.0.0",
|
|
22
|
+
"@types/node": "^24.5.2",
|
|
23
|
+
"@types/nodemailer": "^7.0.1",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^8.44.0",
|
|
25
|
+
"@typescript-eslint/parser": "^8.44.0",
|
|
26
|
+
"eslint": "^9.35.0",
|
|
27
|
+
"eslint-config-prettier": "^10.1.8",
|
|
28
|
+
"eslint-plugin-import": "^2.32.0",
|
|
29
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
30
|
+
"globals": "^16.4.0",
|
|
31
|
+
"husky": "^9.1.7",
|
|
32
|
+
"jest": "^30.1.3",
|
|
33
|
+
"nodemailer-mock": "^2.0.9",
|
|
34
|
+
"prettier": "^3.6.2",
|
|
35
|
+
"ts-jest": "^29.4.3",
|
|
36
|
+
"ts-node": "^10.9.2",
|
|
37
|
+
"tsc-watch": "^7.1.1",
|
|
38
|
+
"typescript": "^5.9.2"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@tomei/config": "^0.3.22",
|
|
45
|
+
"nodemailer": "^7.0.6"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/sonar-project.properties
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
sonar.projectKey=all-tomei-projects_mailer
|
|
2
|
-
sonar.organization=all-tomei-projects
|
|
3
|
-
|
|
4
|
-
# This is the name and version displayed in the SonarCloud UI.
|
|
5
|
-
#sonar.projectName=Mailer
|
|
6
|
-
#sonar.projectVersion=1.0
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
|
|
10
|
-
#sonar.sources=.
|
|
11
|
-
|
|
12
|
-
# Encoding of the source code. Default is default system encoding
|
|
1
|
+
sonar.projectKey=all-tomei-projects_mailer
|
|
2
|
+
sonar.organization=all-tomei-projects
|
|
3
|
+
|
|
4
|
+
# This is the name and version displayed in the SonarCloud UI.
|
|
5
|
+
#sonar.projectName=Mailer
|
|
6
|
+
#sonar.projectVersion=1.0
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
|
|
10
|
+
#sonar.sources=.
|
|
11
|
+
|
|
12
|
+
# Encoding of the source code. Default is default system encoding
|
|
13
13
|
#sonar.sourceEncoding=UTF-8
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export enum EmailStatus {
|
|
2
|
-
Sent = 'Sent',
|
|
3
|
-
Failed = 'Failed',
|
|
4
|
-
}
|
|
1
|
+
export enum EmailStatus {
|
|
2
|
+
Sent = 'Sent',
|
|
3
|
+
Failed = 'Failed',
|
|
4
|
+
}
|
package/src/enum/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { EmailStatus } from './email-status.enum';
|
|
1
|
+
export { EmailStatus } from './email-status.enum';
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
export interface ILogTransactionOption {
|
|
2
|
-
options: {
|
|
3
|
-
from: string;
|
|
4
|
-
to: string;
|
|
5
|
-
subject: string;
|
|
6
|
-
};
|
|
7
|
-
logData: {
|
|
8
|
-
systemCode: string;
|
|
9
|
-
apiUrl: string;
|
|
10
|
-
transactionName: string;
|
|
11
|
-
dbName: string;
|
|
12
|
-
tableName: string;
|
|
13
|
-
dataBefore: string;
|
|
14
|
-
dataAfter: string;
|
|
15
|
-
performAt: Date;
|
|
16
|
-
note: string;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
1
|
+
export interface ILogTransactionOption {
|
|
2
|
+
options: {
|
|
3
|
+
from: string;
|
|
4
|
+
to: string;
|
|
5
|
+
subject: string;
|
|
6
|
+
};
|
|
7
|
+
logData: {
|
|
8
|
+
systemCode: string;
|
|
9
|
+
apiUrl: string;
|
|
10
|
+
transactionName: string;
|
|
11
|
+
dbName: string;
|
|
12
|
+
tableName: string;
|
|
13
|
+
dataBefore: string;
|
|
14
|
+
dataAfter: string;
|
|
15
|
+
performAt: Date;
|
|
16
|
+
note: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export interface ISendMailConfig {
|
|
2
|
-
from: string;
|
|
3
|
-
to: string | string[];
|
|
4
|
-
cc?: string | string[];
|
|
5
|
-
attachments?: any[];
|
|
6
|
-
subject: string;
|
|
7
|
-
html: any;
|
|
8
|
-
}
|
|
1
|
+
export interface ISendMailConfig {
|
|
2
|
+
from: string;
|
|
3
|
+
to: string | string[];
|
|
4
|
+
cc?: string | string[];
|
|
5
|
+
attachments?: any[];
|
|
6
|
+
subject: string;
|
|
7
|
+
html: any;
|
|
8
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { EmailStatus } from '../enum/email-status.enum';
|
|
2
|
-
|
|
3
|
-
export interface IMailLog {
|
|
4
|
-
duration: number;
|
|
5
|
-
from: string;
|
|
6
|
-
to: string;
|
|
7
|
-
cc: string;
|
|
8
|
-
subject: string;
|
|
9
|
-
rawContent: string;
|
|
10
|
-
date: Date;
|
|
11
|
-
status: EmailStatus;
|
|
12
|
-
}
|
|
1
|
+
import { EmailStatus } from '../enum/email-status.enum';
|
|
2
|
+
|
|
3
|
+
export interface IMailLog {
|
|
4
|
+
duration: number;
|
|
5
|
+
from: string;
|
|
6
|
+
to: string;
|
|
7
|
+
cc: string;
|
|
8
|
+
subject: string;
|
|
9
|
+
rawContent: string;
|
|
10
|
+
date: Date;
|
|
11
|
+
status: EmailStatus;
|
|
12
|
+
}
|
package/src/mailer/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { MailerBase } from './mailer.base';
|
|
2
|
-
export { SMTPMailer } from './smtp-mailer';
|
|
1
|
+
export { MailerBase } from './mailer.base';
|
|
2
|
+
export { SMTPMailer } from './smtp-mailer';
|
|
@@ -1,110 +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
|
-
}
|
|
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
|
+
}
|