@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/dist/index.js CHANGED
@@ -1,318 +1,286 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
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
- // src/Mailable.ts
5
- var Mailable = class {
6
- static {
7
- __name(this, "Mailable");
8
- }
9
- toAddress;
10
- ccAddresses;
11
- bccAddresses;
12
- subjectText;
13
- htmlContent;
14
- textContent;
15
- viewPath;
16
- viewData;
17
- attachmentsList;
18
- to(address) {
19
- this.toAddress = address;
20
- return this;
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
- // src/Mailer.ts
75
- var Mailer = class {
76
- static {
77
- __name(this, "Mailer");
78
- }
79
- driver;
80
- edgeRenderer;
81
- constructor(driver, edgeRenderer) {
82
- this.driver = driver;
83
- this.edgeRenderer = edgeRenderer;
84
- }
85
- async send(mailable) {
86
- await mailable.build();
87
- const options = mailable.getMessageOptions();
88
- if (options.viewPath && !options.html) {
89
- options.html = await this.edgeRenderer(options.viewPath, options.viewData || {});
90
- }
91
- try {
92
- return this.driver.send(options);
93
- } catch (error) {
94
- return;
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
- // src/Drivers/SESDriver.ts
100
- import nodemailer from "nodemailer";
101
- import { SendEmailCommand, SESv2Client } from "@aws-sdk/client-sesv2";
48
+ //#endregion
49
+ //#region src/Drivers/SESDriver.ts
102
50
  var SESDriver = class {
103
- static {
104
- __name(this, "SESDriver");
105
- }
106
- transporter;
107
- constructor(config) {
108
- const sesClient = new SESv2Client({
109
- region: config.region,
110
- credentials: {
111
- accessKeyId: config.key,
112
- sessionToken: config.token,
113
- secretAccessKey: config.secret
114
- }
115
- });
116
- this.transporter = nodemailer.createTransport({
117
- SES: {
118
- sesClient,
119
- SendEmailCommand
120
- },
121
- maxConnections: config.maxConnections,
122
- sendingRate: config.sendingRate
123
- });
124
- }
125
- async send(options) {
126
- return await this.transporter.sendMail({
127
- to: options.to,
128
- cc: options.cc,
129
- bcc: options.bcc,
130
- subject: options.subject,
131
- html: options.html,
132
- text: options.text,
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
- // src/Drivers/SMTPDriver.ts
139
- import nodemailer2 from "nodemailer";
83
+ //#endregion
84
+ //#region src/Drivers/SMTPDriver.ts
140
85
  var SMTPDriver = class {
141
- static {
142
- __name(this, "SMTPDriver");
143
- }
144
- transporter;
145
- constructor(config) {
146
- this.transporter = nodemailer2.createTransport({
147
- host: config.host,
148
- port: config.port,
149
- secure: config.port === 465,
150
- auth: {
151
- user: config.auth.user,
152
- pass: config.auth.pass
153
- }
154
- });
155
- }
156
- async send(options) {
157
- return await this.transporter.sendMail({
158
- to: options.to,
159
- cc: options.cc,
160
- bcc: options.bcc,
161
- subject: options.subject,
162
- html: options.html,
163
- text: options.text,
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
- // src/Drivers/LOGDriver.ts
170
- import nodemailer3 from "nodemailer";
171
- import Stream from "stream";
172
- var LOGDriver = class {
173
- static {
174
- __name(this, "LOGDriver");
175
- }
176
- transporter;
177
- constructor(_config) {
178
- this.transporter = nodemailer3.createTransport({
179
- streamTransport: true,
180
- newline: "unix"
181
- });
182
- }
183
- async send(options) {
184
- this.transporter.sendMail(options, (err, info) => {
185
- if (err)
186
- throw err;
187
- console.log(info.envelope);
188
- console.log(info.messageId);
189
- info.message instanceof Stream.Readable && info.message.pipe(process.stdout);
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
- // src/Drivers/SendMailDriver.ts
195
- import nodemailer4 from "nodemailer";
196
- var SendMailDriver = class {
197
- static {
198
- __name(this, "SendMailDriver");
199
- }
200
- transporter;
201
- constructor(config) {
202
- this.transporter = nodemailer4.createTransport({
203
- sendmail: true,
204
- path: config.path
205
- });
206
- }
207
- async send(options) {
208
- return await this.transporter.sendMail({
209
- to: options.to,
210
- cc: options.cc,
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
- // src/Service.ts
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
- static {
223
- __name(this, "Service");
224
- }
225
- /**
226
- * Initializes the mailer service with the given application instance
227
- *
228
- * @param app
229
- * @returns
230
- */
231
- static init(app) {
232
- const view = app.make("view");
233
- const config = app.make("config");
234
- const mailConfig = {
235
- /**
236
- * SMTP configuration with fallback defaults
237
- */
238
- smtp: {
239
- host: config.get("mail.mailers.smtp.host", "smtp.mailtrap.io"),
240
- port: Number(config.get("mail.mailers.smtp.port", 2525)),
241
- auth: {
242
- user: config.get("mail.mailers.smtp.username", ""),
243
- pass: config.get("mail.mailers.smtp.password", "")
244
- },
245
- opportunisticTLS: config.get("mail.mail\u6C14\u7684mailers.smtp.encryption") === "tls",
246
- connectionTimeout: config.get("mail.mailers.smtp.timeout"),
247
- debug: false
248
- },
249
- /**
250
- * SES configuration with fallback defaults
251
- */
252
- ses: {
253
- key: config.get("services.ses.key", ""),
254
- token: config.get("services.ses.token", ""),
255
- secret: config.get("services.ses.secret", ""),
256
- region: config.get("services.ses.region", "us-east-1"),
257
- maxConnections: config.get("mail.mailers.ses.connections", 10),
258
- sendingRate: config.get("mail.mailers.ses.rate", 5)
259
- },
260
- /**
261
- * Sendmail configuration with fallback default path
262
- */
263
- sendmail: {
264
- path: config.get("mail.mailers.sendmail.path", "sendmail")
265
- }
266
- };
267
- const driver = {
268
- /**
269
- * SES driver factory
270
- * @returns
271
- */
272
- ses: () => new SESDriver(mailConfig.ses),
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
- // src/Providers/MailServiceProvider.ts
294
- import { ServiceProvider } from "@h3ravel/core";
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
- static {
297
- __name(this, "MailServiceProvider");
298
- }
299
- static priority = 990;
300
- register() {
301
- this.app.singleton(Mailer, () => {
302
- return Service.init(this.app);
303
- });
304
- }
305
- boot() {
306
- }
307
- };
308
- export {
309
- LOGDriver,
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