Send email from NestJS over SMTP

Inject a Nodemailer transport in NestJS and send over Unitpost SMTP. Same credentials as any other stack.

What are the SMTP settings?

Hostsmtp.unitpost.com
Usernameunitpost(always this literal value)
PasswordYour Unitpost API key(must carry emails:send)
Port (STARTTLS)587(or 2587 if blocked)
Port (implicit TLS)465(or 2465 if blocked)
From addressAny address on a verified domain

How do I set it up?

  1. Install Nodemailer

    Shell
    npm install nodemailer
    npm install -D @types/nodemailer
  2. Provide a transporter

    TypeScript
    import { Injectable } from "@nestjs/common";
    import nodemailer from "nodemailer";
    
    @Injectable()
    export class MailService {
      private transporter = nodemailer.createTransport({
        host: "smtp.unitpost.com",
        port: 587,
        secure: false,
        auth: {
          user: "unitpost",
          pass: process.env.UNITPOST_API_KEY,
        },
      });
    
      sendWelcome(to: string) {
        return this.transporter.sendMail({
          from: "you@yourdomain.com",
          to,
          subject: "Welcome",
          html: "<p>Sent via SMTP.</p>",
        });
      }
    }
  3. Verify delivery

    The send shows up in Activity. See also Nodemailer.