@drax/email-back 0.9.0
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/dist/config/EmailConfig.js +14 -0
- package/dist/factory/EmailServiceFactory.js +46 -0
- package/dist/index.js +4 -0
- package/dist/interfaces/IEmailLayout.js +1 -0
- package/dist/interfaces/ITransportConfig.js +1 -0
- package/dist/services/EmailLayoutService.js +106 -0
- package/dist/services/EmailService.js +58 -0
- package/package.json +42 -0
- package/src/config/EmailConfig.ts +14 -0
- package/src/factory/EmailServiceFactory.ts +56 -0
- package/src/index.ts +18 -0
- package/src/interfaces/IEmailLayout.ts +25 -0
- package/src/interfaces/ITransportConfig.ts +21 -0
- package/src/services/EmailLayoutService.ts +123 -0
- package/src/services/EmailService.ts +81 -0
- package/test/Email.test.ts +83 -0
- package/test/EmailLayout.test.ts +193 -0
- package/test/template.pug +123 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/types/config/EmailConfig.d.ts +14 -0
- package/types/config/EmailConfig.d.ts.map +1 -0
- package/types/factory/EmailServiceFactory.d.ts +11 -0
- package/types/factory/EmailServiceFactory.d.ts.map +1 -0
- package/types/index.d.ts +8 -0
- package/types/index.d.ts.map +1 -0
- package/types/interfaces/IEmailLayout.d.ts +19 -0
- package/types/interfaces/IEmailLayout.d.ts.map +1 -0
- package/types/interfaces/ITransportConfig.d.ts +19 -0
- package/types/interfaces/ITransportConfig.d.ts.map +1 -0
- package/types/services/EmailLayoutService.d.ts +12 -0
- package/types/services/EmailLayoutService.d.ts.map +1 -0
- package/types/services/EmailService.d.ts +13 -0
- package/types/services/EmailService.d.ts.map +1 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
var EmailConfig;
|
|
2
|
+
(function (EmailConfig) {
|
|
3
|
+
EmailConfig["type"] = "EMAIL_TYPE";
|
|
4
|
+
EmailConfig["service"] = "EMAIL_SERVICE";
|
|
5
|
+
EmailConfig["smtpHost"] = "EMAIL_HOST";
|
|
6
|
+
EmailConfig["smtpPort"] = "EMAIL_PORT";
|
|
7
|
+
EmailConfig["authType"] = "EMAIL_AUTH_TYPE";
|
|
8
|
+
EmailConfig["authUsername"] = "EMAIL_AUTH_USERNAME";
|
|
9
|
+
EmailConfig["authPassword"] = "EMAIL_AUTH_PASSWORD";
|
|
10
|
+
EmailConfig["secure"] = "EMAIL_SECURE";
|
|
11
|
+
EmailConfig["ignoreTLS"] = "EMAIL_IGNORE_TLS";
|
|
12
|
+
})(EmailConfig || (EmailConfig = {}));
|
|
13
|
+
export default EmailConfig;
|
|
14
|
+
export { EmailConfig };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { DraxConfig } from "@drax/common-back";
|
|
2
|
+
import { EmailService } from '../services/EmailService.js';
|
|
3
|
+
import EmailConfig from "../config/EmailConfig";
|
|
4
|
+
class EmailServiceFactory {
|
|
5
|
+
static get instance() {
|
|
6
|
+
if (!EmailServiceFactory.service) {
|
|
7
|
+
const type = EmailServiceFactory.getType;
|
|
8
|
+
const options = EmailServiceFactory.getOptions;
|
|
9
|
+
EmailServiceFactory.service = new EmailService(type, options);
|
|
10
|
+
}
|
|
11
|
+
return EmailServiceFactory.service;
|
|
12
|
+
}
|
|
13
|
+
static get getType() {
|
|
14
|
+
return DraxConfig.getOrLoad(EmailConfig.type);
|
|
15
|
+
}
|
|
16
|
+
static get getOptions() {
|
|
17
|
+
let options;
|
|
18
|
+
switch (DraxConfig.getOrLoad(EmailConfig.type)) {
|
|
19
|
+
case 'smtp':
|
|
20
|
+
options = {
|
|
21
|
+
host: DraxConfig.getOrLoad(EmailConfig.smtpHost),
|
|
22
|
+
port: DraxConfig.getOrLoad(EmailConfig.smtpPort),
|
|
23
|
+
secure: DraxConfig.getOrLoad(EmailConfig.secure),
|
|
24
|
+
ignoreTLS: DraxConfig.getOrLoad(EmailConfig.ignoreTLS),
|
|
25
|
+
auth: {
|
|
26
|
+
user: DraxConfig.getOrLoad(EmailConfig.authUsername),
|
|
27
|
+
pass: DraxConfig.getOrLoad(EmailConfig.authPassword),
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
break;
|
|
31
|
+
case 'gmail':
|
|
32
|
+
options = {
|
|
33
|
+
auth: {
|
|
34
|
+
user: DraxConfig.getOrLoad(EmailConfig.authUsername),
|
|
35
|
+
pass: DraxConfig.getOrLoad(EmailConfig.authPassword),
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
throw new Error(`Unsupported email service type: ${DraxConfig.getOrLoad(EmailConfig.type)}`);
|
|
41
|
+
}
|
|
42
|
+
return options;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export default EmailServiceFactory;
|
|
46
|
+
export { EmailServiceFactory };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
class EmailLayoutService {
|
|
2
|
+
constructor(options = {}) {
|
|
3
|
+
const defaultOptions = {
|
|
4
|
+
bodyStyle: 'font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #fcfcfc;',
|
|
5
|
+
maxWidth: "800px",
|
|
6
|
+
headerCentered: true,
|
|
7
|
+
headerBgColor: "#f4f4f4",
|
|
8
|
+
headerColor: "#111111",
|
|
9
|
+
headerImage: "",
|
|
10
|
+
headerImageStyle: "width: auto; height: auto; display: block; margin: 0 auto;",
|
|
11
|
+
headerTitle: "",
|
|
12
|
+
headerTitleStyle: "font-size: 28px; color: #333333; margin: 10px; font-weight: 700; display: inline-block;",
|
|
13
|
+
headerLogo: "",
|
|
14
|
+
headerLogoStyle: "max-height: 50px; display: block; margin: 10px auto;",
|
|
15
|
+
footerBgColor: "#f4f4f4",
|
|
16
|
+
footerCopyright: "Your Company. All rights reserved.",
|
|
17
|
+
footerContent: "Default footer content",
|
|
18
|
+
footerUnsubscribe: "",
|
|
19
|
+
};
|
|
20
|
+
this.options = { ...defaultOptions, ...options };
|
|
21
|
+
}
|
|
22
|
+
get head() {
|
|
23
|
+
return `<head>
|
|
24
|
+
<meta charset="UTF-8">
|
|
25
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
26
|
+
</head>`;
|
|
27
|
+
}
|
|
28
|
+
get header() {
|
|
29
|
+
let header = `<tr style="background-color: ${this.options.headerBgColor}">
|
|
30
|
+
<td style="${this.options.headerCentered ? 'text-align: center;' : ''} vertical-align: middle;">`;
|
|
31
|
+
if (this.options.headerImage) {
|
|
32
|
+
header += `<img src="${this.options.headerImage}"
|
|
33
|
+
alt="${this.options.headerTitle}"
|
|
34
|
+
style="${this.options.headerImageStyle}"
|
|
35
|
+
/>`;
|
|
36
|
+
}
|
|
37
|
+
else if (this.options.headerLogo && this.options.headerTitle) {
|
|
38
|
+
header += `<table style="display: inline-table; text-align: left;">
|
|
39
|
+
<tr>
|
|
40
|
+
<td style="vertical-align: middle; padding: 10px;">
|
|
41
|
+
<img src="${this.options.headerLogo}"
|
|
42
|
+
alt="${this.options.headerTitle}"
|
|
43
|
+
style="${this.options.headerLogoStyle}"
|
|
44
|
+
/>
|
|
45
|
+
</td>
|
|
46
|
+
<td style="vertical-align: middle;">
|
|
47
|
+
<h1 style="${this.options.headerTitleStyle}">${this.options.headerTitle}</h1>
|
|
48
|
+
</td>
|
|
49
|
+
</tr>
|
|
50
|
+
</table>`;
|
|
51
|
+
}
|
|
52
|
+
else if (this.options.headerTitle) {
|
|
53
|
+
header += `<h1 style="${this.options.headerTitleStyle}"
|
|
54
|
+
>
|
|
55
|
+
${this.options.headerTitle}
|
|
56
|
+
</h1>`;
|
|
57
|
+
}
|
|
58
|
+
else if (this.options.headerLogo) {
|
|
59
|
+
header += `<img src="${this.options.headerLogo}" alt="${this.options.headerTitle}"
|
|
60
|
+
style="${this.options.headerLogoStyle}"
|
|
61
|
+
/>`;
|
|
62
|
+
}
|
|
63
|
+
header += `</td></tr>`;
|
|
64
|
+
return header;
|
|
65
|
+
}
|
|
66
|
+
get footer() {
|
|
67
|
+
let footer = `<tr style=" ${this.options.footerBgColor ? 'background-color:' + this.options.footerBgColor : ''}">
|
|
68
|
+
<td style="text-align: center; padding: 20px; font-size: 12px; color: #888888;">`;
|
|
69
|
+
if (this.options.footerCopyright) {
|
|
70
|
+
footer += ` <p style="margin: 0;">© ${new Date().getFullYear()} ${this.options.footerCopyright}.</p>`;
|
|
71
|
+
}
|
|
72
|
+
if (this.options.footerContent) {
|
|
73
|
+
footer += `${this.options.footerContent}`;
|
|
74
|
+
}
|
|
75
|
+
if (this.options.footerUnsubscribe) {
|
|
76
|
+
footer += `${this.options.footerUnsubscribe}`;
|
|
77
|
+
//footer += `<p style="margin: 5px 0 0 0;">Si no deseas recibir nuestros correos, <a href="${this.options.unsubscribeLink}" target="_blank" style="color: #555555; text-decoration: none;">haz clic aquí</a> para darte de baja.</p>`
|
|
78
|
+
}
|
|
79
|
+
footer += `</td></tr>`;
|
|
80
|
+
return footer;
|
|
81
|
+
}
|
|
82
|
+
html(body) {
|
|
83
|
+
return `<!DOCTYPE html>
|
|
84
|
+
<html lang="es">
|
|
85
|
+
${this.head}
|
|
86
|
+
<body style="${this.options.bodyStyle}">
|
|
87
|
+
<table role="presentation"
|
|
88
|
+
style="border-spacing: 0; margin: 0 auto; width: 100%; max-width: ${this.options.maxWidth}"
|
|
89
|
+
>
|
|
90
|
+
|
|
91
|
+
<!-- HEADER -->
|
|
92
|
+
${this.header}
|
|
93
|
+
<!-- BODY -->
|
|
94
|
+
<tr>
|
|
95
|
+
<td style="padding: 20px; background-color: #ffffff;">
|
|
96
|
+
${body}
|
|
97
|
+
</td>
|
|
98
|
+
</tr>
|
|
99
|
+
<!-- FOOTER -->
|
|
100
|
+
${this.footer}
|
|
101
|
+
</table>
|
|
102
|
+
</body>`;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export default EmailLayoutService;
|
|
106
|
+
export { EmailLayoutService };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import nodemailer from 'nodemailer';
|
|
2
|
+
class EmailService {
|
|
3
|
+
constructor(type, config) {
|
|
4
|
+
this.transporterConfig = config;
|
|
5
|
+
switch (type) {
|
|
6
|
+
case 'smtp':
|
|
7
|
+
this.prepareTransportSmtp(config);
|
|
8
|
+
break;
|
|
9
|
+
case 'gmail':
|
|
10
|
+
this.prepareTransportGmail(config);
|
|
11
|
+
break;
|
|
12
|
+
default:
|
|
13
|
+
throw new Error(`Unsupported email service type: ${type}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
prepareTransportGmail(config) {
|
|
17
|
+
this.transporter = nodemailer.createTransport({
|
|
18
|
+
service: 'gmail',
|
|
19
|
+
auth: {
|
|
20
|
+
user: config.auth.user,
|
|
21
|
+
pass: config.auth.pass
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
prepareTransportSmtp(config) {
|
|
26
|
+
this.transporter = nodemailer.createTransport({
|
|
27
|
+
host: config.host,
|
|
28
|
+
port: config.port,
|
|
29
|
+
secure: config.secure,
|
|
30
|
+
ignoreTLS: config.ignoreTLS,
|
|
31
|
+
auth: (config.auth.user && config.auth.pass) ? {
|
|
32
|
+
type: config.auth.type ? config.auth.type : 'login',
|
|
33
|
+
user: config.auth.user,
|
|
34
|
+
pass: config.auth.pass
|
|
35
|
+
} : null
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
sendEmail(options) {
|
|
39
|
+
if (!this.transporter) {
|
|
40
|
+
throw new Error('Transporter is not initialized');
|
|
41
|
+
}
|
|
42
|
+
if (!options.to) {
|
|
43
|
+
throw new Error('No recipient provided');
|
|
44
|
+
}
|
|
45
|
+
if (!options.html && !options.text) {
|
|
46
|
+
throw new Error('Either HTML or TEXT content is required');
|
|
47
|
+
}
|
|
48
|
+
if (!options.subject) {
|
|
49
|
+
throw new Error('No subject provided');
|
|
50
|
+
}
|
|
51
|
+
if (!options.from) {
|
|
52
|
+
options.from = this.transporterConfig.auth.user;
|
|
53
|
+
}
|
|
54
|
+
return this.transporter.sendMail(options);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export default EmailService;
|
|
58
|
+
export { EmailService };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@drax/email-back",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "0.9.0",
|
|
7
|
+
"description": "Email utils across modules",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "types/index.d.ts",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prepublish": "tsc && npm run copygql",
|
|
13
|
+
"tscrun": "tsc",
|
|
14
|
+
"clean": "rm -rf dist",
|
|
15
|
+
"copygql": "copyfiles -u 1 ./**/*.graphql dist/",
|
|
16
|
+
"tsc": "tsc -b tsconfig.json",
|
|
17
|
+
"test": "node --import tsx --test test/**/*",
|
|
18
|
+
"testCache": "node --import tsx --test test/cache/*",
|
|
19
|
+
"testMongoose": "node --import tsx --test test/mongoose/*"
|
|
20
|
+
},
|
|
21
|
+
"author": "Cristian Incarnato & Drax Team",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"nodemailer": "^6.9.16",
|
|
25
|
+
"preview-email": "^3.1.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.12.10",
|
|
29
|
+
"@types/nodemailer": "^6.4.17",
|
|
30
|
+
"@types/preview-email": "^3.1.0",
|
|
31
|
+
"copyfiles": "^2.4.1",
|
|
32
|
+
"mongoose-paginate-v2": "^1.8.3",
|
|
33
|
+
"nodemailer": "^6.9.16",
|
|
34
|
+
"nodemon": "^3.1.0",
|
|
35
|
+
"preview-email": "^3.1.0",
|
|
36
|
+
"ts-node": "^10.9.2",
|
|
37
|
+
"tsc-alias": "^1.8.10",
|
|
38
|
+
"typescript": "^5.6.2",
|
|
39
|
+
"vitest": "^2.1.8"
|
|
40
|
+
},
|
|
41
|
+
"gitHead": "aae0068b0343cbb23e85c2a9ea454e183739a8a7"
|
|
42
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
enum EmailConfig {
|
|
2
|
+
type = "EMAIL_TYPE",
|
|
3
|
+
service = "EMAIL_SERVICE",
|
|
4
|
+
smtpHost = "EMAIL_HOST",
|
|
5
|
+
smtpPort = "EMAIL_PORT",
|
|
6
|
+
authType = "EMAIL_AUTH_TYPE",
|
|
7
|
+
authUsername = "EMAIL_AUTH_USERNAME",
|
|
8
|
+
authPassword = "EMAIL_AUTH_PASSWORD",
|
|
9
|
+
secure = "EMAIL_SECURE",
|
|
10
|
+
ignoreTLS = "EMAIL_IGNORE_TLS",
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default EmailConfig;
|
|
14
|
+
export {EmailConfig};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {DraxConfig} from "@drax/common-back";
|
|
2
|
+
import {EmailService} from '../services/EmailService.js'
|
|
3
|
+
import EmailConfig from "../config/EmailConfig";
|
|
4
|
+
import {TransportGmailConfig, TransportSmtpConfig} from "../interfaces/ITransportConfig";
|
|
5
|
+
|
|
6
|
+
class EmailServiceFactory {
|
|
7
|
+
private static service: EmailService;
|
|
8
|
+
|
|
9
|
+
public static get instance(): EmailService {
|
|
10
|
+
if (!EmailServiceFactory.service) {
|
|
11
|
+
const type = EmailServiceFactory.getType;
|
|
12
|
+
const options = EmailServiceFactory.getOptions;
|
|
13
|
+
EmailServiceFactory.service = new EmailService(type, options);
|
|
14
|
+
}
|
|
15
|
+
return EmailServiceFactory.service;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public static get getType() {
|
|
19
|
+
return DraxConfig.getOrLoad(EmailConfig.type)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static get getOptions() {
|
|
23
|
+
let options: TransportSmtpConfig | TransportGmailConfig;
|
|
24
|
+
switch(DraxConfig.getOrLoad(EmailConfig.type)){
|
|
25
|
+
case 'smtp':
|
|
26
|
+
options = {
|
|
27
|
+
host: DraxConfig.getOrLoad(EmailConfig.smtpHost),
|
|
28
|
+
port: DraxConfig.getOrLoad(EmailConfig.smtpPort),
|
|
29
|
+
secure: DraxConfig.getOrLoad(EmailConfig.secure),
|
|
30
|
+
ignoreTLS: DraxConfig.getOrLoad(EmailConfig.ignoreTLS),
|
|
31
|
+
auth: {
|
|
32
|
+
user: DraxConfig.getOrLoad(EmailConfig.authUsername),
|
|
33
|
+
pass: DraxConfig.getOrLoad(EmailConfig.authPassword),
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
case 'gmail':
|
|
38
|
+
options = {
|
|
39
|
+
auth: {
|
|
40
|
+
user: DraxConfig.getOrLoad(EmailConfig.authUsername),
|
|
41
|
+
pass: DraxConfig.getOrLoad(EmailConfig.authPassword),
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
default:
|
|
46
|
+
throw new Error(`Unsupported email service type: ${DraxConfig.getOrLoad(EmailConfig.type)}`)
|
|
47
|
+
}
|
|
48
|
+
return options;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default EmailServiceFactory
|
|
53
|
+
export {
|
|
54
|
+
EmailServiceFactory
|
|
55
|
+
}
|
|
56
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import EmailConfig from "./config/EmailConfig.js";
|
|
2
|
+
import EmailService from "./services/EmailService.js";
|
|
3
|
+
import EmailLayoutService from "./services/EmailLayoutService.js";
|
|
4
|
+
import type {IEmailLayout} from "./interfaces/IEmailLayout"
|
|
5
|
+
import type {TransportGmailConfig, TransportSmtpConfig} from "./interfaces/ITransportConfig"
|
|
6
|
+
|
|
7
|
+
export type {
|
|
8
|
+
IEmailLayout,
|
|
9
|
+
TransportGmailConfig,
|
|
10
|
+
TransportSmtpConfig,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
EmailConfig,
|
|
16
|
+
EmailService,
|
|
17
|
+
EmailLayoutService,
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
interface IEmailLayout{
|
|
2
|
+
|
|
3
|
+
bodyStyle?:string
|
|
4
|
+
maxWidth?: string
|
|
5
|
+
|
|
6
|
+
headerCentered?: boolean
|
|
7
|
+
headerBgColor?: string
|
|
8
|
+
headerColor?: string
|
|
9
|
+
|
|
10
|
+
headerImage?: string
|
|
11
|
+
headerImageStyle?: string
|
|
12
|
+
|
|
13
|
+
headerTitle?: string
|
|
14
|
+
headerTitleStyle?: string
|
|
15
|
+
|
|
16
|
+
headerLogo?: string
|
|
17
|
+
headerLogoStyle?: string
|
|
18
|
+
|
|
19
|
+
footerBgColor?: string
|
|
20
|
+
footerCopyright?: string
|
|
21
|
+
footerContent?: string
|
|
22
|
+
footerUnsubscribe?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type {IEmailLayout}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface TransportSmtpConfig {
|
|
2
|
+
host: string;
|
|
3
|
+
port: number;
|
|
4
|
+
secure: boolean;
|
|
5
|
+
ignoreTLS: boolean;
|
|
6
|
+
auth: {
|
|
7
|
+
type: 'login' | 'oauth2';
|
|
8
|
+
user: string;
|
|
9
|
+
pass: string;
|
|
10
|
+
} | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface TransportGmailConfig {
|
|
14
|
+
auth: {
|
|
15
|
+
user: string;
|
|
16
|
+
pass: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export type {TransportSmtpConfig, TransportGmailConfig}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {IEmailLayout} from "../interfaces/IEmailLayout";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class EmailLayoutService {
|
|
5
|
+
|
|
6
|
+
options: IEmailLayout
|
|
7
|
+
|
|
8
|
+
constructor(options: IEmailLayout = {}) {
|
|
9
|
+
const defaultOptions: IEmailLayout = {
|
|
10
|
+
bodyStyle: 'font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #fcfcfc;',
|
|
11
|
+
maxWidth: "800px",
|
|
12
|
+
headerCentered: true,
|
|
13
|
+
headerBgColor: "#f4f4f4",
|
|
14
|
+
headerColor: "#111111",
|
|
15
|
+
headerImage: "",
|
|
16
|
+
headerImageStyle: "width: auto; height: auto; display: block; margin: 0 auto;",
|
|
17
|
+
headerTitle: "",
|
|
18
|
+
headerTitleStyle: "font-size: 28px; color: #333333; margin: 10px; font-weight: 700; display: inline-block;",
|
|
19
|
+
headerLogo: "",
|
|
20
|
+
headerLogoStyle: "max-height: 50px; display: block; margin: 10px auto;",
|
|
21
|
+
footerBgColor: "#f4f4f4",
|
|
22
|
+
footerCopyright: "Your Company. All rights reserved.",
|
|
23
|
+
footerContent: "Default footer content",
|
|
24
|
+
footerUnsubscribe: "",
|
|
25
|
+
};
|
|
26
|
+
this.options = { ...defaultOptions, ...options };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get head() {
|
|
30
|
+
return `<head>
|
|
31
|
+
<meta charset="UTF-8">
|
|
32
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
33
|
+
</head>`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get header() {
|
|
37
|
+
let header = `<tr style="background-color: ${this.options.headerBgColor}">
|
|
38
|
+
<td style="${this.options.headerCentered ? 'text-align: center;' : ''} vertical-align: middle;">`
|
|
39
|
+
|
|
40
|
+
if (this.options.headerImage) {
|
|
41
|
+
header += `<img src="${this.options.headerImage}"
|
|
42
|
+
alt="${this.options.headerTitle}"
|
|
43
|
+
style="${this.options.headerImageStyle}"
|
|
44
|
+
/>`
|
|
45
|
+
} else if (this.options.headerLogo && this.options.headerTitle) {
|
|
46
|
+
header += `<table style="display: inline-table; text-align: left;">
|
|
47
|
+
<tr>
|
|
48
|
+
<td style="vertical-align: middle; padding: 10px;">
|
|
49
|
+
<img src="${this.options.headerLogo}"
|
|
50
|
+
alt="${this.options.headerTitle}"
|
|
51
|
+
style="${this.options.headerLogoStyle}"
|
|
52
|
+
/>
|
|
53
|
+
</td>
|
|
54
|
+
<td style="vertical-align: middle;">
|
|
55
|
+
<h1 style="${this.options.headerTitleStyle}">${this.options.headerTitle}</h1>
|
|
56
|
+
</td>
|
|
57
|
+
</tr>
|
|
58
|
+
</table>`
|
|
59
|
+
} else if (this.options.headerTitle) {
|
|
60
|
+
header += `<h1 style="${this.options.headerTitleStyle}"
|
|
61
|
+
>
|
|
62
|
+
${this.options.headerTitle}
|
|
63
|
+
</h1>`
|
|
64
|
+
}else if (this.options.headerLogo) {
|
|
65
|
+
header += `<img src="${this.options.headerLogo}" alt="${this.options.headerTitle}"
|
|
66
|
+
style="${this.options.headerLogoStyle}"
|
|
67
|
+
/>`
|
|
68
|
+
}
|
|
69
|
+
header += `</td></tr>`
|
|
70
|
+
return header
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get footer() {
|
|
74
|
+
let footer = `<tr style=" ${this.options.footerBgColor ? 'background-color:'+this.options.footerBgColor : ''}">
|
|
75
|
+
<td style="text-align: center; padding: 20px; font-size: 12px; color: #888888;">`
|
|
76
|
+
|
|
77
|
+
if (this.options.footerCopyright) {
|
|
78
|
+
footer += ` <p style="margin: 0;">© ${new Date().getFullYear()} ${this.options.footerCopyright}.</p>`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (this.options.footerContent) {
|
|
82
|
+
footer += `${this.options.footerContent}`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (this.options.footerUnsubscribe) {
|
|
86
|
+
footer += `${this.options.footerUnsubscribe}`
|
|
87
|
+
//footer += `<p style="margin: 5px 0 0 0;">Si no deseas recibir nuestros correos, <a href="${this.options.unsubscribeLink}" target="_blank" style="color: #555555; text-decoration: none;">haz clic aquí</a> para darte de baja.</p>`
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
footer += `</td></tr>`
|
|
91
|
+
|
|
92
|
+
return footer
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
html(body: string) {
|
|
96
|
+
return `<!DOCTYPE html>
|
|
97
|
+
<html lang="es">
|
|
98
|
+
${this.head}
|
|
99
|
+
<body style="${this.options.bodyStyle}">
|
|
100
|
+
<table role="presentation"
|
|
101
|
+
style="border-spacing: 0; margin: 0 auto; width: 100%; max-width: ${this.options.maxWidth}"
|
|
102
|
+
>
|
|
103
|
+
|
|
104
|
+
<!-- HEADER -->
|
|
105
|
+
${this.header}
|
|
106
|
+
<!-- BODY -->
|
|
107
|
+
<tr>
|
|
108
|
+
<td style="padding: 20px; background-color: #ffffff;">
|
|
109
|
+
${body}
|
|
110
|
+
</td>
|
|
111
|
+
</tr>
|
|
112
|
+
<!-- FOOTER -->
|
|
113
|
+
${this.footer}
|
|
114
|
+
</table>
|
|
115
|
+
</body>`
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
export default EmailLayoutService
|
|
123
|
+
export {EmailLayoutService}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import nodemailer from 'nodemailer';
|
|
2
|
+
|
|
3
|
+
import type {Transporter, SendMailOptions} from "nodemailer";
|
|
4
|
+
import type {TransportGmailConfig, TransportSmtpConfig} from "../interfaces/ITransportConfig";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class EmailService {
|
|
8
|
+
|
|
9
|
+
transporter: Transporter;
|
|
10
|
+
transporterConfig : TransportGmailConfig | TransportSmtpConfig;
|
|
11
|
+
|
|
12
|
+
constructor(type: 'smtp' | 'gmail', config: TransportGmailConfig | TransportSmtpConfig) {
|
|
13
|
+
this.transporterConfig = config;
|
|
14
|
+
switch (type) {
|
|
15
|
+
case 'smtp':
|
|
16
|
+
this.prepareTransportSmtp(config as TransportSmtpConfig);
|
|
17
|
+
break;
|
|
18
|
+
case 'gmail':
|
|
19
|
+
this.prepareTransportGmail(config as TransportGmailConfig);
|
|
20
|
+
break;
|
|
21
|
+
default:
|
|
22
|
+
throw new Error(`Unsupported email service type: ${type}`)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
prepareTransportGmail(config: TransportGmailConfig): any {
|
|
28
|
+
this.transporter = nodemailer.createTransport({
|
|
29
|
+
service: 'gmail',
|
|
30
|
+
auth: {
|
|
31
|
+
user: config.auth.user,
|
|
32
|
+
pass: config.auth.pass
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
prepareTransportSmtp(config: TransportSmtpConfig): any {
|
|
39
|
+
this.transporter = nodemailer.createTransport({
|
|
40
|
+
host: config.host,
|
|
41
|
+
port: config.port,
|
|
42
|
+
secure: config.secure,
|
|
43
|
+
ignoreTLS: config.ignoreTLS,
|
|
44
|
+
auth: (config.auth.user && config.auth.pass) ? {
|
|
45
|
+
type: config.auth.type ? config.auth.type : 'login',
|
|
46
|
+
user: config.auth.user,
|
|
47
|
+
pass: config.auth.pass
|
|
48
|
+
} : null
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
sendEmail(options: SendMailOptions): Promise<any> {
|
|
54
|
+
|
|
55
|
+
if(!this.transporter){
|
|
56
|
+
throw new Error('Transporter is not initialized')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if(!options.to){
|
|
60
|
+
throw new Error('No recipient provided')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if(!options.html && !options.text){
|
|
64
|
+
throw new Error('Either HTML or TEXT content is required')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if(!options.subject){
|
|
68
|
+
throw new Error('No subject provided')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if(!options.from){
|
|
72
|
+
options.from = this.transporterConfig.auth.user
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return this.transporter.sendMail(options);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export default EmailService
|
|
81
|
+
export {EmailService}
|