Comparing version 1.0.14 to 1.1.0
60
mail.js
// Simple SMTP Email Sender | ||
// Copyright (c) 2015 - 2019 Joseph Huckaby | ||
// Copyright (c) 2015 - 2024 Joseph Huckaby | ||
// Released under the MIT License | ||
var fs = require('fs'); | ||
var nodemailer = require('nodemailer'); | ||
var Tools = require('pixl-tools'); | ||
var Class = require('pixl-class'); | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const nodemailer = require('nodemailer'); | ||
const Tools = require('pixl-tools'); | ||
module.exports = Class.create({ | ||
module.exports = class Mailer { | ||
options: null, | ||
__construct: function(host, port) { | ||
constructor(host, port) { | ||
// class constructor | ||
@@ -20,10 +18,10 @@ this.options = { | ||
}; | ||
}, | ||
} | ||
setOption: function(key, value) { | ||
setOption(key, value) { | ||
// set single option | ||
this.options[key] = value; | ||
}, | ||
} | ||
setOptions: function(opts) { | ||
setOptions(opts) { | ||
// set multiple options | ||
@@ -33,5 +31,10 @@ for (var key in opts) { | ||
} | ||
}, | ||
} | ||
send: function(data, args, callback) { | ||
attachLogAgent(agent) { | ||
// attach pixl-logger compatible log agent | ||
this.logger = agent; | ||
} | ||
send(data, args, callback) { | ||
// send e-mail | ||
@@ -100,2 +103,16 @@ var self = this; | ||
// capture debug trace from nodemailer, and log if configured | ||
// also send it back with response, whatever the result | ||
this.options.debug = true; | ||
this.options.logger = {}; | ||
var log = []; | ||
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach( function(level) { | ||
self.options.logger[level] = function(entry, message, ...args) { | ||
message = util.format(message, ...args); | ||
self.logDebug(9, message, entry); | ||
log.push( message, entry ); | ||
}; | ||
} ); | ||
// setup transport | ||
@@ -126,5 +143,14 @@ var transport = nodemailer.createTransport(this.options); | ||
transport.sendMail( opts, function(err) { | ||
callback( err, data ); | ||
callback( err, data, log ); | ||
} ); | ||
} | ||
}); | ||
logDebug(level, msg, data) { | ||
// log if we have an attached agent | ||
if (this.logger) { | ||
this.logger.set( 'component', 'Mailer' ); | ||
this.logger.debug( level, msg, data ); | ||
} | ||
} | ||
}; |
{ | ||
"name": "pixl-mail", | ||
"version": "1.0.14", | ||
"version": "1.1.0", | ||
"description": "A very simple class for sending e-mail via SMTP.", | ||
@@ -23,4 +23,3 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", | ||
"nodemailer": "6.9.9", | ||
"pixl-tools": "^1.0.19", | ||
"pixl-class": "^1.0.3" | ||
"pixl-tools": "^1.0.19" | ||
}, | ||
@@ -27,0 +26,0 @@ "devDependencies": { |
@@ -0,1 +1,17 @@ | ||
<details><summary>Table of Contents</summary> | ||
<!-- toc --> | ||
- [Overview](#overview) | ||
- [Usage](#usage) | ||
* [Placeholder Substitution](#placeholder-substitution) | ||
* [Loading From Files](#loading-from-files) | ||
* [Attachments](#attachments) | ||
* [HTML Emails](#html-emails) | ||
* [Options](#options) | ||
* [Logging](#logging) | ||
* [Debugging](#debugging) | ||
- [License](#license) | ||
</details> | ||
# Overview | ||
@@ -38,3 +54,3 @@ | ||
mail.send( message, function(err) { | ||
if (err) console.log( "Mail Error: " + err ); | ||
if (err) console.error( "Mail Error: " + err ); | ||
} ); | ||
@@ -114,3 +130,3 @@ ``` | ||
mail.send( message, args, function(err) { | ||
if (err) console.log( "Mail Error: " + err ); | ||
if (err) console.error( "Mail Error: " + err ); | ||
} ); | ||
@@ -134,3 +150,3 @@ ``` | ||
mail.send( message, function(err) { | ||
if (err) console.log( "Mail Error: " + err ); | ||
if (err) console.error( "Mail Error: " + err ); | ||
} ); | ||
@@ -168,2 +184,48 @@ ``` | ||
## Logging | ||
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: | ||
```js | ||
mail.attachLogAgent( logger ); | ||
``` | ||
## Debugging | ||
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: | ||
```js | ||
mail.send( message, function(err, message, log) { | ||
if (err) console.error( "Mail Error: " + err ); | ||
console.log( "Full composed message: " + message ); | ||
log.forEach( function(row) { | ||
console.log( ...row ); | ||
} ); | ||
} ); | ||
``` | ||
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: | ||
``` | ||
Creating transport: nodemailer (6.4.11; +https://nodemailer.com/; SMTP/6.4.11[client:6.4.11]) | ||
{"component":"mail","tnx":"create"} | ||
Sending mail using SMTP/6.4.11[client:6.4.11] | ||
{"component":"mail","tnx":"transport","name":"SMTP","version":"6.4.11[client:6.4.11]","action":"send"} | ||
Resolved localhost as ::1 [cache miss] | ||
{"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"dns","source":"localhost","resolved":"::1","cached":false} | ||
Connection established to ::1:25 | ||
{"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"network","localAddress":"::1","localPort":53068,"remoteAddress":"::1","remotePort":25} | ||
220 joemax.local ESMTP Postfix | ||
{"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"server"} | ||
EHLO joemax.local | ||
{"component":"smtp-connection","sid":"N5uAYhHPKsY","tnx":"client"} | ||
``` | ||
# License | ||
@@ -173,3 +235,3 @@ | ||
*Copyright (c) 2015 - 2019 Joseph Huckaby.* | ||
*Copyright (c) 2015 - 2024 Joseph Huckaby.* | ||
@@ -176,0 +238,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13692
2
122
250
- Removedpixl-class@^1.0.3
- Removedpixl-class@1.0.3(transitive)