@hedhog/mail 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +90 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/mail.service.d.ts.map +1 -1
- package/dist/mail.service.js +3 -3
- package/dist/mail.service.js.map +1 -1
- package/package.json +6 -5
- package/src/enums/mail-configuration-type.enum.ts +5 -0
- package/src/index.ts +5 -0
- package/src/interfaces/index.ts +2 -0
- package/src/interfaces/mail-module-options.interface.ts +40 -0
- package/src/interfaces/mail-type.ts +12 -0
- package/src/mail.consts.ts +20 -0
- package/src/mail.module.ts +46 -0
- package/src/mail.service.ts +300 -0
- package/tsconfig.lib.json +9 -0
- package/tsconfig.production.json +20 -0
package/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# <p align="center">hedhog/mail</p>
|
2
|
+
|
3
|
+
<p align="center">
|
4
|
+
<img src="https://avatars.githubusercontent.com/u/177489127?s=200&v=4" alt="Hedhog Avatar" />
|
5
|
+
</p>
|
6
|
+
|
7
|
+
**HedHog Mail** is an email sending library that is part of the HedHog framework. This library provides support for sending emails using various email services such as AWS SES, Gmail, and SMTP. It is configured to handle multiple types of email sending, including emails with attachments and multi-part content.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- **Send Emails via AWS SES**: Send emails using Amazon Simple Email Service.
|
12
|
+
- **Send Emails via Gmail**: Send emails using the Gmail API.
|
13
|
+
- **Send Emails via SMTP**: Send emails through an SMTP server.
|
14
|
+
- **Create Emails with Attachments**: Support for emails with multiple attachments and formatted content.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
This library is an integral part of the HedHog framework and should be installed as a dependency in your HedHog project. Ensure that the necessary dependencies are configured in your HedHog project.
|
19
|
+
|
20
|
+
```bash
|
21
|
+
npm i @hedhog/mail
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
The `MailService` is the main class responsible for sending emails. Here is a basic example of how to use the email service:
|
27
|
+
|
28
|
+
### Configuration and Sending Emails
|
29
|
+
|
30
|
+
```typescript
|
31
|
+
import { MailService } from '@hedhog/mail';
|
32
|
+
import { Mail } from '@hedhog/mail/interfaces/mail.interface';
|
33
|
+
|
34
|
+
// Assuming you have properly configured MailService in your module
|
35
|
+
|
36
|
+
const mailService = new MailService(/* configuration options */);
|
37
|
+
|
38
|
+
const mail: Mail = {
|
39
|
+
from: 'sender@example.com',
|
40
|
+
to: 'recipient@example.com',
|
41
|
+
subject: 'Test Email',
|
42
|
+
body: '<h1>Hello World!</h1>',
|
43
|
+
attachments: [
|
44
|
+
{
|
45
|
+
filename: 'attachment.txt',
|
46
|
+
content: Buffer.from('Hello, this is an attachment!'),
|
47
|
+
contentType: 'text/plain',
|
48
|
+
},
|
49
|
+
],
|
50
|
+
};
|
51
|
+
|
52
|
+
mailService
|
53
|
+
.send(mail)
|
54
|
+
.then((response) => {
|
55
|
+
console.log('Email sent successfully:', response);
|
56
|
+
})
|
57
|
+
.catch((error) => {
|
58
|
+
console.error('Error sending email:', error);
|
59
|
+
});
|
60
|
+
```
|
61
|
+
|
62
|
+
## Available Methods
|
63
|
+
|
64
|
+
- **send(mail: Mail)**: Sends an email based on the specified configuration (AWS, GMAIL, or SMTP).
|
65
|
+
- **createRawEmail(mail: Mail)**: Creates a raw representation of the email for sending.
|
66
|
+
|
67
|
+
## Folder Structure
|
68
|
+
|
69
|
+
```plaintext
|
70
|
+
mail/
|
71
|
+
├── dist/ # Compiled JavaScript file from build
|
72
|
+
├── node_modules/ # Discardable folder with all module dependencies
|
73
|
+
├── src/
|
74
|
+
│ ├── mail.service.ts # Email sending service
|
75
|
+
│ ├── mail.module.ts # Module for MailService
|
76
|
+
│ └── mail.consts.ts # Constants related to email sending
|
77
|
+
│ ├── mail.service.spec.ts # Unit tests for MailService
|
78
|
+
│ └── mail.template.ejs # Email templates in EJS format
|
79
|
+
│ ├── index.ts # Entry point for mail file
|
80
|
+
│ ├── interfaces/
|
81
|
+
│ │ └── mail-module-options.interface.ts # Interface for configuration options
|
82
|
+
│ ├── enums/
|
83
|
+
│ │ └── mail-configuration-type.enum.ts # Enum for email configuration types
|
84
|
+
├── .gitignore # Specifies which file Git should ignore
|
85
|
+
├── package.json # Manages dependencies and scripts for the library
|
86
|
+
├── package-lock.json # Lock file for dependencies
|
87
|
+
├── README.md # Documentation for the library
|
88
|
+
└── tsconfig.lib.json # TypeScript configuration for the library
|
89
|
+
└── tsconfig.production.json # TypeScript configuration for production builds
|
90
|
+
```
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
export * from './
|
2
|
-
export * from './interfaces';
|
1
|
+
export * from './mail.consts';
|
3
2
|
export * from './mail.module';
|
4
3
|
export * from './mail.service';
|
4
|
+
export * from './enums/mail-configuration-type.enum';
|
5
|
+
export * from './interfaces';
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sCAAsC,CAAC;AACrD,cAAc,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
@@ -14,8 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./
|
18
|
-
__exportStar(require("./interfaces"), exports);
|
17
|
+
__exportStar(require("./mail.consts"), exports);
|
19
18
|
__exportStar(require("./mail.module"), exports);
|
20
19
|
__exportStar(require("./mail.service"), exports);
|
20
|
+
__exportStar(require("./enums/mail-configuration-type.enum"), exports);
|
21
|
+
__exportStar(require("./interfaces"), exports);
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,gDAA8B;AAC9B,iDAA+B;AAC/B,uEAAqD;AACrD,+CAA6B"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mail.service.d.ts","sourceRoot":"","sources":["../src/mail.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAK5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAC/E,OAAO,EAAE,IAAI,EAAuB,MAAM,eAAe,CAAC;AAE1D,qBACa,WAAW;IAGpB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAE3B,OAAO,CAAC,QAAQ,CAAC,WAAW;gBAFX,UAAU,EAAE,iBAAiB,EAE7B,WAAW,EAAE,WAAW;IAGrC,IAAI,CAAC,IAAI,EAAE,IAAI;IAaf,cAAc,CAAC,IAAI,EAAE,IAAI;IAyEzB,YAAY,CAAC,IAAI,EAAE,IAAI;IAqCvB,aAAa,CAAC,IAAI,EAAE,IAAI;
|
1
|
+
{"version":3,"file":"mail.service.d.ts","sourceRoot":"","sources":["../src/mail.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAK5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAC/E,OAAO,EAAE,IAAI,EAAuB,MAAM,eAAe,CAAC;AAE1D,qBACa,WAAW;IAGpB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAE3B,OAAO,CAAC,QAAQ,CAAC,WAAW;gBAFX,UAAU,EAAE,iBAAiB,EAE7B,WAAW,EAAE,WAAW;IAGrC,IAAI,CAAC,IAAI,EAAE,IAAI;IAaf,cAAc,CAAC,IAAI,EAAE,IAAI;IAyEzB,YAAY,CAAC,IAAI,EAAE,IAAI;IAqCvB,aAAa,CAAC,IAAI,EAAE,IAAI;IAsCxB,WAAW,CAAC,IAAI,EAAE,IAAI;CAyH7B"}
|
package/dist/mail.service.js
CHANGED
@@ -146,14 +146,14 @@ let MailService = class MailService {
|
|
146
146
|
}
|
147
147
|
const { clientId, clientSecret, from, refreshToken } = this.mailConfig;
|
148
148
|
const redirectURI = 'https://developers.google.com/oauthplayground';
|
149
|
-
const {
|
150
|
-
const oauth2Client = new
|
149
|
+
const { auth } = await Promise.resolve().then(() => __importStar(require('googleapis/build/src/apis/oauth2')));
|
150
|
+
const oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectURI);
|
151
151
|
oauth2Client.setCredentials({
|
152
152
|
refresh_token: refreshToken,
|
153
153
|
});
|
154
154
|
const { token } = await oauth2Client.getAccessToken();
|
155
155
|
const raw = await this.createRawEmail(Object.assign(Object.assign({}, mail), { from }));
|
156
|
-
const url = 'https://www.googleapis.com/gmail/v1/
|
156
|
+
const url = 'https://www.googleapis.com/gmail/v1/user/me/messages/send';
|
157
157
|
const requestBody = {
|
158
158
|
raw,
|
159
159
|
};
|
package/dist/mail.service.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mail.service.js","sourceRoot":"","sources":["../src/mail.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAA4C;AAC5C,2CAAgE;AAChE,yDAA2C;AAC3C,+BAAsC;AACtC,uFAAiF;AAEjF,+CAA0D;AAGnD,IAAM,WAAW,GAAjB,MAAM,WAAW;IACt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AO,EAAE,CAAC;QACf,CAAC;aAAM,CAAC;YACN,MAAM,MAAM,GAAG;gBACb,WAAW,EAAE;oBACX,WAAW,EAAE,IAAI,CAAC,EAAE;oBACpB,YAAY,EAAE,IAAI,CAAC,GAAG;oBACtB,WAAW,EAAE,IAAI,CAAC,EAAE;iBACrB;gBACD,OAAO,EAAE;oBACP,IAAI,EAAE;wBACJ,IAAI,EAAE;4BACJ,IAAI,EAAE,IAAI,CAAC,IAAI;4BACf,OAAO,EAAE,OAAO;yBACjB;wBACD,IAAI,EAAE;4BACJ,IAAI,EAAE,IAAI,CAAC,IAAI;4BACf,OAAO,EAAE,OAAO;yBACjB;qBACF;oBACD,OAAO,EAAE;wBACP,IAAI,EAAE,IAAI,CAAC,OAAO;wBAClB,OAAO,EAAE,OAAO;qBACjB;iBACF;gBACD,gBAAgB,EAAE,IAAI,CAAC,OAAO;gBAC9B,MAAM,EAAE,IAAI;aACb,CAAC;YAEF,OAAO,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC;QACzC,CAAC;IACH,CAAC;CACF,CAAA;AAlSY,kCAAW;sBAAX,WAAW;IADvB,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,iCAAmB,CAAC,CAAA;IAE3B,WAAA,IAAA,eAAM,EAAC,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,mBAAW,CAAC,CAAC,CAAA;6CACR,mBAAW;GALhC,WAAW,CAkSvB"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hedhog/mail",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.14",
|
4
4
|
"main": "dist/index.js",
|
5
5
|
"scripts": {
|
6
6
|
"build": "tsc --project tsconfig.production.json && npm version patch",
|
@@ -10,20 +10,21 @@
|
|
10
10
|
"author": "HedHog",
|
11
11
|
"license": "MIT",
|
12
12
|
"description": "",
|
13
|
-
"
|
13
|
+
"file": [
|
14
14
|
"dist/**/*",
|
15
15
|
"src/migrations/**/*.ts",
|
16
16
|
"src/**/*.ejs"
|
17
17
|
],
|
18
18
|
"dependencies": {
|
19
19
|
"@nestjs/axios": "^3.0.3",
|
20
|
+
"@nestjs/jwt": "^10.2.0",
|
20
21
|
"axios": "^1.7.7",
|
21
|
-
"mimemessage": "1.0.5"
|
22
|
+
"mimemessage": "1.0.5",
|
23
|
+
"bcrypt": "^5.1.1"
|
22
24
|
},
|
23
25
|
"peerDependencies": {
|
24
26
|
"aws-sdk": "2.1691.0",
|
25
|
-
"googleapis": "144.0.0"
|
26
|
-
"nodemailer": "6.9.15"
|
27
|
+
"googleapis": "144.0.0"
|
27
28
|
},
|
28
29
|
"peerDependenciesMeta": {
|
29
30
|
"aws-sdk": {
|
package/src/index.ts
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
import { ModuleMetadata } from '@nestjs/common';
|
2
|
+
|
3
|
+
export type MailModuleOptions =
|
4
|
+
| {
|
5
|
+
global?: boolean;
|
6
|
+
type: 'AWS';
|
7
|
+
region: string;
|
8
|
+
accessKeyId: string;
|
9
|
+
secretAccessKey: string;
|
10
|
+
from: string;
|
11
|
+
}
|
12
|
+
| {
|
13
|
+
global?: boolean;
|
14
|
+
type: 'SMTP';
|
15
|
+
host: string;
|
16
|
+
port: number;
|
17
|
+
secure?: boolean;
|
18
|
+
username: string;
|
19
|
+
password: string;
|
20
|
+
}
|
21
|
+
| {
|
22
|
+
global?: boolean;
|
23
|
+
type: 'GMAIL';
|
24
|
+
clientId: string;
|
25
|
+
clientSecret: string;
|
26
|
+
refreshToken: string;
|
27
|
+
from: string;
|
28
|
+
};
|
29
|
+
|
30
|
+
export interface MailOptionsFactory {
|
31
|
+
createMailOptions(): Promise<MailModuleOptions> | MailModuleOptions;
|
32
|
+
}
|
33
|
+
|
34
|
+
export interface MailModuleAsyncOptions
|
35
|
+
extends Pick<ModuleMetadata, 'imports'> {
|
36
|
+
useFactory: (
|
37
|
+
...args: any[]
|
38
|
+
) => Promise<MailModuleOptions> | MailModuleOptions;
|
39
|
+
inject?: any[];
|
40
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
export interface MailType {
|
2
|
+
from?: string;
|
3
|
+
to: string | string[];
|
4
|
+
cc?: string | string[];
|
5
|
+
bcc?: string | string[];
|
6
|
+
subject?: string;
|
7
|
+
body?: string;
|
8
|
+
replyTo?: string | string[];
|
9
|
+
priority?: string;
|
10
|
+
createdAt?: string;
|
11
|
+
updatedAt?: string;
|
12
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { MailType } from './interfaces/mail-type';
|
2
|
+
|
3
|
+
export type MailAttachment = {
|
4
|
+
filename?: string;
|
5
|
+
content?: Buffer;
|
6
|
+
contentType?: string;
|
7
|
+
};
|
8
|
+
|
9
|
+
export type Mail = {
|
10
|
+
attachments?: MailAttachment[];
|
11
|
+
} & MailType;
|
12
|
+
|
13
|
+
export type MailConfig = {
|
14
|
+
host?: string;
|
15
|
+
from?: string;
|
16
|
+
accessKeyId?: string;
|
17
|
+
secretAccessKey?: string;
|
18
|
+
};
|
19
|
+
|
20
|
+
export const MAIL_MODULE_OPTIONS = 'MAIL_MODULE_OPTIONS';
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import { HttpModule } from '@nestjs/axios';
|
2
|
+
import { DynamicModule, Global, Module, Provider } from '@nestjs/common';
|
3
|
+
import {
|
4
|
+
MailModuleAsyncOptions,
|
5
|
+
MailModuleOptions,
|
6
|
+
} from './interfaces/mail-module-options.interface';
|
7
|
+
import { MAIL_MODULE_OPTIONS } from './mail.consts';
|
8
|
+
import { MailService } from './mail.service';
|
9
|
+
|
10
|
+
@Global()
|
11
|
+
@Module({})
|
12
|
+
export class MailModule {
|
13
|
+
static forRoot(options: MailModuleOptions): DynamicModule {
|
14
|
+
return {
|
15
|
+
module: MailModule,
|
16
|
+
imports: [HttpModule],
|
17
|
+
providers: [
|
18
|
+
MailService,
|
19
|
+
{
|
20
|
+
provide: MAIL_MODULE_OPTIONS,
|
21
|
+
useValue: options,
|
22
|
+
},
|
23
|
+
],
|
24
|
+
exports: [MailService],
|
25
|
+
};
|
26
|
+
}
|
27
|
+
|
28
|
+
static forRootAsync(options: MailModuleAsyncOptions): DynamicModule {
|
29
|
+
return {
|
30
|
+
module: MailModule,
|
31
|
+
imports: [...(options.imports || []), HttpModule],
|
32
|
+
providers: [MailService, this.createAsyncOptionsProvider(options)],
|
33
|
+
exports: [MailService],
|
34
|
+
};
|
35
|
+
}
|
36
|
+
|
37
|
+
private static createAsyncOptionsProvider(
|
38
|
+
options: MailModuleAsyncOptions,
|
39
|
+
): Provider {
|
40
|
+
return {
|
41
|
+
provide: MAIL_MODULE_OPTIONS,
|
42
|
+
useFactory: options.useFactory,
|
43
|
+
inject: options.inject || [],
|
44
|
+
};
|
45
|
+
}
|
46
|
+
}
|
@@ -0,0 +1,300 @@
|
|
1
|
+
import { HttpService } from '@nestjs/axios';
|
2
|
+
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
3
|
+
import * as mimemessage from 'mimemessage';
|
4
|
+
import { firstValueFrom } from 'rxjs';
|
5
|
+
import { MailConfigurationTypeEnum } from './enums/mail-configuration-type.enum';
|
6
|
+
import { MailModuleOptions } from './interfaces/mail-module-options.interface';
|
7
|
+
import { Mail, MAIL_MODULE_OPTIONS } from './mail.consts';
|
8
|
+
|
9
|
+
@Injectable()
|
10
|
+
export class MailService {
|
11
|
+
constructor(
|
12
|
+
@Inject(MAIL_MODULE_OPTIONS)
|
13
|
+
private readonly mailConfig: MailModuleOptions,
|
14
|
+
@Inject(forwardRef(() => HttpService))
|
15
|
+
private readonly httpService: HttpService,
|
16
|
+
) {}
|
17
|
+
|
18
|
+
async send(mail: Mail) {
|
19
|
+
switch (this.mailConfig.type) {
|
20
|
+
case MailConfigurationTypeEnum.AWS:
|
21
|
+
return this.sendWithSES(mail);
|
22
|
+
|
23
|
+
case MailConfigurationTypeEnum.GMAIL:
|
24
|
+
return this.sendWithGmail(mail);
|
25
|
+
|
26
|
+
case MailConfigurationTypeEnum.SMTP:
|
27
|
+
return this.sendWithSMTP(mail);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
async createRawEmail(mail: Mail) {
|
32
|
+
if (mail.attachments instanceof Array && mail.attachments?.length) {
|
33
|
+
const mailContent = mimemessage.factory({
|
34
|
+
contentType: 'multipart/mixed',
|
35
|
+
body: [],
|
36
|
+
});
|
37
|
+
|
38
|
+
mailContent.header('From', mail.from);
|
39
|
+
mailContent.header('To', mail.to);
|
40
|
+
mailContent.header('Subject', mail.subject);
|
41
|
+
|
42
|
+
const alternateEntity = mimemessage.factory({
|
43
|
+
contentType: 'multipart/alternate',
|
44
|
+
body: [],
|
45
|
+
});
|
46
|
+
|
47
|
+
const htmlEntity = mimemessage.factory({
|
48
|
+
contentType: 'text/html;charset=utf-8',
|
49
|
+
body: mail.body,
|
50
|
+
});
|
51
|
+
|
52
|
+
alternateEntity.body.push(htmlEntity);
|
53
|
+
|
54
|
+
mailContent.body.push(alternateEntity);
|
55
|
+
|
56
|
+
await Promise.all(
|
57
|
+
(mail.attachments ?? []).map(async (attachment) => {
|
58
|
+
const attachmentEntity = mimemessage.factory({
|
59
|
+
contentType: attachment.contentType,
|
60
|
+
contentTransferEncoding: 'base64',
|
61
|
+
body: attachment.content
|
62
|
+
.toString('base64')
|
63
|
+
.replace(/([^\0]{76})/g, '$1\n'),
|
64
|
+
});
|
65
|
+
|
66
|
+
attachmentEntity.header(
|
67
|
+
'Content-Disposition',
|
68
|
+
`attachment; filename="${attachment.filename}"`,
|
69
|
+
);
|
70
|
+
|
71
|
+
mailContent.body.push(attachmentEntity);
|
72
|
+
}),
|
73
|
+
);
|
74
|
+
|
75
|
+
const messageString = mailContent.toString();
|
76
|
+
const encodedMessage = Buffer.from(messageString)
|
77
|
+
.toString('base64')
|
78
|
+
.replace(/\+/g, '-')
|
79
|
+
.replace(/\//g, '_')
|
80
|
+
.replace(/=+$/, '');
|
81
|
+
|
82
|
+
return encodedMessage;
|
83
|
+
} else {
|
84
|
+
const messageParts = [
|
85
|
+
`From: ${mail.from}`,
|
86
|
+
`To: ${mail.to instanceof Array ? mail.to.join(',') : mail.to}`,
|
87
|
+
`Subject: ${mail.subject}`,
|
88
|
+
'Content-Type: text/plain; charset="UTF-8"',
|
89
|
+
'',
|
90
|
+
mail.body,
|
91
|
+
];
|
92
|
+
|
93
|
+
const message = messageParts.join('\n');
|
94
|
+
const encodedMessage = Buffer.from(message)
|
95
|
+
.toString('base64')
|
96
|
+
.replace(/\+/g, '-')
|
97
|
+
.replace(/\//g, '_')
|
98
|
+
.replace(/=+$/, '');
|
99
|
+
|
100
|
+
return encodedMessage;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
async sendWithSMTP(mail: Mail) {
|
105
|
+
if (this.mailConfig.type !== 'SMTP') {
|
106
|
+
throw new Error('Invalid mail configuration type');
|
107
|
+
}
|
108
|
+
|
109
|
+
const {
|
110
|
+
password: pass,
|
111
|
+
username: user,
|
112
|
+
host,
|
113
|
+
port,
|
114
|
+
secure = false,
|
115
|
+
} = this.mailConfig;
|
116
|
+
|
117
|
+
const nodemailer = await import('nodemailer');
|
118
|
+
|
119
|
+
const transporter = nodemailer.createTransport({
|
120
|
+
host,
|
121
|
+
port,
|
122
|
+
secure,
|
123
|
+
auth: {
|
124
|
+
user,
|
125
|
+
pass,
|
126
|
+
},
|
127
|
+
});
|
128
|
+
|
129
|
+
return transporter.sendMail({
|
130
|
+
from: mail.from || process.env.SMTP_FROM || process.env.SMTP_USER,
|
131
|
+
to: mail.to,
|
132
|
+
subject: mail.subject,
|
133
|
+
html: mail.body,
|
134
|
+
cc: mail.cc,
|
135
|
+
bcc: mail.bcc,
|
136
|
+
replyTo: mail.replyTo,
|
137
|
+
priority: mail.priority,
|
138
|
+
});
|
139
|
+
}
|
140
|
+
|
141
|
+
async sendWithGmail(mail: Mail) {
|
142
|
+
if (this.mailConfig.type !== 'GMAIL') {
|
143
|
+
throw new Error('Invalid mail configuration type');
|
144
|
+
}
|
145
|
+
const { clientId, clientSecret, from, refreshToken } = this.mailConfig;
|
146
|
+
const redirectURI = 'https://developers.google.com/oauthplayground';
|
147
|
+
|
148
|
+
const { auth } = await import('googleapis/build/src/apis/oauth2');
|
149
|
+
|
150
|
+
const oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectURI);
|
151
|
+
|
152
|
+
oauth2Client.setCredentials({
|
153
|
+
refresh_token: refreshToken,
|
154
|
+
});
|
155
|
+
|
156
|
+
const { token } = await oauth2Client.getAccessToken();
|
157
|
+
|
158
|
+
const raw = await this.createRawEmail({
|
159
|
+
...mail,
|
160
|
+
from,
|
161
|
+
});
|
162
|
+
|
163
|
+
const url = 'https://www.googleapis.com/gmail/v1/user/me/messages/send';
|
164
|
+
|
165
|
+
const requestBody = {
|
166
|
+
raw,
|
167
|
+
};
|
168
|
+
|
169
|
+
const headers = {
|
170
|
+
Authorization: `Bearer ${token}`,
|
171
|
+
'Content-Type': 'application/json',
|
172
|
+
};
|
173
|
+
|
174
|
+
const response = this.httpService.post(url, requestBody, { headers });
|
175
|
+
|
176
|
+
return firstValueFrom(response);
|
177
|
+
}
|
178
|
+
|
179
|
+
async sendWithSES(mail: Mail) {
|
180
|
+
if (this.mailConfig.type !== 'AWS') {
|
181
|
+
throw new Error('Invalid mail configuration type');
|
182
|
+
}
|
183
|
+
const { region, from, accessKeyId, secretAccessKey } = this.mailConfig;
|
184
|
+
|
185
|
+
const { SES } = await import('aws-sdk');
|
186
|
+
|
187
|
+
const ses = new SES({
|
188
|
+
apiVersion: '2010-12-01',
|
189
|
+
region,
|
190
|
+
credentials: {
|
191
|
+
accessKeyId,
|
192
|
+
secretAccessKey,
|
193
|
+
},
|
194
|
+
});
|
195
|
+
|
196
|
+
ses.config.update({
|
197
|
+
credentials: {
|
198
|
+
accessKeyId,
|
199
|
+
secretAccessKey,
|
200
|
+
},
|
201
|
+
});
|
202
|
+
|
203
|
+
if (typeof mail.to === 'string') {
|
204
|
+
mail.to = mail.to.split(';');
|
205
|
+
}
|
206
|
+
if (typeof mail.bcc === 'string') {
|
207
|
+
mail.bcc = mail.bcc.split(';');
|
208
|
+
} else if (!mail.bcc) {
|
209
|
+
mail.bcc = [];
|
210
|
+
}
|
211
|
+
if (typeof mail.cc === 'string') {
|
212
|
+
mail.cc = mail.cc.split(';');
|
213
|
+
} else if (!mail.cc) {
|
214
|
+
mail.cc = [];
|
215
|
+
}
|
216
|
+
if (typeof mail.replyTo === 'string') {
|
217
|
+
mail.replyTo = mail.replyTo.split(';');
|
218
|
+
} else if (!mail.replyTo) {
|
219
|
+
mail.replyTo = [];
|
220
|
+
}
|
221
|
+
|
222
|
+
if (mail.attachments instanceof Array && mail.attachments.length > 0) {
|
223
|
+
const mailContent = mimemessage.factory({
|
224
|
+
contentType: 'multipart/mixed',
|
225
|
+
body: [],
|
226
|
+
});
|
227
|
+
|
228
|
+
mailContent.header('From', from);
|
229
|
+
mailContent.header('To', mail.to);
|
230
|
+
mailContent.header('Subject', mail.subject);
|
231
|
+
|
232
|
+
const alternateEntity = mimemessage.factory({
|
233
|
+
contentType: 'multipart/alternate',
|
234
|
+
body: [],
|
235
|
+
});
|
236
|
+
|
237
|
+
const htmlEntity = mimemessage.factory({
|
238
|
+
contentType: 'text/html;charset=utf-8',
|
239
|
+
body: mail.body,
|
240
|
+
});
|
241
|
+
|
242
|
+
alternateEntity.body.push(htmlEntity);
|
243
|
+
|
244
|
+
mailContent.body.push(alternateEntity);
|
245
|
+
|
246
|
+
await Promise.all(
|
247
|
+
(mail.attachments ?? []).map((item) => {
|
248
|
+
const attachmentEntity = mimemessage.factory({
|
249
|
+
contentType: item.contentType,
|
250
|
+
contentTransferEncoding: 'base64',
|
251
|
+
body: item.content
|
252
|
+
.toString('base64')
|
253
|
+
.replace(/([^\0]{76})/g, '$1\n'),
|
254
|
+
});
|
255
|
+
|
256
|
+
attachmentEntity.header(
|
257
|
+
'Content-Disposition',
|
258
|
+
`attachment ;filename="${item.filename}"`,
|
259
|
+
);
|
260
|
+
|
261
|
+
mailContent.body.push(attachmentEntity);
|
262
|
+
}),
|
263
|
+
);
|
264
|
+
|
265
|
+
return ses
|
266
|
+
.sendRawEmail({
|
267
|
+
RawMessage: { Data: mailContent.toString() },
|
268
|
+
})
|
269
|
+
.promise();
|
270
|
+
} else {
|
271
|
+
const params = {
|
272
|
+
Destination: {
|
273
|
+
ToAddresses: mail.to,
|
274
|
+
BccAddresses: mail.bcc,
|
275
|
+
CcAddresses: mail.cc,
|
276
|
+
},
|
277
|
+
Message: {
|
278
|
+
Body: {
|
279
|
+
Html: {
|
280
|
+
Data: mail.body,
|
281
|
+
Charset: 'utf-8',
|
282
|
+
},
|
283
|
+
Text: {
|
284
|
+
Data: mail.body,
|
285
|
+
Charset: 'utf-8',
|
286
|
+
},
|
287
|
+
},
|
288
|
+
Subject: {
|
289
|
+
Data: mail.subject,
|
290
|
+
Charset: 'utf-8',
|
291
|
+
},
|
292
|
+
},
|
293
|
+
ReplyToAddresses: mail.replyTo,
|
294
|
+
Source: from,
|
295
|
+
};
|
296
|
+
|
297
|
+
return ses.sendEmail(params).promise();
|
298
|
+
}
|
299
|
+
}
|
300
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"experimentalDecorators": true,
|
4
|
+
"target": "es2017",
|
5
|
+
"module": "commonjs",
|
6
|
+
"lib": ["es2017", "es7", "es6"],
|
7
|
+
"declaration": true,
|
8
|
+
"declarationMap": true,
|
9
|
+
"sourceMap": true,
|
10
|
+
"outDir": "./dist",
|
11
|
+
"rootDir": "./src",
|
12
|
+
"strict": true,
|
13
|
+
"noImplicitAny": false,
|
14
|
+
"strictNullChecks": false,
|
15
|
+
"allowSyntheticDefaultImports": true,
|
16
|
+
"esModuleInterop": true,
|
17
|
+
"emitDecoratorMetadata": true
|
18
|
+
},
|
19
|
+
"exclude": ["node_modules", "dist"]
|
20
|
+
}
|