Skip to content

BullMQ

Guide for background mail delivery via BullMQ on Node.js 18+ (with Express in this example). Messages are sent to Mailexam SMTP from a worker through Nodemailer — convenient for notifications and CI without blocking the HTTP request.

The web layer enqueues a job; BullMQ sends the message asynchronously (similar to Celery in Python or Sidekiq in Ruby).

What you need

  • A Mailexam account and a project with SMTP credentials.
  • A message broker — Redis (required by BullMQ).
  • Node.js 18+ and npm.

Copy from the welcome email (or dashboard) for your project:

  • YOUR_LOGIN — SMTP login (for example, xxxxx);
  • YOUR_PASSWORD — SMTP password (a unique pair with the login);
  • host — YOUR_LOGIN.mailexam.io (matches the login).

1. Dependencies

npm init -y
npm install bullmq ioredis express nodemailer dotenv

Minimal package.json dependencies:

{
  "dependencies": {
    "bullmq": "^5.34.0",
    "dotenv": "^16.0.0",
    "express": "^4.21.0",
    "ioredis": "^5.4.0",
    "nodemailer": "^6.9.0"
  },
  "scripts": {
    "start": "node server.js",
    "worker": "node worker.js"
  }
}

Run Redis locally (Docker):

docker run -d --name redis -p 6379:6379 redis:7-alpine

2. Environment variables

.env file in the project root:

REDIS_URL=redis://localhost:6379/0

MAILEXAM_LOGIN=YOUR_LOGIN
MAILEXAM_PASSWORD=YOUR_PASSWORD
MAILEXAM_PORT=587
MAIL_FROM=noreply@example.test

SMTP host: YOUR_LOGIN.mailexam.io.

3. Nodemailer transport

Same as Expressmail.js in the project root:

// mail.js
const nodemailer = require('nodemailer');

function createTransport() {
  const login = process.env.MAILEXAM_LOGIN;
  const port = Number(process.env.MAILEXAM_PORT || 587);

  return nodemailer.createTransport({
    host: `${login}.mailexam.io`,
    port,
    secure: port === 465,
    auth: {
      user: login,
      pass: process.env.MAILEXAM_PASSWORD,
    },
  });
}

async function sendTest({ to, subject, text }) {
  const transport = createTransport();

  await transport.sendMail({
    from: process.env.MAIL_FROM || 'noreply@example.test',
    to: to || 'user@example.test',
    subject: subject || 'BullMQ + Mailexam',
    text: text || 'Mailexam test from BullMQ',
  });
}

module.exports = { sendTest };

4. BullMQ queue and worker

queue.js:

require('dotenv').config();

const { Queue } = require('bullmq');
const IORedis = require('ioredis');

const connection = new IORedis(process.env.REDIS_URL || 'redis://localhost:6379/0', {
  maxRetriesPerRequest: null,
});

const mailQueue = new Queue('mail', { connection });

module.exports = { mailQueue, connection };

worker.js:

require('dotenv').config();

const { Worker } = require('bullmq');
const IORedis = require('ioredis');
const { sendTest } = require('./mail');

const connection = new IORedis(process.env.REDIS_URL || 'redis://localhost:6379/0', {
  maxRetriesPerRequest: null,
});

new Worker(
  'mail',
  async (job) => {
    const { to, subject, text } = job.data;
    await sendTest({ to, subject, text });
    return { status: 'ok', to: to || 'user@example.test' };
  },
  { connection },
);

5. HTTP endpoint

// server.js
require('dotenv').config();

const express = require('express');
const { mailQueue } = require('./queue');

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

app.post('/mail/test', async (req, res, next) => {
  try {
    const { to, subject, text, body } = req.body ?? {};

    const job = await mailQueue.add('send-test', {
      to,
      subject,
      text: text ?? body,
    });

    res.json({ status: 'ok', queued: true, jobId: job.id });
  } catch (err) {
    next(err);
  }
});

const host = process.env.HTTP_HOST || '127.0.0.1';
const port = Number(process.env.HTTP_PORT || 3000);

app.listen(port, host);

Start Redis, worker, and server:

npm run worker
npm start
curl -X POST http://127.0.0.1:3000/mail/test \
  -H 'Content-Type: application/json' \
  -d '{"to":"user@example.test","subject":"Test","text":"Hello"}'

The message will appear in the Mailexam dashboard → your project → inbox after the worker processes the job.

6. Local development and CI

Environment Recommendation
local Redis + worker process + HTTP server
CI Redis service, REDIS_URL, and Mailexam secrets in pipeline variables

Example for .gitlab-ci.yml:

variables:
  REDIS_URL: redis://redis:6379/0
  MAILEXAM_LOGIN: $MAILEXAM_LOGIN
  MAILEXAM_PASSWORD: $MAILEXAM_PASSWORD
  MAILEXAM_PORT: "587"
  MAIL_FROM: "noreply@example.test"

After an integration test, verify delivery via the Mailexam API.

7. Common issues

Job queued but mail not sent

  • Ensure the worker is running (npm run worker).
  • Check REDIS_URL and Redis connectivity.
  • Inspect worker logs for SMTP errors.

Connection timeout / authentication failed

  • Host must be {login}.mailexam.io, user the same login from the email.

Message not in the dashboard

  • View the inbox of the same Mailexam project.
  • Wait for the worker to finish processing.

See also