---
title: "Mail Relay"
description: "Keep Google Workspace as your mailbox provider and get every message in Primitive too, with webhooks, Functions and the API."
canonical: "https://docs.primitive.dev/docs/mail-relay"
last-updated: "2026-05-17T00:00:00.000Z"
---

# Mail Relay

A mail relay domain keeps your existing mailbox provider. Your team keeps using Gmail as
before, and Primitive gets a copy of the domain's mail: incoming mail is stored and
triggers webhooks and Functions like any other domain, and mail your team sends from
Gmail is recorded in your outbox.

Mail relay domains currently support Google Workspace. They are available to accounts
with mail relay enabled.

## How mail flows

- Inbound: the domain's MX points at a Primitive mail relay first, with Google's MX kept as a backup at a higher priority number. The relay forwards each message to your Google Workspace mailboxes and, at the same time, stores a copy in Primitive.

- Outbound: Google Workspace routes outgoing mail through the relay. The relay checks the message really comes from your Workspace, sends it on, and records it in your Primitive outbox with its delivery result.

- Inside Workspace: by default, mail between users of the same Workspace stays inside Google and is not stored in Primitive. An optional routing rule (below) sends it through the relay as well.

## Setup

Add the domain on the Domains page and choose **Keep my existing mailbox provider (mail
relay)**. Then open the **Mail relay** page, or **Open relay setup** on the domain's card.
Setup shows one step at a time, in the order below, and the domain's card says which step
it is on. Every step is required: the relay needs all of them to keep mail flowing.

The domain card shows the mailbox provider as a badge, with its size limit against
Primitive's. Primitive detects the provider when you add the domain: from the MX while it
still points at your provider, otherwise from your SPF record's include (for Google
Workspace, `include:_spf.google.com`) or Google Workspace's DKIM key. The relay forwards
only to Google Workspace today; a domain on any other provider, or one where nothing in
DNS names a provider, is shown as not supported yet.

Switch MX last, after the domain verifies and the Google Workspace steps are done: if MX
changes before Google is ready, Gmail sees all incoming mail arriving from the relay's
address and may treat it as spam, and before the domain verifies the relay asks senders
to try again later, so mail is delayed. Google says settings can take up to 24 hours to
apply, so we recommend waiting 24 hours after saving the inbound gateway before you
change the MX. Until then, mail can land in spam or be refused.

- Google DKIM key. In the Google Admin console, as a super administrator, open Apps, Google Workspace, Gmail, Authenticate email. If this domain already shows Authenticating email with DKIM, do not generate a new record: enter its prefix selector on the setup page (the part before `._domainkey` in the DNS Host name Google shows) if it is not `google`, and go on. Otherwise select the domain and generate a 2048-bit record. Keep that page open, and do not select Start authentication yet: the record must be published first. If you changed the prefix selector when generating, enter it on the setup page. If Gmail was only just turned on for your organization, Google can take 24 to 72 hours before it lets you generate the key.

- DNS. Publish the records in the setup page's one list: the ownership TXT, one SPF record, DMARC, the relay's DKIM records, Primitive's DKIM records and Google's DKIM key from step 1. Publish each record as the list shows it. Where the relay's DKIM records are CNAMEs and the SPF record includes a name of your domain's own, Primitive keeps them pointing at the relay's current keys and addresses, so they never need editing; where the list shows the relay's key or include directly, or no standby DKIM record, a later change to the relay means updating them. Do not change the MX yet. The domain verifies by itself once every record passes; to check right away, click Verify the domain.

- DKIM signing. Back in Authenticate email, select Start authentication. Google says DKIM can take up to 48 hours to start working after you add a key. It is done when the status reads Authenticating email with DKIM. Do not add the outbound routing rule until then: until Google signs, the relay refuses every outbound message.

- Inbound gateway. In Gmail, Spam, Phishing and Malware, Inbound gateway, add the relay addresses shown on the setup page, select "Automatically detect external IP", and require TLS.

- Catch-all. Create a group to receive mail for addresses that have no mailbox, and a routing rule that sends unknown recipients to it, as described on the setup page. This stops Google bouncing mail that the relay already accepted.

- Outbound route. In Gmail, Hosts, add the relay hostname from the setup page on port 25 with TLS required. Then add a Routing rule for outbound mail whose envelope sender matches your domain, routed to that host. The setup page keeps this rule's step locked until DKIM signing is confirmed.

- Test sending. Send any message from a mailbox on the domain to the one-time address the setup page shows. It confirms your mail leaves through the relay.

- Wait. The setup page counts 24 hours from when you save the inbound gateway. Mail keeps going straight to Google until the MX changes, so waiting is safe.

- MX. Add an MX record for the relay at priority 10, and keep Google's MX with its priority changed to 20 or higher. The relay must have the lowest number, and Google's MX is the only other one allowed: senders try the relay first and fall back to Google if it cannot be reached.

- Test receiving. The setup page sends one message to a mailbox you choose and one to an address with no mailbox, then confirms Google accepted both.

The setup page checks every DNS record and the Google DKIM key for you. The Google Admin
steps cannot be seen from outside Google, so you confirm them yourself; the two tests then
check the outbound route, DKIM signing and the catch-all.

