@impruthvi/nodemail 0.3.0 → 0.4.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/README.md +145 -7
  3. package/dist/core/MailFacade.d.ts +1 -0
  4. package/dist/core/MailFacade.d.ts.map +1 -1
  5. package/dist/core/MailFacade.js +4 -0
  6. package/dist/core/MailFacade.js.map +1 -1
  7. package/dist/core/MailManager.d.ts +11 -2
  8. package/dist/core/MailManager.d.ts.map +1 -1
  9. package/dist/core/MailManager.js +79 -1
  10. package/dist/core/MailManager.js.map +1 -1
  11. package/dist/core/Mailable.d.ts +12 -2
  12. package/dist/core/Mailable.d.ts.map +1 -1
  13. package/dist/core/Mailable.js +44 -2
  14. package/dist/core/Mailable.js.map +1 -1
  15. package/dist/index.d.ts +3 -1
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +6 -2
  18. package/dist/index.js.map +1 -1
  19. package/dist/templates/EjsEngine.d.ts +14 -0
  20. package/dist/templates/EjsEngine.d.ts.map +1 -0
  21. package/dist/templates/EjsEngine.js +59 -0
  22. package/dist/templates/EjsEngine.js.map +1 -0
  23. package/dist/templates/HandlebarsEngine.d.ts +16 -0
  24. package/dist/templates/HandlebarsEngine.d.ts.map +1 -0
  25. package/dist/templates/HandlebarsEngine.js +65 -0
  26. package/dist/templates/HandlebarsEngine.js.map +1 -0
  27. package/dist/templates/PugEngine.d.ts +15 -0
  28. package/dist/templates/PugEngine.d.ts.map +1 -0
  29. package/dist/templates/PugEngine.js +70 -0
  30. package/dist/templates/PugEngine.js.map +1 -0
  31. package/dist/templates/TemplateEngine.d.ts +12 -0
  32. package/dist/templates/TemplateEngine.d.ts.map +1 -0
  33. package/dist/templates/TemplateEngine.js +3 -0
  34. package/dist/templates/TemplateEngine.js.map +1 -0
  35. package/dist/templates/index.d.ts +5 -0
  36. package/dist/templates/index.d.ts.map +1 -0
  37. package/dist/templates/index.js +10 -0
  38. package/dist/templates/index.js.map +1 -0
  39. package/dist/types/index.d.ts +9 -0
  40. package/dist/types/index.d.ts.map +1 -1
  41. package/examples/mailgun.ts +1 -1
  42. package/examples/postmark.ts +1 -1
  43. package/examples/resend.ts +1 -1
  44. package/examples/views/invoice.ejs +26 -0
  45. package/examples/views/notification.pug +14 -0
  46. package/examples/views/welcome.hbs +15 -0
  47. package/examples/with-ejs.ts +63 -0
  48. package/examples/with-handlebars.ts +60 -0
  49. package/examples/with-mailable-templates.ts +146 -0
  50. package/examples/with-pug.ts +66 -0
  51. package/package.json +16 -4
