pixl-mail 1.0.13 → 1.1.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 (3) hide show
  1. package/README.md +91 -29
  2. package/mail.js +43 -17
  3. package/package.json +3 -4
package/README.md CHANGED
@@ -1,3 +1,19 @@
1
+ <details><summary>Table of Contents</summary>
2
+
3
+ <!-- toc -->
4
+ - [Overview](#overview)
5
+ - [Usage](#usage)
6
+ * [Placeholder Substitution](#placeholder-substitution)
7
+ * [Loading From Files](#loading-from-files)
8
+ * [Attachments](#attachments)
9
+ * [HTML Emails](#html-emails)
10
+ * [Options](#options)
11
+ * [Logging](#logging)
12
+ * [Debugging](#debugging)
13
+ - [License](#license)
14
+
15
+ </details>
16
+
1
17
  # Overview
2
18
 
3
19
  This module provides a very simple e-mail sender, which leans heavily on the awesome [nodemailer](https://nodemailer.com/) package. It layers on the ability to pass in a complete e-mail message with headers and body in one string (or file), and optionally perform placeholder substitution using [sub()](https://www.npmjs.com/package/pixl-tools#sub) from the [pixl-tools](https://www.npmjs.com/package/pixl-tools) package. Auto-detects HTML or plain text e-mail body, and supports custom headers and attachments as well.
@@ -6,36 +22,36 @@ This module provides a very simple e-mail sender, which leans heavily on the awe
6
22
 
7
23
  Use [npm](https://www.npmjs.com/) to install the module:
8
24
 
9
- ```
25
+ ```sh
10
26
  npm install pixl-mail
11
27
  ```
12
28
 
13
29
  Then use `require()` to load it in your code:
14
30
 
15
- ```javascript
16
- var PixlMail = require('pixl-mail');
31
+ ```js
32
+ const PixlMail = require('pixl-mail');
17
33
  ```
18
34
 
19
35
  Instantiate a mailer object and pass in the SMTP hostname (defaults to `127.0.0.1`) and the port number (defaults to `25`):
20
36
 
21
- ```javascript
22
- var mail = new PixlMail( 'smtp.myserver.com', 25 );
23
- var mail = new PixlMail( '127.0.0.1' );
24
- var mail = new PixlMail();
37
+ ```js
38
+ let mail = new PixlMail( 'smtp.myserver.com', 25 );
39
+ let mail = new PixlMail( '127.0.0.1' );
40
+ let mail = new PixlMail();
25
41
  ```
26
42
 
27
43
  Send mail using the `send()` method. Pass in the complete e-mail message as a multi-line string including `To`, `From` and `Subject` headers, and a callback:
28
44
 
29
- ```javascript
30
- var message =
45
+ ```js
46
+ let message =
31
47
  "To: president@whitehouse.gov\n" +
32
48
  "From: citizen@email.com\n" +
33
- "Subject: NASA Budget\n" +
49
+ "Subject: State Budget\n" +
34
50
  "\n" +
35
- "Dear Mr. President,\nPlease give NASA back their money.\n";
51
+ "Dear Mr. President,\nOur state needs more money.\n";
36
52
 
37
53
  mail.send( message, function(err) {
38
- if (err) console.log( "Mail Error: " + err );
54
+ if (err) console.error( "Mail Error: " + err );
39
55
  } );
40
56
  ```
41
57
 
@@ -43,13 +59,13 @@ For multiple recipients, simply separate them by commas on the `To` line. You c
43
59
 
44
60
  ## Placeholder Substitution
45
61
 
46
- The library supports a simple e-mail templating system, where you can insert `[bracket_placeholders]` in your e-mail message, and have the library fill them with appropriate content from a separate object. This feature uses the [sub()](https://www.npmjs.com/package/pixl-tools#sub) function from the [pixl-tools](https://www.npmjs.com/package/pixl-tools) package.
62
+ The library supports a simple e-mail templating system, where you can insert `[bracket_placeholders]` in your e-mail message, and have the library fill them with appropriate content from a separate object. This feature uses the [sub()](https://github.com/jhuckaby/pixl-tools#sub) function from the [pixl-tools](https://github.com/jhuckaby/pixl-tools) package.
47
63
 
48
64
  As an example, imagine a welcome e-mail for a new user who has signed up for your app. You have the welcome e-mail "template" stored separately, and want to fill in the user's e-mail address, full name and username at sending time. Here is how to do this:
49
65
 
50
- ```javascript
66
+ ```js
51
67
  // email template
52
- var message =
68
+ let message =
53
69
  "To: [email]\n" +
54
70
  "From: support@myapp.com\n" +
55
71
  "Subject: Welcome to My App, [full_name]!\n" +
@@ -57,7 +73,7 @@ var message =
57
73
  "Dear [full_name],\nWelcome to My App! Your username is '[username]'.\n";
58
74
 
59
75
  // placeholder args
60
- var user = {
76
+ let user = {
61
77
  username: "jhuckaby",
62
78
  full_name: "Joseph Huckaby",
63
79
  email: "jhuckaby@email.com"
@@ -79,14 +95,14 @@ Dear Joseph Huckaby,
79
95
  Welcome to My App! Your username is 'jhuckaby'.
80
96
  ```
81
97
 
82
- You can actually use a complex hash / array tree of arguments, and then specify `[filesystem/style/paths]` or `[dot.style.paths]` in your placeholders. See the [sub()](https://www.npmjs.com/package/pixl-tools#sub) docs for details.
98
+ You can actually use a complex hash / array tree of arguments, and then specify `[filesystem/style/paths]` or `[dot.style.paths]` in your placeholders. See the [sub()](https://github.com/jhuckaby/pixl-tools#sub) docs for details.
83
99
 
84
100
  ## Loading From Files
85
101
 
86
102
  You can specify a file path instead of the raw message, like this:
87
103
 
88
- ```javascript
89
- var user = {
104
+ ```js
105
+ let user = {
90
106
  username: "jhuckaby",
91
107
  full_name: "Joseph Huckaby",
92
108
  email: "jhuckaby@email.com"
@@ -101,8 +117,8 @@ mail.send( "conf/emails/new_user_welcome.txt", user, function(err) {
101
117
 
102
118
  To attach files, include an `attachments` array in your `args` object, and specify a `filename` and `path` for each one. This is passed directly to [nodemailer](https://nodemailer.com/), so you can use all of their attachment features. Example:
103
119
 
104
- ```javascript
105
- var args = {
120
+ ```js
121
+ let args = {
106
122
  attachments: [
107
123
  { filename: "contract.pdf", path: "files/contracts/4573D.PDF" },
108
124
  { filename: "policy.pdf", path: "files/misc/POLICY-2015.PDF" }
@@ -110,7 +126,7 @@ var args = {
110
126
  };
111
127
 
112
128
  mail.send( message, args, function(err) {
113
- if (err) console.log( "Mail Error: " + err );
129
+ if (err) console.error( "Mail Error: " + err );
114
130
  } );
115
131
  ```
116
132
 
@@ -120,16 +136,16 @@ For details, see the [Attachments](https://nodemailer.com/message/attachments/)
120
136
 
121
137
  If you want to send HTML formatted e-mails, the library will automatically detect this. Just provide the headers in plain text, two end-of-lines, then start your HTML markup. Example:
122
138
 
123
- ```javascript
124
- var message =
139
+ ```js
140
+ let message =
125
141
  "To: president@whitehouse.gov\n" +
126
142
  "From: citizen@email.com\n" +
127
- "Subject: NASA Budget\n" +
143
+ "Subject: State Budget\n" +
128
144
  "\n" +
129
- "<h1>Dear Mr. President,</h1>\n<p><b>Please</b> give NASA back their <i>money</i>.</p>\n";
145
+ "<h1>Dear Mr. President,</h1>\n<p><b>Please</b> give our state more <i>money</i>.</p>\n";
130
146
 
131
147
  mail.send( message, function(err) {
132
- if (err) console.log( "Mail Error: " + err );
148
+ if (err) console.error( "Mail Error: " + err );
133
149
  } );
134
150
  ```
135
151
 
@@ -141,7 +157,7 @@ You can set a number of options using the `setOption()` or `setOptions()` method
141
157
 
142
158
  The `setOption()` method takes one single key/value to set or replace, while `setOptions()` accepts an object containing multiple keys/values.
143
159
 
144
- ```javascript
160
+ ```js
145
161
  mail.setOption( 'secure', true ); // use ssl
146
162
  mail.setOption( 'auth', { user: 'fsmith', pass: '12345' } );
147
163
 
@@ -162,11 +178,57 @@ mail.setOptions({
162
178
  });
163
179
  ```
164
180
 
181
+ ## Logging
182
+
183
+ You can optionally attach a [pixl-logger](https://github.com/jhuckaby/pixl-logger) compatible log agent, which will log all the [nodemailer](https://nodemailer.com/) debug messages at level 9, with the component column set to `Mailer`. To use this feature, call the `attachLogAgent()` method on your class instance, and pass in your pixl-logger instance:
184
+
185
+ ```js
186
+ mail.attachLogAgent( logger );
187
+ ```
188
+
189
+ ## Debugging
190
+
191
+ The `send()` method actually returns three arguments: the error (if any), the final composed mail body with headers (after all macro expansion), and a full debug log capture from [nodemailer](https://nodemailer.com/). Here is how to use them:
192
+
193
+ ```js
194
+ mail.send( message, function(err, message, log) {
195
+ if (err) console.error( "Mail Error: " + err );
196
+
197
+ console.log( "Full composed message: " + message );
198
+
199
+ log.forEach( function(row) {
200
+ console.log( ...row );
201
+ } );
202
+ } );
203
+ ```
204
+
205
+ Each log row will contain two elements: the log message itself, and an object containing additional metadata. These come directly from [nodemailer](https://nodemailer.com/). Here is an example excerpt:
206
+
207
+ ```
208
+ Creating transport: nodemailer (6.4.11; +https://nodemailer.com/; SMTP/6.4.11[client:6.4.11])
209
+ {"component":"mail","tnx":"create"}
210
+
211
+ Sending mail using SMTP/6.4.11[client:6.4.11]
212
+ {"component":"mail","tnx":"transport","name":"SMTP","version":"6.4.11[client:6.4.11]","action":"send"}
213
+
214
+ Resolved localhost as ::1 [cache miss]
215
+ {"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"dns","source":"localhost","resolved":"::1","cached":false}
216
+
217
+ Connection established to ::1:25
218
+ {"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"network","localAddress":"::1","localPort":53068,"remoteAddress":"::1","remotePort":25}
219
+
220
+ 220 joemax.local ESMTP Postfix
221
+ {"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"server"}
222
+
223
+ EHLO joemax.local
224
+ {"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"client"}
225
+ ```
226
+
165
227
  # License
166
228
 
167
229
  **The MIT License (MIT)**
168
230
 
169
- *Copyright (c) 2015 - 2019 Joseph Huckaby.*
231
+ *Copyright (c) 2015 - 2024 Joseph Huckaby.*
170
232
 
171
233
  Permission is hereby granted, free of charge, to any person obtaining a copy
172
234
  of this software and associated documentation files (the "Software"), to deal
package/mail.js CHANGED
@@ -1,37 +1,40 @@
1
1
  // Simple SMTP Email Sender
2
- // Copyright (c) 2015 - 2019 Joseph Huckaby
2
+ // Copyright (c) 2015 - 2024 Joseph Huckaby
3
3
  // Released under the MIT License
4
4
 
5
- var fs = require('fs');
6
- var nodemailer = require('nodemailer');
7
- var Tools = require('pixl-tools');
8
- var Class = require('pixl-class');
5
+ const fs = require('fs');
6
+ const util = require('util');
7
+ const nodemailer = require('nodemailer');
8
+ const Tools = require('pixl-tools');
9
9
 
10
- module.exports = Class.create({
10
+ module.exports = class Mailer {
11
11
 
12
- options: null,
13
-
14
- __construct: function(host, port) {
12
+ constructor(host, port) {
15
13
  // class constructor
16
14
  this.options = {
17
15
  host: host || '127.0.0.1',
18
16
  port: port || 25
19
17
  };
20
- },
18
+ }
21
19
 
22
- setOption: function(key, value) {
20
+ setOption(key, value) {
23
21
  // set single option
24
22
  this.options[key] = value;
25
- },
23
+ }
26
24
 
27
- setOptions: function(opts) {
25
+ setOptions(opts) {
28
26
  // set multiple options
29
27
  for (var key in opts) {
30
28
  this.options[key] = opts[key];
31
29
  }
32
- },
30
+ }
31
+
32
+ attachLogAgent(agent) {
33
+ // attach pixl-logger compatible log agent
34
+ this.logger = agent;
35
+ }
33
36
 
34
- send: function(data, args, callback) {
37
+ send(data, args, callback) {
35
38
  // send e-mail
36
39
  var self = this;
37
40
 
@@ -95,6 +98,20 @@ module.exports = Class.create({
95
98
  delete this.options.port;
96
99
  }
97
100
 
101
+ // capture debug trace from nodemailer, and log if configured
102
+ // also send it back with response, whatever the result
103
+ this.options.debug = true;
104
+ this.options.logger = {};
105
+ var log = [];
106
+
107
+ ['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach( function(level) {
108
+ self.options.logger[level] = function(entry, message, ...args) {
109
+ message = util.format(message, ...args);
110
+ self.logDebug(9, message, entry);
111
+ log.push( message, entry );
112
+ };
113
+ } );
114
+
98
115
  // setup transport
99
116
  var transport = nodemailer.createTransport(this.options);
100
117
 
@@ -120,7 +137,16 @@ module.exports = Class.create({
120
137
 
121
138
  // send mail
122
139
  transport.sendMail( opts, function(err) {
123
- callback( err, data );
140
+ callback( err, data, log );
124
141
  } );
125
142
  }
126
- });
143
+
144
+ logDebug(level, msg, data) {
145
+ // log if we have an attached agent
146
+ if (this.logger) {
147
+ this.logger.set( 'component', 'Mailer' );
148
+ this.logger.debug( level, msg, data );
149
+ }
150
+ }
151
+
152
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixl-mail",
3
- "version": "1.0.13",
3
+ "version": "1.1.0",
4
4
  "description": "A very simple class for sending e-mail via SMTP.",
5
5
  "author": "Joseph Huckaby <jhuckaby@gmail.com>",
6
6
  "homepage": "https://github.com/jhuckaby/pixl-mail",
@@ -19,9 +19,8 @@
19
19
  "smtp"
20
20
  ],
21
21
  "dependencies": {
22
- "nodemailer": "6.6.1",
23
- "pixl-tools": "^1.0.19",
24
- "pixl-class": "^1.0.3"
22
+ "nodemailer": "6.9.9",
23
+ "pixl-tools": "^1.0.19"
25
24
  },
26
25
  "devDependencies": {
27
26