### SPF

A domain must have exactly one SPF record. The setup page shows the record to publish; it
authorises the relay, Google and Primitive. If you send from other services, add their
mechanisms to that same record rather than publishing a second one. Two SPF records (or
two DMARC records) make receivers treat both as broken, and the setup page reports them.

### DMARC

A domain must have exactly one DMARC record. The record the setup page shows sends DMARC
reports to `dmarc_reports@system.primitive.dev`. If there is already a DMARC record at
`_dmarc.<your domain>`, keep that one record and add `mailto:dmarc_reports@system.primitive.dev` to its `rua` list,
separated from the other addresses by a comma, instead of publishing a second record.

### Optional: store mail between your own users

In the Google Admin console, go to **Gmail > Routing**.

- Click Add Another Rule.

- Give it a name, such as `Primitive internal`.

- Check only Internal outbound messages.

- Under action, choose Modify message.

- Check Change the route and choose the relay host from the outbound route.

- Click Show more options.

- Check only Active user account.

- Under Envelope filter, check Only affect specific envelope senders.

- Choose Pattern match.

- Enter the same pattern as your outbound routing rule.

- Click Save.

Internal mail is then stored in Primitive like other mail: in your outbox for the sender
and as an incoming message for the recipient.

The trade-off: internal mail then depends on the relay. If the relay cannot be reached,
Google retries and that mail is delayed. The relay accepts internal mail only when it
carries your Google DKIM signature, and whether Google signs mail routed this way has not
been confirmed yet; if it does not, internal mail on the rule bounces.

Try the rule on one organizational unit first:

- Send a message between two of its users.

- Check that the recipient gets the message once, with no bounce.

- Check that Primitive shows it in your outbox and as an incoming message.

If the test bounces, delete the rule, which returns internal mail to staying inside
Google. Widen the rule only after it passes.

## What you get

- Inbound mail is stored and delivered to your webhooks and Functions as usual. The email object and the `email.received` event carry a `relay` object, for example `{ "hostname": "relay-mx.example.com", "via": "mail_relay" }`, and `relay: null` for mail that did not come through a relay.

- Delivery to Google Workspace is shown per recipient: `GET /v1/emails/{id}` returns `relay.delivery`, a list of `{ recipient, status, smtp_code, enhanced_status_code, smtp_response, at }` with `status` one of `delivered`, `deferred` or `bounced`, and the dashboard shows the same on the email page, with a "Not delivered to Google Workspace" badge naming the reason when Google did not take it for any recipient. Each refusal is explained in plain words from Google's SMTP answer (no such mailbox, mailbox full, refused under Google's security or spam policy, or larger than Google accepts), with the raw answer alongside. A message the relay stopped retrying is shown as a delivery timeout, not as a refusal by Google. Google only allows 25 MB of attachments (50 MB per message, encoding included) while Primitive allows 50 MiB on your account, so a large message can be stored in Primitive but not delivered to Google. The message stays stored in Primitive either way. The `email.received` webhook fires when the message reaches Primitive, before the Google result is known, so it does not include `delivery`, and no separate webhook fires for the Google result.

- Relayed mail Primitive refused (for example over a plan limit, or a protocol violation) is not stored in Primitive. Delivery to the mailbox is separate and usually still succeeds. The domain's Relay setup section shows how many messages Primitive refused in the last 7 days, by reason.

- Mail sent from Gmail appears in your outbox, threaded with the conversation, with its delivery result for each recipient.

- Sending through the API from the domain works as for any verified domain. Mail sent to an address on a relay domain always goes through the relay, so it reaches the mailbox as well as Primitive.

## While setup is in progress

Until the MX switch, inbound mail keeps going straight to Google and is not stored in
Primitive. The relay starts accepting the domain's mail as soon as it verifies, which is
why the MX switch comes after verification: mail arriving at the relay for a domain that
has not verified yet is answered with a temporary failure, so senders retry and nothing
bounces, but it is delayed.

## Limits

- The relay accepts messages up to 50 MiB, in both directions, and stores them in Primitive at that size.

- Mail sent from Gmail is recorded in the outbox; it does not trigger webhooks or Functions.

- Addresses with quoted or non-ASCII local parts are delivered to Google Workspace but not stored in Primitive.

## Removing the relay

Remove things in this order, so mail keeps flowing at every step:

- Outbound first. In the Google Admin console, delete the outbound routing rule, and the rule for mail between your own users if you added one. Keep the relay host for now. Within 24 hours, mail you send goes straight out from Google again.

- MX. Delete the relay's MX record, leaving every Google MX record (`smtp.google.com`, or the records Google gave you).

- Wait. Wait one day, plus your old MX TTL if it is longer than an hour. Mail already accepted by the relay is still delivered.

- Catch-all. Delete the catch-all routing rule, then the catch-all group. Mail to unknown addresses bounces again, instead of disappearing into a group with no members.

- Inbound gateway and host. Remove the relay addresses from the inbound gateway, then delete the relay host in Gmail, Hosts.

- SPF and DKIM. Remove the relay's include from your SPF record, and the relay DKIM records. Keep Google's.

Maintained by the [primitive.dev team](/about) · [Trust & security](/trust)Last updated: May 17, 2026
