smtp-relay

Open-source SMTP server

One SMTP server, any destination

Your apps speak SMTP. smtp-relay routes each email through flows and filters, then writes it to a folder, relays it through Amazon SES or another SMTP server, or stores it in S3 and Cloud Storage with an event on SQS or Pub/Sub.

docker run -p 10025:10025 \
  -v "$(pwd)/emails:/smtp-relay/received_emails" \
  loopingz/smtp-relay:latest configs/fake-smtp-docker.jsonc
One envelope routed to a folder, Amazon SES, S3 and SQS, Cloud Storage and Pub/SubFolderAmazon SESS3 / SQSGCS / Pub/Sub

In 1 min 22 s

What is smtp-relay?

From a local debug inbox to SES, S3 and Pub/Sub, with the same server and one configuration file.

What is smtp-relay: watch the video on YouTube
The narration is an AI-generated voice.

How it works

The life of an email

From the SMTP conversation to the places the email ends up, in four steps.

An email goes through the SMTP session, is seen by three flows, passes the filters of two of them and fans out to a folder, Amazon SES, S3 and SQS, Cloud Storage and Pub/Sub Your appSMTP sessionconnectMAIL FROMRCPT TODATAsupportbillingarchiveacceptedrefusedacceptedFolderfileAmazonSESS3SQSGCSPub/Sub
  1. The SMTP session

    Your app connects and talks SMTP: MAIL FROM names the sender, one RCPT TO per recipient, then DATA sends the message. smtp-relay answers each command as it comes.

    MAIL FROM:<app@mydomain.com>     250
    RCPT TO:<support@mydomain.com>   250
    DATA                           354
  2. Every flow sees every email

    A configuration declares flows. They are not tried one after the other: every flow sees every connection and every email, and any number of them can take the same email.

  3. The filter gate

    Each flow's filters (IP or recipient whitelist, SMTP login, HTTP check, SPF/DKIM/DMARC) decide whether it takes the email: accepted or refused. Recipients are judged one by one, and one that no flow accepts is refused at RCPT TO.

  4. Fan out to the outputs

    Each flow that took the email runs its outputs: write it to a folder, log it, send it with Amazon SES or another SMTP server, store it in S3 or Cloud Storage, publish an event on SQS or Pub/Sub.

Routing

Pick the recipients, see where the email goes

Three flows: one for support, one for billing, one that archives everything sent to the domain. A flow takes the email if it accepted at least one of its recipients, whatever their order.

RCPT TO

support@mydomain.com: 250, taken by support and archive.

Flows and outputs

  • supporttakes the email
    to: support@mydomain.com
    SQS queue support
  • billingnot for this email
    to: billing@mydomain.com
    SQS queue billing
  • archivetakes the email
    to: regexp:.*@mydomain.com
    S3 bucket archive

routing.yaml

$schema: https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json
flows:
  support:
    filters:
      - type: whitelist
        to: ["support@mydomain.com"]
    outputs:
      - type: aws
        pubsub:
          topic: https://sqs.us-east-1.amazonaws.com/123456789012/support
  billing:
    filters:
      - type: whitelist
        to: ["billing@mydomain.com"]
    outputs:
      - type: aws
        pubsub:
          topic: https://sqs.us-east-1.amazonaws.com/123456789012/billing
  archive:
    filters:
      - type: whitelist
        to: ["regexp:.*@mydomain.com"]
    outputs:
      - type: aws
        storage:
          bucket: mydomain-mail-archive
          path: ${iso8601}_${id}.eml
bind: 0.0.0.0
options:
  authOptional: true

Typical setups

Start from a working configuration

Each one is a file of the repository's configs/ folder, also shipped in the Docker image.

  • Local debug inbox

    Catch every email your app sends in development and write each one as a file in a folder.

    {
      "outputs": [
        {
          "type": "file",
          "path": "./received_emails/"
        }
      ]
    }
  • Relay to Amazon SES

    Keep your apps on SMTP and send through the Amazon SES API, like the former aws-smtp-relay.

    {
      "outputs": [
        {
          "type": "aws",
          "ses": true
        }
      ]
    }
  • Inbound to cloud storage and events

    Receive the mail of your domain, store it in a Cloud Storage bucket and publish its metadata on Pub/Sub.

    {
      "outputs": [
        {
          "type": "gcp",
          "storage": {
            "bucket": "myemail",
            "path": "${iso8601}_${id}.eml"
          },
          "pubsub": {
            "topic": "myemail"
          }
        }
      ]
    }
  • SMTP login from the environment

    Require SMTP AUTH, with the username and password read from SMTP_USERNAME and SMTP_PASSWORD.

    {
      "filters": [
        {
          "type": "static-auth"
        }
      ]
    }