1.0.1 • Published 2 years ago

errormail v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

ErrorMail

ErrorMail is a Node.js module for sending error emails using the Nodemailer library, and it supports being used as middleware for easy integration into your existing Node.js application.

Installation

npm i errormail

Usage

const ErrorMail = require('errormail');
const errorMail = new ErrorMail({
  transporter: {
    service: 'gmail',
    auth: {
      user: 'youremail@gmail.com',
      pass: 'yourpassword',
    }
  },
  receiver: {
    to: 'recipient@example.com',
  },
  showLogs: true,
});

//Usage Example1
const error = new Error('Something went wrong');
errorMail.sendError(error, 'optional-recipient@example.com', 'Optional Subject')
  .then(info => {
    console.log(`Error email sent: ${info.response}`);
  })
  .catch(err => {
    console.error('Error sending email:', err);
  });

//Usage Example2
fs.readFile('path/file/errors.txt', (err, chunk) => {
    if (err) {
        errorMail.sendError(
            err,
            'optional-recipient@example.com',
            'Optional Subject'
        ).then(() => console.log('Error email sent.')).catch(console.error);
    }
    // your code...
});

//Usage Example3
(async () => {
    try {
        await fs.promises.readFile('path/file/errors.txt');
    } catch (err) {
        const sendedError = await errorMail.sendError(err, 'optional-recipient@example.com');
        console.log(sendedError);
        //handle err...
    }
})();

ErrorMail Constructor

The ErrorMail constructor takes an options object with the following properties:

const errorMail = new ErrorMail({
  transporter: {
    service: 'gmail', // String specifying the email service. Default is 'gmail'.
    auth: {
      user: 'sender@example.com', // String specifying the email address of the sender.
      pass: 'password' // String specifying the password for the email address of the sender.
    }
  },
  receiver: {
    to: 'recipient@example.com' // String specifying the email address of the recipient.
  },
  showLogs: false // Boolean specifying whether to show logs in the console. Default is false.
});

Constructor Object Options

When creating an instance of the ErrorMail class, you can provide the following options:

The ErrorMail constructor takes an options object with the following properties:

  • transporter: Object specifying the email service and authentication credentials.
    • Required properties:
      • service: String specifying the email service. Default is 'gmail'.
      • auth: Object specifying the authentication credentials.
        • Required properties:
          • user: String specifying the email address of the sender.
          • pass: String specifying the password for the email address of the sender.
  • receiver: Object specifying the default recipient.
    • to: String specifying the email address of the recipient.
  • showLogs: Boolean specifying whether to show logs in the console. Default is false.
Example options object:
const options = {
  transporter: {
    service: 'gmail',
    auth: {
      user: 'youremail@gmail.com',
      pass: 'yourpassword'
    }
  },
  receiver: {
    to: 'recipient1@example.com, recipient2@example.com'
  },
  showLogs: true
};
You can then pass this options object as an argument when creating a new instance of the ErrorMail class:
const errorMail = new ErrorMail(options);

Methods

  • sendError: The sendError method is thenable and takes three optional parameters:
    • error: Error object or string representing the error message.
    • to: String specifying an optional recipient for the email.
    • subject: String specifying an optional subject line for the email.
errorMail.sendError(error, to, subject);

NOTE: When using ErrorMail as a middleware, you can pass the errorMail.sendError method as a parameter to the app.use function. The errorHandler middleware will catch any errors that occur in subsequent middleware or routes, and pass them to errorMail.sendError to be sent as an email.

Using ErrorMail with Middleware

ErrorMail can be used as a middleware to send error emails automatically when an error occurs in your Node.js application. To use ErrorMail as a middleware, you can pass the errorMail.sendError method as a parameter to the next function in your error handling middleware.

Here's an example of how you can use ErrorMail with middleware:

const express = require('express');
const ErrorMail = require('errormail');

const app = express();

const { errorHandler } = new ErrorMail({
  transporter: {
    service: 'gmail',
    auth: {
      user: 'youremail@gmail.com',
      pass: 'yourpassword',
    }
  },
  receiver: {
    to: 'recipient@example.com',
  },
  showLogs: true,
});

app.use(errorHandler);

app.get('/', (req, res) => {
  throw new Error('Something went wrong');
});

app.listen(3000, () => console.log('Server started on port 3000'));

In the example above, we create an instance of the ErrorMail class and pass it to our error handling middleware. When an error occurs in our application, the middleware will call the errorMail.sendError method and pass the error object as a parameter. The sendError method will then send an error email to the specified recipient.

After the email is sent, the middleware will pass the error to the next middleware or error handler using the next function.

Middleware Example

License This project is licensed under the MIT License - see the LICENSE file for details.

This README.md file includes information on how to install the module, as well as an example usage snippet that shows how to create an instance of the ErrorMail class and send an error email with optional recipient and subject parameters.

It also includes an Options section that describes the properties of the options object that can be passed to the ErrorMail constructor, as well as the optional parameters for the sendError method.

Finally, it includes a License section that describes the license for the module.

1.0.1

2 years ago

1.0.0

2 years ago

0.1.0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago