# Real send check: can we find our sent email in Gmail again?

Sending from the CRM (HUB-119) never sends an email twice because the `gmail.send` job can find
an email it may already have sent by **our own Message-ID**, and the incremental sync matches the
sent copy to its row by the same id. The tests use a fake Gmail, so this has to be checked with a
real mailbox (HUB-172). Run it on staging, or locally with the Google app's settings.

**Gmail replaces the `Message-ID` header we set** with its own (`<CA…@mail.gmail.com>`), whatever
the domain: checked on 8 Oct 2026 with a real Workspace mailbox, for `@127.0.0.1` and for
`@crm-staging.admedia.com`. It keeps other headers, so every email also carries our id in
`X-CRM-Message-ID`. The send job lists the user's emails from shortly before the attempt and
compares that header (Gmail's search can't look inside it), and the sync matches on it.

## What you need

- The API and the worker running, with `API_URL`, `APP_URL`, `GOOGLE_CLIENT_ID` and
  `GOOGLE_CLIENT_SECRET` set, and `${API_URL}/api/v1/mailboxes/gmail/callback` registered as a
  redirect URI in the Google app.
- A Google Workspace account you can sign in to in a browser, and a second address to send to.
- Database access (to look at two rows).

## Steps

1. **Connect the mailbox.** Swagger (`/docs`), sign in (or use the dev login headers), then
   `POST /api/v1/mailboxes/gmail/connect` and open the returned `authUrl`. After approving,
   `GET /api/v1/mailboxes/me` shows `syncing`, then `connected` once the initial sync is done.

2. **Send an email** with `POST /api/v1/inbox/send`:

   ```json
   {
     "to": ["<your second address>"],
     "subject": "CRM send check <today's date>",
     "bodyHtml": "<p>Real send check (HUB-172).</p>",
     "idempotencyKey": "real-send-check-0001"
   }
   ```

   Note the `id` it returns (the message id).

3. **Note the Message-ID we chose:**

   ```sql
   SELECT id, rfc_message_id, send_status, provider_message_id, send_attempted_at
   FROM email_messages WHERE id = '<message id>';
   ```

   Within a minute `send_status` is `sent` and `provider_message_id` is set.

4. **Compare it with Gmail.** In Gmail, open the email in **Sent** → ⋮ → **Show original**. The
   `X-CRM-Message-ID:` line (the name may be written `X-Crm-Message-ID`) must be exactly the
   `rfc_message_id` from step 3 (for example `<0199…@crm-api.example.com>`). The `Message-ID:`
   line is Gmail's own. **Take a screenshot of the "Show original" page**, showing both lines.

5. **Check the sync left one copy.** Wait for the incremental sync (at most a few minutes), then:

   ```sql
   SELECT COUNT(*) AS rows_for_this_email
   FROM email_messages
   WHERE mailbox_id = (SELECT mailbox_id FROM email_messages WHERE id = '<message id>')
     AND provider_message_id = '<provider_message_id from step 3>';
   ```

   It must be `1`, and `GET /api/v1/inbox/threads?folder=sent` shows the email once. The row's
   `rfc_message_id` is now Gmail's Message-ID, the one recipients reply to.

6. **Double send.** Send the request from step 2 again, same `idempotencyKey`: the answer is
   `200` with the same `id`, and Gmail Sent still has one copy.

## If X-CRM-Message-ID is missing

If "Show original" has no `X-CRM-Message-ID` line, or it differs from `rfc_message_id`, the search
before a resend cannot find an email that was sent, and a send whose outcome was unknown could go
out twice. Stop using CRM sending and tell the team.

## Local check (8 Oct 2026, real Workspace mailbox)

- Gmail replaced the Message-ID (`<…@127.0.0.1>` became `<CAHZ_LC…@mail.gmail.com>`; a direct
  send with `<…@crm-staging.admedia.com>` was replaced too) and kept `X-CRM-Message-ID`.
- An email was sent, then its row put back as a crash after Gmail accepted it leaves it
  (`sending`, no `provider_message_id`, attempt 3 minutes ago) and the job run again: "already in
  Gmail", matched to the same Gmail message, and Gmail Sent still had one copy.

## Result (staging)

Fill this in and attach the screenshot to the ticket (HUB-172).

|                                                       |                     |
| ----------------------------------------------------- | ------------------- |
| Date, environment                                     |                     |
| Who ran it                                            |                     |
| Message-ID in the CRM (step 3)                        |                     |
| X-CRM-Message-ID in "Show original" (step 4)          |                     |
| Same?                                                 |                     |
| Rows for this email after the sync (step 5)           |                     |
| Repeat with the same key sent a second copy? (step 6) |                     |
| Screenshot                                            | attached to HUB-172 |
