pax_global_header 0000666 0000000 0000000 00000000064 15215263733 0014521 g ustar 00root root 0000000 0000000 52 comment=2656ebcf5f85a079feceae30ae77471bbf71514f
pixl-mail-1.1.9/ 0000775 0000000 0000000 00000000000 15215263733 0013425 5 ustar 00root root 0000000 0000000 pixl-mail-1.1.9/.npmignore 0000664 0000000 0000000 00000000041 15215263733 0015417 0 ustar 00root root 0000000 0000000 .gitignore
node_modules/
test.js
pixl-mail-1.1.9/README.md 0000664 0000000 0000000 00000022111 15215263733 0014701 0 ustar 00root root 0000000 0000000 Table of Contents
- [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)
Please give our state more money.
\n"; mail.send( message, function(err) { if (err) console.error( "Mail Error: " + err ); } ); ``` **Note:** Your e-mail body must begin with an HTML tag for it to be recognized. ## Options You can set a number of options using the `setOption()` or `setOptions()` methods. These are passed directly to the underlying [nodemailer](https://nodemailer.com/) module, so please check out their documentation for details. Examples include setting timeouts, SSL, and authentication. The `setOption()` method takes one single key/value to set or replace, while `setOptions()` accepts an object containing multiple keys/values. ```js mail.setOption( 'secure', true ); // use ssl mail.setOption( 'auth', { user: 'fsmith', pass: '12345' } ); mail.setOptions({ connectionTimeout: 10000, // milliseconds greetingTimeout: 10000, // milliseconds socketTimeout: 10000 // milliseconds }); ``` You can also use local [sendmail](https://nodemailer.com/transports/sendmail/), if you have that configured on your server. To do this, set the following options, and tune as needed: ```js mail.setOptions({ "sendmail": true, "newline": "unix", "path": "/usr/sbin/sendmail" }); ``` ## 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 **The MIT License (MIT)** *Copyright (c) 2015 - 2024 Joseph Huckaby.* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pixl-mail-1.1.9/mail.js 0000664 0000000 0000000 00000007477 15215263733 0014724 0 ustar 00root root 0000000 0000000 // Simple SMTP Email Sender // Copyright (c) 2015 - 2024 Joseph Huckaby // Released under the MIT License const fs = require('fs'); const util = require('util'); const nodemailer = require('nodemailer'); const Tools = require('pixl-tools'); module.exports = class Mailer { constructor(host, port) { // class constructor this.options = { host: host || '127.0.0.1', port: port || 25 }; } setOption(key, value) { // set single option this.options[key] = value; } setOptions(opts) { // set multiple options for (var key in opts) { this.options[key] = opts[key]; } } attachLogAgent(agent) { // attach pixl-logger compatible log agent this.logger = agent; } send(data, args, callback) { // send e-mail var self = this; // support 2-argument convention (data and callback only) if (!callback && (typeof(args) == 'function')) { callback = args; args = null; } // support buffers if (data instanceof Buffer) { data = data.toString(); } // support loading files if (!data.match(/\n/)) { fs.readFile(data, { encoding: 'utf8' }, function (err, data) { if (err) callback(err); else self.send( data, args, callback ); } ); return; } // support null callback if (!callback) callback = function() {}; // perform placeholder substitution on body if (args) data = Tools.sub( data, args ); // fix line endings data = data.replace(/\r\n/g, "\n").replace(/\r/g, "\n"); // split out headers and body var parts = data.split(/\n\n/); var headers_raw = parts.shift(); var body_raw = parts.join("\n\n"); if (!body_raw.match(/\S/)) return callback( new Error("Cannot locate e-mail body."), data ); // parse headers into key/value pairs var headers = {}; headers_raw.replace(/([\w\-]+)\:\s*([^\n]*)/g, function(m_all, m1, m2) { headers[ m1 ] = m2; return ''; } ); // grab to, from and subject var to = headers['To']; var from = headers['From']; var subject = headers['Subject']; if (!to) return callback( new Error("Missing required header: 'To'"), data ); if (!from) return callback( new Error("Missing required header: 'From'"), data ); if (!subject) return callback( new Error("Missing required header: 'Subject'"), data ); delete headers['To']; delete headers['From']; delete headers['Subject']; if (this.options.sendmail) { delete this.options.host; delete this.options.port; } // 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) { if (!entry) entry = {}; message = util.format(message, ...args); self.logDebug(9, message, entry); log.push([ message, { date: Tools.timeNow(), level, ...entry } ]); }; } ); // setup transport var transport = nodemailer.createTransport(this.options); var opts = { from: from, to: to, subject: subject }; // support cc and bcc if (headers['Cc']) { opts.cc = headers['Cc']; delete headers['Cc']; } if (headers['Bcc']) { opts.bcc = headers['Bcc']; delete headers['Bcc']; } // custom headers if (Tools.numKeys(headers)) opts.headers = headers; // attachments if (args && args.attachments) opts.attachments = args.attachments; // auto-detect html or text if (body_raw.match(/^\s*)) opts.html = body_raw; else opts.text = body_raw; // send mail transport.sendMail( opts, function(err) { 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 ); } } }; pixl-mail-1.1.9/package.json 0000664 0000000 0000000 00000001066 15215263733 0015716 0 ustar 00root root 0000000 0000000 { "name": "pixl-mail", "version": "1.1.9", "description": "A very simple class for sending e-mail via SMTP.", "author": "Joseph Huckaby