@h3ravel/mail 10.0.0 → 11.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/README.md +13 -1
- package/dist/index.cjs +291 -331
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +126 -116
- package/dist/index.d.ts +126 -116
- package/dist/index.js +265 -297
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,318 +1,286 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import nodemailer from "nodemailer";
|
|
2
|
+
import Stream from "stream";
|
|
3
|
+
import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2";
|
|
4
|
+
import { ServiceProvider } from "@h3ravel/core";
|
|
3
5
|
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
cc(...addresses) {
|
|
23
|
-
this.ccAddresses = addresses;
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
26
|
-
bcc(...addresses) {
|
|
27
|
-
this.bccAddresses = addresses;
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
subject(subject) {
|
|
31
|
-
this.subjectText = subject;
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
html(html) {
|
|
35
|
-
this.htmlContent = html;
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
text(text) {
|
|
39
|
-
this.textContent = text;
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
|
-
view(path, data = {}) {
|
|
43
|
-
this.viewPath = path;
|
|
44
|
-
this.viewData = data;
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
|
-
attach(filename, filePath) {
|
|
48
|
-
if (!this.attachmentsList)
|
|
49
|
-
this.attachmentsList = [];
|
|
50
|
-
this.attachmentsList.push({
|
|
51
|
-
filename,
|
|
52
|
-
path: filePath
|
|
53
|
-
});
|
|
54
|
-
return this;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Called internally by Mailer
|
|
58
|
-
*/
|
|
59
|
-
getMessageOptions() {
|
|
60
|
-
return {
|
|
61
|
-
to: this.toAddress,
|
|
62
|
-
cc: this.ccAddresses,
|
|
63
|
-
bcc: this.bccAddresses,
|
|
64
|
-
subject: this.subjectText,
|
|
65
|
-
html: this.htmlContent,
|
|
66
|
-
text: this.textContent,
|
|
67
|
-
viewPath: this.viewPath,
|
|
68
|
-
viewData: this.viewData,
|
|
69
|
-
attachments: this.attachmentsList
|
|
70
|
-
};
|
|
71
|
-
}
|
|
6
|
+
//#region src/Drivers/LOGDriver.ts
|
|
7
|
+
var LOGDriver = class {
|
|
8
|
+
transporter;
|
|
9
|
+
constructor(_config) {
|
|
10
|
+
this.transporter = nodemailer.createTransport({
|
|
11
|
+
streamTransport: true,
|
|
12
|
+
newline: "unix"
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async send(options) {
|
|
16
|
+
this.transporter.sendMail(options, (err, info) => {
|
|
17
|
+
if (err) throw err;
|
|
18
|
+
console.log(info.envelope);
|
|
19
|
+
console.log(info.messageId);
|
|
20
|
+
info.message instanceof Stream.Readable && info.message.pipe(process.stdout);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
72
23
|
};
|
|
73
24
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
}
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/Drivers/SendMailDriver.ts
|
|
27
|
+
var SendMailDriver = class {
|
|
28
|
+
transporter;
|
|
29
|
+
constructor(config) {
|
|
30
|
+
this.transporter = nodemailer.createTransport({
|
|
31
|
+
sendmail: true,
|
|
32
|
+
path: config.path
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async send(options) {
|
|
36
|
+
return await this.transporter.sendMail({
|
|
37
|
+
to: options.to,
|
|
38
|
+
cc: options.cc,
|
|
39
|
+
bcc: options.bcc,
|
|
40
|
+
subject: options.subject,
|
|
41
|
+
html: options.html,
|
|
42
|
+
text: options.text,
|
|
43
|
+
attachments: options.attachments
|
|
44
|
+
});
|
|
45
|
+
}
|
|
97
46
|
};
|
|
98
47
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
import { SendEmailCommand, SESv2Client } from "@aws-sdk/client-sesv2";
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/Drivers/SESDriver.ts
|
|
102
50
|
var SESDriver = class {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
attachments: options.attachments
|
|
134
|
-
});
|
|
135
|
-
}
|
|
51
|
+
transporter;
|
|
52
|
+
constructor(config) {
|
|
53
|
+
const sesClient = new SESv2Client({
|
|
54
|
+
region: config.region,
|
|
55
|
+
credentials: {
|
|
56
|
+
accessKeyId: config.key,
|
|
57
|
+
sessionToken: config.token,
|
|
58
|
+
secretAccessKey: config.secret
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
this.transporter = nodemailer.createTransport({
|
|
62
|
+
SES: {
|
|
63
|
+
sesClient,
|
|
64
|
+
SendEmailCommand
|
|
65
|
+
},
|
|
66
|
+
maxConnections: config.maxConnections,
|
|
67
|
+
sendingRate: config.sendingRate
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async send(options) {
|
|
71
|
+
return await this.transporter.sendMail({
|
|
72
|
+
to: options.to,
|
|
73
|
+
cc: options.cc,
|
|
74
|
+
bcc: options.bcc,
|
|
75
|
+
subject: options.subject,
|
|
76
|
+
html: options.html,
|
|
77
|
+
text: options.text,
|
|
78
|
+
attachments: options.attachments
|
|
79
|
+
});
|
|
80
|
+
}
|
|
136
81
|
};
|
|
137
82
|
|
|
138
|
-
|
|
139
|
-
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/Drivers/SMTPDriver.ts
|
|
140
85
|
var SMTPDriver = class {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
attachments: options.attachments
|
|
165
|
-
});
|
|
166
|
-
}
|
|
86
|
+
transporter;
|
|
87
|
+
constructor(config) {
|
|
88
|
+
this.transporter = nodemailer.createTransport({
|
|
89
|
+
host: config.host,
|
|
90
|
+
port: config.port,
|
|
91
|
+
secure: config.port === 465,
|
|
92
|
+
auth: {
|
|
93
|
+
user: config.auth.user,
|
|
94
|
+
pass: config.auth.pass
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async send(options) {
|
|
99
|
+
return await this.transporter.sendMail({
|
|
100
|
+
to: options.to,
|
|
101
|
+
cc: options.cc,
|
|
102
|
+
bcc: options.bcc,
|
|
103
|
+
subject: options.subject,
|
|
104
|
+
html: options.html,
|
|
105
|
+
text: options.text,
|
|
106
|
+
attachments: options.attachments
|
|
107
|
+
});
|
|
108
|
+
}
|
|
167
109
|
};
|
|
168
110
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/Mailable.ts
|
|
113
|
+
var Mailable = class {
|
|
114
|
+
toAddress;
|
|
115
|
+
ccAddresses;
|
|
116
|
+
bccAddresses;
|
|
117
|
+
subjectText;
|
|
118
|
+
htmlContent;
|
|
119
|
+
textContent;
|
|
120
|
+
viewPath;
|
|
121
|
+
viewData;
|
|
122
|
+
attachmentsList;
|
|
123
|
+
to(address) {
|
|
124
|
+
this.toAddress = address;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
cc(...addresses) {
|
|
128
|
+
this.ccAddresses = addresses;
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
bcc(...addresses) {
|
|
132
|
+
this.bccAddresses = addresses;
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
subject(subject) {
|
|
136
|
+
this.subjectText = subject;
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
html(html) {
|
|
140
|
+
this.htmlContent = html;
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
text(text) {
|
|
144
|
+
this.textContent = text;
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
view(path, data = {}) {
|
|
148
|
+
this.viewPath = path;
|
|
149
|
+
this.viewData = data;
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
attach(filename, filePath) {
|
|
153
|
+
if (!this.attachmentsList) this.attachmentsList = [];
|
|
154
|
+
this.attachmentsList.push({
|
|
155
|
+
filename,
|
|
156
|
+
path: filePath
|
|
157
|
+
});
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Called internally by Mailer
|
|
162
|
+
*/
|
|
163
|
+
getMessageOptions() {
|
|
164
|
+
return {
|
|
165
|
+
to: this.toAddress,
|
|
166
|
+
cc: this.ccAddresses,
|
|
167
|
+
bcc: this.bccAddresses,
|
|
168
|
+
subject: this.subjectText,
|
|
169
|
+
html: this.htmlContent,
|
|
170
|
+
text: this.textContent,
|
|
171
|
+
viewPath: this.viewPath,
|
|
172
|
+
viewData: this.viewData,
|
|
173
|
+
attachments: this.attachmentsList
|
|
174
|
+
};
|
|
175
|
+
}
|
|
192
176
|
};
|
|
193
177
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
var
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
bcc: options.bcc,
|
|
212
|
-
subject: options.subject,
|
|
213
|
-
html: options.html,
|
|
214
|
-
text: options.text,
|
|
215
|
-
attachments: options.attachments
|
|
216
|
-
});
|
|
217
|
-
}
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/Mailer.ts
|
|
180
|
+
var Mailer = class {
|
|
181
|
+
constructor(driver, edgeRenderer) {
|
|
182
|
+
this.driver = driver;
|
|
183
|
+
this.edgeRenderer = edgeRenderer;
|
|
184
|
+
}
|
|
185
|
+
async send(mailable) {
|
|
186
|
+
await mailable.build();
|
|
187
|
+
const options = mailable.getMessageOptions();
|
|
188
|
+
if (options.viewPath && !options.html) options.html = await this.edgeRenderer(options.viewPath, options.viewData || {});
|
|
189
|
+
try {
|
|
190
|
+
return this.driver.send(options);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
218
195
|
};
|
|
219
196
|
|
|
220
|
-
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/Service.ts
|
|
199
|
+
/**
|
|
200
|
+
* Service class to initialize and configure the mailer service
|
|
201
|
+
*/
|
|
221
202
|
var Service = class {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
* SMTP driver factory
|
|
275
|
-
* @returns
|
|
276
|
-
*/
|
|
277
|
-
smtp: () => new SMTPDriver(mailConfig.smtp),
|
|
278
|
-
/**
|
|
279
|
-
* LOG driver factory for debugging
|
|
280
|
-
* @returns
|
|
281
|
-
*/
|
|
282
|
-
log: () => new LOGDriver(mailConfig.smtp),
|
|
283
|
-
/**
|
|
284
|
-
* Sendmail driver factory
|
|
285
|
-
* @returns
|
|
286
|
-
*/
|
|
287
|
-
sendmail: () => new SendMailDriver(mailConfig.sendmail)
|
|
288
|
-
};
|
|
289
|
-
return new Mailer((driver[config.get("mail.default")] ?? driver.smtp)(), async (viewPath, data) => await view(viewPath, data));
|
|
290
|
-
}
|
|
203
|
+
/**
|
|
204
|
+
* Initializes the mailer service with the given application instance
|
|
205
|
+
*
|
|
206
|
+
* @param app
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
|
+
static init(app) {
|
|
210
|
+
/**
|
|
211
|
+
* Resolve the view and config services from the container
|
|
212
|
+
*/
|
|
213
|
+
const view = app.make("view");
|
|
214
|
+
const config = app.make("config");
|
|
215
|
+
/**
|
|
216
|
+
* Configure mailer settings for different drivers
|
|
217
|
+
*/
|
|
218
|
+
const mailConfig = {
|
|
219
|
+
smtp: {
|
|
220
|
+
host: config.get("mail.mailers.smtp.host", "smtp.mailtrap.io"),
|
|
221
|
+
port: Number(config.get("mail.mailers.smtp.port", 2525)),
|
|
222
|
+
auth: {
|
|
223
|
+
user: config.get("mail.mailers.smtp.username", ""),
|
|
224
|
+
pass: config.get("mail.mailers.smtp.password", "")
|
|
225
|
+
},
|
|
226
|
+
opportunisticTLS: config.get("mail.mail气的mailers.smtp.encryption") === "tls",
|
|
227
|
+
connectionTimeout: config.get("mail.mailers.smtp.timeout"),
|
|
228
|
+
debug: false
|
|
229
|
+
},
|
|
230
|
+
ses: {
|
|
231
|
+
key: config.get("services.ses.key", ""),
|
|
232
|
+
token: config.get("services.ses.token", ""),
|
|
233
|
+
secret: config.get("services.ses.secret", ""),
|
|
234
|
+
region: config.get("services.ses.region", "us-east-1"),
|
|
235
|
+
maxConnections: config.get("mail.mailers.ses.connections", 10),
|
|
236
|
+
sendingRate: config.get("mail.mailers.ses.rate", 5)
|
|
237
|
+
},
|
|
238
|
+
sendmail: { path: config.get("mail.mailers.sendmail.path", "sendmail") }
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* Define available mail drivers
|
|
242
|
+
*/
|
|
243
|
+
const driver = {
|
|
244
|
+
ses: () => new SESDriver(mailConfig.ses),
|
|
245
|
+
smtp: () => new SMTPDriver(mailConfig.smtp),
|
|
246
|
+
log: () => new LOGDriver(mailConfig.smtp),
|
|
247
|
+
sendmail: () => new SendMailDriver(mailConfig.sendmail)
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* Initialize Mailer with the selected driver (default to SMTP if not specified)
|
|
251
|
+
* and a view rendering function
|
|
252
|
+
*/
|
|
253
|
+
return new Mailer((driver[config.get("mail.default")] ?? driver.smtp)(), async (viewPath, data) => await view(viewPath, data));
|
|
254
|
+
}
|
|
291
255
|
};
|
|
292
256
|
|
|
293
|
-
|
|
294
|
-
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/Providers/MailServiceProvider.ts
|
|
259
|
+
/**
|
|
260
|
+
* Mail delivery setup.
|
|
261
|
+
*
|
|
262
|
+
* Bind Mailer service.
|
|
263
|
+
* Load mail drivers (SMTP, SES, etc.).
|
|
264
|
+
* Register Mail facade.
|
|
265
|
+
*
|
|
266
|
+
*/
|
|
295
267
|
var MailServiceProvider = class extends ServiceProvider {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
MailServiceProvider,
|
|
311
|
-
Mailable,
|
|
312
|
-
Mailer,
|
|
313
|
-
SESDriver,
|
|
314
|
-
SMTPDriver,
|
|
315
|
-
SendMailDriver,
|
|
316
|
-
Service
|
|
268
|
+
static priority = 990;
|
|
269
|
+
register() {
|
|
270
|
+
/**
|
|
271
|
+
* Register Mailer instance
|
|
272
|
+
*/
|
|
273
|
+
this.app.singleton(Mailer, () => {
|
|
274
|
+
return Service.init(this.app);
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
boot() {
|
|
278
|
+
/**
|
|
279
|
+
* Add logic here for global mail "from" address and others
|
|
280
|
+
*/
|
|
281
|
+
}
|
|
317
282
|
};
|
|
283
|
+
|
|
284
|
+
//#endregion
|
|
285
|
+
export { LOGDriver, MailServiceProvider, Mailable, Mailer, SESDriver, SMTPDriver, SendMailDriver, Service };
|
|
318
286
|
//# sourceMappingURL=index.js.map
|