@@ -0,0 +1,146 @@
1
+ import { Mailable, Mail } from '../src';
2
+ import { config } from 'dotenv';
3
+
4
+ config();
5
+
6
+ /**
7
+ * Example: Using Mailable class with templates
8
+ *
9
+ * This demonstrates how to create reusable email classes
10
+ * that use template engines for rendering
11
+ */
12
+
13
+ class WelcomeEmail extends Mailable {
14
+ constructor(
15
+ private user: { name: string; email: string },
16
+ private appName: string
17
+ ) {
18
+ super();
19
+ }
20
+
21
+ build() {
22
+ return this.subject(`Welcome to ${this.appName}!`)
23
+ .view('welcome', {
24
+ name: this.user.name,
25
+ email: this.user.email,
26
+ appName: this.appName,
27
+ joinDate: new Date().toLocaleDateString(),
28
+ });
29
+ }
30
+ }
31
+
32
+ class InvoiceEmail extends Mailable {
33
+ constructor(
34
+ private invoice: {
35
+ number: string;
36
+ customer: string;
37
+ items: Array<{ name: string; quantity: number; price: number }>;
38
+ total: number;
39
+ }
40
+ ) {
41
+ super();
42
+ }
43
+
44
+ build() {
45
+ return this.subject(`Invoice #${this.invoice.number}`)
46
+ .view('invoice', {
47
+ invoiceNumber: this.invoice.number,
48
+ customerName: this.invoice.customer,
49
+ items: this.invoice.items,
50
+ total: this.invoice.total,
51
+ });
52
+ }
53
+ }
54
+
55
+ async function main() {
56
+ // Configure with Handlebars for welcome email
57
+ Mail.configure({
58
+ default: 'smtp',
59
+ from: {
60
+ address: process.env.MAIL_FROM_ADDRESS || 'noreply@example.com',
61
+ name: process.env.MAIL_FROM_NAME || 'Example App',
62
+ },
63
+ mailers: {
64
+ smtp: {
65
+ driver: 'smtp',
66
+ host: process.env.MAIL_HOST || 'smtp.ethereal.email',
67
+ port: parseInt(process.env.MAIL_PORT || '587'),
68
+ auth: {
69
+ user: process.env.MAIL_USERNAME || '',
70
+ pass: process.env.MAIL_PASSWORD || '',
71
+ },
72
+ },
73
+ },
74
+ templates: {
75
+ engine: 'handlebars',
76
+ viewsPath: './examples/views',
77
+ cache: true,
78
+ },
79
+ });
80
+
81
+ try {
82
+ // Method 1: Laravel-style - Mail.to().send(mailable)
83
+ console.log('šŸ“§ Method 1: Sending welcome email (Laravel-style)...');
84
+ const welcomeEmail = new WelcomeEmail(
85
+ { name: 'John Doe', email: 'john@example.com' },
86
+ 'NodeMail'
87
+ );
88
+
89
+ const response1 = await Mail.to('john@example.com').send(welcomeEmail);
90
+
91
+ console.log('āœ… Welcome email sent!');
92
+ console.log('Response:', response1);
93
+
94
+ // Switch to EJS for invoice email
95
+ Mail.configure({
96
+ default: 'smtp',
97
+ from: {
98
+ address: process.env.MAIL_FROM_ADDRESS || 'noreply@example.com',
99
+ name: process.env.MAIL_FROM_NAME || 'Example App',
100
+ },
101
+ mailers: {
102
+ smtp: {
103
+ driver: 'smtp',
104
+ host: process.env.MAIL_HOST || 'smtp.ethereal.email',
105
+ port: parseInt(process.env.MAIL_PORT || '587'),
106
+ auth: {
107
+ user: process.env.MAIL_USERNAME || '',
108
+ pass: process.env.MAIL_PASSWORD || '',
109
+ },
110
+ },
111
+ },
112
+ templates: {
113
+ engine: 'ejs',
114
+ viewsPath: './examples/views',
115
+ cache: true,
116
+ },
117
+ });
118
+
119
+ // Method 2: Direct mailable send
120
+ console.log('\nšŸ“§ Method 2: Sending invoice email (Direct)...');
121
+ const invoiceEmail = new InvoiceEmail({
122
+ number: 'INV-2025-001',
123
+ customer: 'Jane Smith',
124
+ items: [
125
+ { name: 'Pro Plan', quantity: 1, price: 99.99 },
126
+ { name: 'Extra Storage', quantity: 2, price: 19.99 },
127
+ ],
128
+ total: 139.97,
129
+ });
130
+
131
+ // Set mail manager and send directly
132
+ invoiceEmail.setMailManager(Mail['getInstance']());
133
+ const response2 = await invoiceEmail.to('jane@example.com').send();
134
+
135
+ console.log('āœ… Invoice email sent!');
136
+ console.log('Response:', response2);
137
+
138
+ console.log('\nšŸŽ‰ Both methods work perfectly!');
139
+ console.log('✨ Method 1 (Recommended): Mail.to().send(mailable)');
140
+ console.log('✨ Method 2 (Alternative): mailable.to().send()');
141
+ } catch (error) {
142
+ console.error('āŒ Failed to send email:', error);
143
+ }
144
+ }
145
+
146
+ void main();
@@ -0,0 +1,66 @@
1
+ import { Mail } from '../src';
2
+ import { config } from 'dotenv';
3
+
4
+ config();
5
+
6
+ /**
7
+ * Example: Using Pug templates
8
+ *
9
+ * Install dependencies:
10
+ * npm install pug
11
+ */
12
+
13
+ async function main() {
14
+ // Configure Mail with Pug template engine
15
+ Mail.configure({
16
+ default: 'smtp',
17
+ from: {
18
+ address: process.env.MAIL_FROM_ADDRESS || 'noreply@example.com',
19
+ name: process.env.MAIL_FROM_NAME || 'Example App',
20
+ },
21
+ mailers: {
22
+ smtp: {
23
+ driver: 'smtp',
24
+ host: process.env.MAIL_HOST || 'smtp.ethereal.email',
25
+ port: parseInt(process.env.MAIL_PORT || '587'),
26
+ auth: {
27
+ user: process.env.MAIL_USERNAME || '',
28
+ pass: process.env.MAIL_PASSWORD || '',
29
+ },
30
+ },
31
+ },
32
+ templates: {
33
+ engine: 'pug',
34
+ viewsPath: './examples/views',
35
+ extension: '.pug',
36
+ cache: true,
37
+ },
38
+ });
39
+
40
+ try {
41
+ // Send email using Pug template
42
+ const response = await Mail.to('user@example.com')
43
+ .subject('Account Notification')
44
+ .template('notification')
45
+ .data({
46
+ title: 'Account Notification',
47
+ message: 'Important Account Update',
48
+ username: 'Alex Johnson',
49
+ urgent: true,
50
+ items: [
51
+ 'Password changed successfully',
52
+ 'New device login detected',
53
+ 'Security settings updated',
54
+ ],
55
+ timestamp: new Date().toLocaleString(),
56
+ })
57
+ .send();
58
+
59
+ console.log('āœ… Email sent successfully with Pug template!');
60
+ console.log('Response:', response);
61
+ } catch (error) {
62
+ console.error('āŒ Failed to send email:', error);
63
+ }
64
+ }
65
+
66
+ void main();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@impruthvi/nodemail",
3
- "version": "0.3.0",
4
- "description": "A unified mail service for Node.js/TypeScript with Laravel-like simplicity. Support for multiple providers (SMTP, SendGrid, AWS SES, Mailgun, Resend, Postmark) - just change environment variables, no code changes needed.",
3
+ "version": "0.4.0",
4
+ "description": "A unified mail service for Node.js/TypeScript with Laravel-like simplicity. Support for multiple providers (SMTP, SendGrid, AWS SES, Mailgun, Resend, Postmark) and template engines (Handlebars, EJS, Pug) - just change environment variables, no code changes needed.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
@@ -11,8 +11,8 @@
11
11
  "test": "jest",
