npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bgroup/mailer

v1.0.3

Published

Node JS Basic application

Downloads

12

Readme

BG-mailer 📧

BG-mailer is a powerful tool for sending dynamic HTML emails. With BG-mailer, you can generate a token associated with an application, store it in the database, and use that information to customize the HTML template before sending the email.

Key Features

The heart of BG-mailer is the MailManager class, which manages the entire email sending process. There are two main functions you need to know to use BG-mailer:

  • hasPermission: This function checks whether the provided token belongs to an application registered in the database.
  • send: This function sends the email. It takes the parameters {to, subject, module, data, attachments}. Where:
    • to: Recipient's email address.
    • subject: Email subject.
    • module: Module to fetch from the database.
    • data: Values to be set in the HTML document.
    • attachments: An array of objects with the structure { filename: "file name", path: "file path" }.

Installation

Installing BG-mailer is straightforward. You just need to run the command npm i to install all the dependencies.

import { MailManager } from '@bgroup/mailer/manager';

Methods :

  • hasPermission(token: string): Verifies if a token has permissions to access the mail service.

  • getData(data: any, module: any): Retrieves necessary data to construct email content. Processes email templates with provided data.

  • send({ to, subject, module, data, from, attachments }: IDataSend): Sends an email using the provided data.

  • Other private methods like setInfo, findChildrenTemplates, insertValueIntoChildren, getAdditionals, #findTemplate, processChildren, getTpl, setDataTpl: These methods are helpers used to process data, templates, and attachments before sending the email.

Use case sending an email :

import { MailManager } from '@bgroup/mailer/manager';

const mailerManager = new MailManager();

const hasPermission = await mailerManager.hasPermission('token');

if (hasPermission) {
    const emailData = {
        to: '[email protected]',
        subject: 'Email Subject',
        module: 'module',
        data: {
            name: 'Alice',
            message: 'Hello, here is some additional information!',
        },
        from: '[email protected]',
        attachments: [
            {
                filename: 'file.pdf',
                path: '/path/to/file.pdf',
            },
        ],
    };

    const emailSent = await mailerManager.send(emailData);
    if (!emailSent.error) {
        console.log('Email sent successfully!');
    } else {
        console.error('Error sending email:', emailSent.error);
    }
}

Use case sending mails with files :

import { MailManager } from '@bgroup/mailer/manager';

const mailerManager = new MailManager();

const emailData = {
    to: '[email protected]',
    subject: 'Email with Attachments',
    module: 'module',
    data: {
        name: 'John Doe',
        message: 'Hello! Please find the attached files.',
    },
    from: '[email protected]',
    attachments: [
        {
            filename: 'document.pdf',
            path: '/path/to/document.pdf',
        },
        {
            filename: 'image.jpg',
            path: '/path/to/image.jpg',
        },
    ],
};

const emailSent = await mailerManager.send(emailData);

if (!emailSent.error) {
    console.log('Email with attachments sent successfully!');
} else {
    console.error('Error sending email with attachments:', emailSent.error);
}

passing the template parameter :

const mailerManager = new MailManager();

mailerManager.setCredentials(
    'mail.example.com',
    '587',
    '[email protected]',
    'your_password'
);

const emailData = {
    to: '[email protected]',
    subject: 'Email with Template',
    module: 'module_name',
    data: {
        name: 'John Doe',
        message: 'Hello! Please find the attached files.',
    },
    from: '[email protected]',
    attachments: [
        {
            filename: 'document.pdf',
            path: '/path/to/document.pdf',
        },
    ],
    template:
        '<html><body><h1>Hello, {{name}}!</h1><p>{{message}}</p></body></html>',
    variables: {
        name: 'John Doe',
        message: 'Hello! Please find the attached files.',
    },
};

const emailSent = await mailerManager.send(emailData);

if (!emailSent.error) {
    console.log('Email sent successfully!');
} else {
    console.error('Error sending email:', emailSent.error);
}

you can also pass a template and variables only :

import { MailManager } from '@bgroup/mailer/MailManager';

const mailerManager = new MailManager();

mailerManager.setCredentials({
    host: 'smtp.example.com',
    port: '587',
    user: '[email protected]',
    pass: 'your_email_password',
});

const emailData = {
    template: '<html><body><h1>{{name}}</h1><p>{{message}}</p></body></html>',
    variables: {
        name: 'Bob',
        message: 'This is a test email with template and variables.',
    },
};

const emailSent = await mailerManager.send(emailData);

if (!emailSent.error) {
    console.log('Email sent successfully!');
} else {
    console.error('Error sending email:', emailSent.error);
}

In this example, MailManager is instantiated, the connection credentials are established, and the e-mail data is defined. The template parameter is included with the content of the HTML template and variables with the values to be replaced.

Contributing 🎁

Please feel free to contribute to the development of Mailer. You can clone the repository, create a new branch for your features or fixes, and submit a pull request.