1.0.0 • Published 4 months ago

email-services v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Email Service

A simple, configurable email service with SMTP support and webhook tracking. This package allows you to send emails via SMTP, track opens via a tracking pixel, and receive status updates via webhooks.

Note- webhook event can be recieved on only live url not on local

Installation

To install the package, run the following command:

npm install email-service

Usage 1. Initialize the Email Service Import the EmailService class and initialize it with your SMTP configuration and webhook URL.

const EmailService = require('email-service');

const emailService = new EmailService({
    smtpHost: 'smtp.gmail.com',
    smtpPort: 465,
    username: 'your-email@gmail.com',
    password: 'your-smtp-password',
    webhookUrl: 'https://your-webhook-url.com/email-status-webhook',
    port: 3000 // Optional: the port for your webhook listener (default is 3000)
});
  1. Send an Email Use the sendEmailSMTP method to send an email with support for tracking and attachments.
emailService.sendEmailSMTP({
    from: 'your-email@gmail.com',
    to: 'recipient@example.com',
    subject: 'Hello from NPM!',
    body: '<p>This is a test email with tracking.</p>',
    isHtml: true,
    trackingId: '123456',
    // Optional: Add attachments
    // attachments: [
    //     { path: './path/to/file.jpg', filename: 'file.jpg', mimeType: 'image/jpeg' }
    // ]
})
    .then(response => {
        console.log('Email sent successfully:', response);
    })
    .catch(error => {
        console.error('Error sending email:', error);
    });
  1. Webhook for Email Status You can configure a webhook URL to receive notifications about the email's status (e.g., sent, opened, failed). When an email is opened, the email service will send a request to your webhook URL with the status of the email.

Example webhook handler:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

app.post('/email-status-webhook', (req, res) => {
    console.log('Received Email Status Event:', req.body);
    res.status(200).send('Event received');
});

app.listen(3000, () => {
    console.log('Webhook listener running at http://localhost:3000');
});
  1. Track Email Opens The email service automatically adds a tracking pixel to each email. When the recipient opens the email, the pixel will be triggered, and a request will be sent to the configured webhook URL.

  2. Server Setup (Optional) If you want to set up the webhook listener locally, you can use the following server configuration

const EmailService = require('email-service');

const emailService = new EmailService({
    smtpHost: 'smtp.gmail.com',
    smtpPort: 465,
    username: 'your-email@gmail.com',
    password: 'your-smtp-password',
    webhookUrl: 'https://your-webhook-url.com/email-status-webhook',
    port: 3000
});

emailService.sendEmailSMTP({
    from: 'your-email@gmail.com',
    to: 'recipient@example.com',
    subject: 'Test Email',
    body: '<p>This is a test email with tracking.</p>',
    isHtml: true,
    trackingId: '123456'
})
    .then(console.log)
    .catch(console.error);

Configuration Parameters SMTP Configuration: smtpHost: SMTP server host (e.g., smtp.gmail.com). smtpPort: SMTP server port (e.g., 465 for Gmail SSL). username: Your email address used for authentication. password: Your email password or application-specific password. Webhook Configuration: webhookUrl: The URL that will receive email status updates. Optional: port: The port number for the local webhook server (default is 3000). License MIT License. See LICENSE for details.

Notes:

  • Replace your-email@gmail.com and your-smtp-password with your actual credentials.
  • The example webhook server listens on port 3000. You can change the port if needed.
  • The sendEmailSMTP method supports email tracking and optional attachments.
  • The attachments field is commented out in the example. You can use it to attach files to the email
1.0.0

4 months ago