12
12
  "test:watch": "jest --watch",
13
13
  "test:coverage": "jest --coverage",
14
- "lint": "eslint src/**/*.ts",
15
- "lint:fix": "eslint src/**/*.ts --fix",
14
+ "lint": "eslint \"src/**/*.ts\" \"examples/**/*.ts\" \"tests/**/*.ts\"",
15
+ "lint:fix": "eslint \"src/**/*.ts\" \"examples/**/*.ts\" \"tests/**/*.ts\" --fix",
16
16
  "format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
17
17
  "format:check": "prettier --check \"src/**/*.ts\" \"tests/**/*.ts\"",
18
18
  "prepublishOnly": "npm run build",
@@ -64,9 +64,12 @@
64
64
  "peerDependencies": {
65
65
  "@aws-sdk/client-ses": "^3.699.0",
66
66
  "@sendgrid/mail": "^8.1.4",
67
+ "ejs": "^3.1.10",
67
68
  "form-data": "^4.0.1",
69
+ "handlebars": "^4.7.8",
68
70
  "mailgun.js": "^10.2.3",
69
71
  "postmark": "^4.0.5",
72
+ "pug": "^3.0.3",
70
73
  "resend": "^4.0.1"
71
74
  },
72
75
  "peerDependenciesMeta": {
@@ -87,6 +90,15 @@
87
90
  },
88
91
  "postmark": {
89
92
  "optional": true
93
+ },
94
+ "handlebars": {
95
+ "optional": true
96
+ },
97
+ "ejs": {
98
+ "optional": true
99
+ },
100
+ "pug": {
101
+ "optional": true
90
102
  }
91
103
  },
92
104
  "devDependencies": {