Guides / Node.js

Test Node.js emails locally

RustMail is a local SMTP mail catcher. Your JavaScript app keeps sending mail the way it does in production, and every message lands in a web UI instead of a real inbox. Four steps, about two minutes.

  1. Start RustMail

    One container, SMTP on port 1025 and the web UI on 8025.

    docker run -p 1025:1025 -p 8025:8025 smyile/rustmail:latest
  2. Point Node.js at it

    In mailer.js, send mail to localhost:1025 with no TLS and no credentials.

    import nodemailer from "nodemailer";
    
    export const transport = nodemailer.createTransport({
      host: "localhost",
      port: 1025,
      secure: false,
    });
  3. Send a test email

    Then open localhost:8025: the message is already in the inbox, with its HTML, headers and links.

    await transport.sendMail({
      from: "[email protected]",
      to: "[email protected]",
      subject: "Welcome",
      text: "Hello from Node.js",
    });
  4. Assert on it in CI

    In GitHub Actions, start RustMail, run tests that send mail with the mailer.js settings from step 2, then fail the job when the email never arrives.

    - name: Start RustMail on localhost:1025
      uses: rustmailapp/rustmail-action@v1
      with:
        mode: start
    
    - name: Run the tests that send mail over SMTP
      run: npm test
    
    - name: Fail if the welcome email never arrived
      uses: rustmailapp/rustmail-action@v1
      with:
        mode: assert
        assert-count: 1
        assert-subject: "Welcome"

Good to know

  • The same host and port work with any SMTP client library, not only Nodemailer.

Other frameworks

LaravelDjangoRuby on RailsSpring Boot