pixl-mail 1.0.14 → 1.1.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.
Files changed (3) hide show
  1. package/README.md +66 -4
  2. package/mail.js +43 -17
  3. package/package.json +2 -3
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.
@@ -35,7 +51,7 @@ let message =
35
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
 
@@ -110,7 +126,7 @@ let 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
 
@@ -129,7 +145,7 @@ let message =
129
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
 
@@ -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, { date: Tools.timeNow(), level, ...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.14",
3
+ "version": "1.1.1",
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",
@@ -20,8 +20,7 @@
20
20
  ],
21
21
  "dependencies": {
22
22
  "nodemailer": "6.9.9",
23
- "pixl-tools": "^1.0.19",
24
- "pixl-class": "^1.0.3"
23
+ "pixl-tools": "^1.0.19"
25
24
  },
26
25
  "devDependencies": {
27
26