Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

errormail

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

errormail

Error mail sender

  • 1.0.1
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 01 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc