souravbapari/emailing

By souravbapari

Updated 7 months ago

High-performance email delivery and scheduling service using Bun, BullMQ, and Redis.

Image
Message queues
Developer tools
0

751

souravbapari/emailing repository overview

Mailing Service

A production-ready email delivery and scheduling service built with Bun , BullMQ , and Redis , designed to run seamlessly with Docker and Docker Compose.


Features

  • SMTP email sending
  • Scheduled email delivery
  • Redis-backed job queue (BullMQ)
  • High-performance Bun runtime
  • Docker-first deployment
  • Graceful shutdown & retry support

Architecture

┌──────────────────┐        ┌──────────────┐
│ Mailing Service  │ 6379   │ Redis 7       │
│ (Bun + BullMQ)   │──────▶│ Job Queue     │
└──────────────────┘        └──────────────┘

Each service runs in its own container following Docker best practices.


Requirements

  • Docker 20+
  • Docker Compose v2+

Quick Start

1. Clone the repository
git clone <your-repo-url>
cd mailing-service

2. Start services
docker compose up --build -d

Services started:

  • Mailing Service → http://localhost:5878
  • Redis → internal container network

3. Stop services
docker compose down

docker-compose.yml

version: "3.9"

services:
  app:
    build: .
    container_name: mailing-service
    ports:
      - "5878:5878"
    environment:
      PORT: 5878
      REDIS_HOST: redis
      REDIS_PORT: 6379
      API_KEY: hello
    depends_on:
      - redis
    restart: unless-stopped

  redis:
    image: redis:7
    container_name: redis
    restart: unless-stopped
    volumes:
      - redis-data:/data

volumes:
  redis-data:

Environment Variables

VariableDescriptionRequired
PORTApplication port
REDIS_HOSTRedis hostname
REDIS_PORTRedis port
API_KEYAPI authentication key
REDIS_USERNAMERedis ACL username
REDIS_PASSWORDRedis password

API Authentication

All requests must include the API key:

Header
Authorization: Bearer token

Using the Node.js Client

The easiest way to interact with the Mailing Service is using the official Node.js client package.

Installation
npm install emailing-service

or

pnpm add emailing-service

or

bun add emailing-service
Quick Start
import { Mailing } from "emailing-service";

const mailer = new Mailing({
  host: "http://localhost:5878", // Your mailing service URL
  apiKey: "your-api-key",
  credential: {
    host: "smtp.gmail.com",
    port: 587,
    secure: false,
    auth: {
      user: "[email protected]",
      pass: "app-password",
    },
  },
});

// Send email immediately
await mailer.send({
  from: "[email protected]",
  to: "[email protected]",
  subject: "Welcome",
  text: "Hello from Mailing Service",
});

// Schedule email for later
const scheduled = await mailer.schedule(
  new Date(Date.now() + 60 * 60 * 1000) // 1 hour later
);

const result = await scheduled.send({
  from: "[email protected]",
  to: "[email protected]",
  subject: "Scheduled Email",
  html: "<h1>Hello</h1><p>This email was scheduled.</p>",
});

// Check email existence
const check = await mailer.isEmailExistence("[email protected]");
console.log(check.data.exists); // true or false

// Cancel scheduled email
await mailer.cancelMail(result.data.id);

For more details, visit the npm package page.


Example API Request

Send Email
POST /mail/send
Content-Type: application/json
Authorization: Bearer token
{
  "credential": {
    "host": "smtp.gmail.com",
    "port": 587,
    "secure": false,
    "auth": {
      "user": "[email protected]",
      "pass": "app-password"
    }
  },
  "email": {
    "from": "[email protected]",
    "to": "[email protected]",
    "subject": "Test Email",
    "text": "Hello from Mailing Service"
  }
}

Schedule Email
POST /mail/schedule?date=2025-01-30T10:00:00Z
Content-Type: application/json
Authorization: Bearer token
{
  "credential": {
    "host": "smtp.gmail.com",
    "port": 587,
    "secure": false,
    "auth": {
      "user": "[email protected]",
      "pass": "app-password"
    }
  },
  "email": {
    "from": "[email protected]",
    "to": "[email protected]",
    "subject": "Scheduled Email",
    "html": "<h1>Hello</h1><p>This email was scheduled.</p>"
  }
}
Check Email Existence
GET /mail/[email protected]
Authorization: Bearer token

Response:

{
  "status": true,
  "email": "[email protected]",
  "exists": true,
  "date": "2025-01-30T10:00:00.000Z"
}

Cancel Scheduled Job
POST /mail/cancel/:jobId
Authorization: Bearer token

Example:

POST /mail/cancel/job-id-456
Authorization: Bearer token

Response:

{
  "status": true,
  "jobId": "job-id-456",
  "result": true,
  "date": "2025-01-30T10:00:00.000Z"
}

Redis Persistence

Redis data is persisted using Docker volumes:

redis-data

This ensures queued jobs survive container restarts.


Health & Stability

  • Automatic container restart (unless-stopped)
  • Redis ≥ 7 (BullMQ recommended)
  • Graceful shutdown using exec-form CMD
  • No manual Redis connection handling

Security Best Practices

  • Do not commit .env files
  • Use secrets management in production
  • Restrict Redis access to internal networks
  • Rotate SMTP credentials regularly

Production Notes

  • Use immutable image tags (v1.0.0)
  • Add Redis authentication for public deployments
  • Scale workers horizontally if needed
  • Use a managed Redis service for high availability

License

MIT License


Support & Contributions

Issues and pull requests are welcome. For production hardening or enterprise setups, please open a discussion.

Tag summary

Content type

Image

Digest

sha256:6d930cbfa

Size

97.2 MB

Last updated

7 months ago

docker pull souravbapari/emailing