Send email from Node.js

Send email from Express, Fastify, NestJS or any Node.js app with TrivSend's SDK, or point Nodemailer at TrivSend's SMTP server. Every email is signed for your own domain and logged with its delivery result.

Start free · Node.js SDK in the docs

Install and send

bash
npm install trivsend
export TRIVSEND_API_KEY=ts_...
js
import { TrivSend } from "trivsend";

const trivsend = new TrivSend(process.env.TRIVSEND_API_KEY);

const { data, error } = await trivsend.emails.send(
  {
    from: "Acme <noreply@notify.example.com>",
    to: "asha@example.com",
    subject: "Welcome to Acme",
    html: "<p>Your account is ready.</p>",
    text: "Your account is ready.",
  },
  { idempotencyKey: "welcome-42" },
);
if (error) throw new Error(`${error.name}: ${error.message}`);
console.log(data.id);

The SDK has no dependencies, is typed, and runs on Node.js 18 and later. Every call resolves to { data, error } and never throws for an API error. It also covers batches, domains, templates, contacts, broadcasts, webhooks and validations. Using Next.js? See Next.js email.

Nodemailer

Already using Nodemailer? Change the transport and keep the rest of your code:

js
import nodemailer from "nodemailer";

const transport = nodemailer.createTransport({
  host: "smtp.trivsend.com",
  port: 587, // upgrades to TLS; or 465 with secure: true
  auth: { user: "trivsend", pass: process.env.TRIVSEND_API_KEY },
});

await transport.sendMail({
  from: "Acme <noreply@notify.example.com>",
  to: "asha@example.com",
  subject: "TrivSend test",
  text: "It works",
  headers: { "X-TrivSend-Priority": "high" },
});

The X-TrivSend-Priority: high header sends sign-in codes and password resets before bulk mail (SMTP relay).

Know what happened to each email

TrivSend calls your app when an email bounces, is marked as spam or fails. Verify the signature with the SDK, using the raw body before any JSON parsing:

js
import express from "express";

app.post("/hooks/trivsend", express.raw({ type: "application/json" }), (req, res) => {
  let event;
  try {
    event = trivsend.webhooks.verify({
      payload: req.body.toString(),
      headers: {
        id: req.header("webhook-id"),
        timestamp: req.header("webhook-timestamp"),
        signature: req.header("webhook-signature"),
      },
      webhookSecret: process.env.TRIVSEND_WEBHOOK_SECRET,
    });
  } catch {
    return res.sendStatus(401);
  }
  if (event.type === "email.bounced") {
    // mark the address as bad in your database
  }
  res.sendStatus(204);
});

Webhooks are retried when your app is down, and any delivery can be sent again (webhooks).

Built for app email

Also see: Sending OTP emails from Node.js · SMTP relay · Transactional email