Skip to content

Give every outgoing mail a Date, a Message-ID and an encoded subject - #420

Merged
abhishekgahlot2 merged 2 commits into
useagenthq:mainfrom
officialasishkumar:fix/smtp-message-headers
Nov 5, 2026
Merged

abhishekgahlot2 merged 2 commits into
useagenthq:mainfrom
officialasishkumar:fix/smtp-message-headers

Conversation

@officialasishkumar

@officialasishkumar officialasishkumar commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

By opening this pull request you agree to the Contributor License Agreement. On your first pull request a bot asks you to sign it with a one-line comment.

Fixes #418

Every mail UseAgent sends goes through sendSmtp: invitations, sign-up confirmations and email connector run digests. Its header block had no Date and no Message-ID. It also put the subject and the From display name on the wire as raw text. Invitation subjects repeat the names people typed, and digest subjects repeat the prompt and end in …. Raw UTF-8 headers do not name their charset, so some clients show those names garbled. A subject from EmailTransport.sendMessage is the whole first line of the content, with no length cap, so it could pass the 998-character header line limit and carry a bare \r.

What changed

All changes are in backend/src/connectors/email/smtp.ts:

  • Date: in RFC 5322 form (UTC, +0000) and Message-ID: <random uuid@sender's domain> on every message. If the sender address has no usable domain, the Message-ID uses localhost.
  • encodeHeaderText: plain printable ASCII up to 900 characters goes out as typed, so existing ASCII subjects and the "UseAgent" display name are unchanged on the wire. Other text becomes RFC 2047 =?UTF-8?B?...?= words. That covers non-ASCII text, control characters, text containing =?, and longer text. Each word holds at most 45 bytes, so it stays within 75 characters. Words are cut on character boundaries and folded onto continuation lines.
  • fromHeader quotes an ASCII display name as before and encodes any other name.

Tests

New cases in backend/test/smtp-message.test.ts. Each one captures the DATA section of a real SMTP dialog over a local socket and decodes it with a small decoder defined in the test:

  • Date matches RFC 5322 form and falls between the start and end of the send. Message-ID is a uuid under the sender's domain and differs between two sends.
  • A plain ASCII subject goes out as typed. This case passes before and after the change and guards against regressions.
  • A subject in several scripts with an emoji, and an accented display name: the header block is pure ASCII, both decode back to the exact text, and every encoded word is at most 75 characters.
  • A subject of about 2,500 characters: every header line is at most 998 characters, and the subject decodes back.
  • A subject containing \r cannot start a header of its own.

The four cases for new behaviour fail on main. I also parsed captured messages with Python's email package (policy.default). The subject and display name decode exactly, Date parses, and the parser reports no defects.

Checks, run with Bun 1.3.14 against pgvector/pgvector:0.8.6-pg16-bookworm:

  • cd backend && bunx -p typescript@5.9.3 tsc --noEmit: passes
  • bun test test/smtp-message.test.ts test/smtp-timeout.test.ts src/connectors plus the invitation and connector tests: 52 pass, 0 fail

This touches the same file as #419, but different lines. Either can merge first.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

sendSmtp wrote a header block with no Date and no Message-ID, and put
the subject and the From display name on the wire as raw text. That
affects every mail UseAgent sends: invitations, sign-up confirmations
and the email connector's run digests.

- RFC 5322 requires Date, and every message should carry a Message-ID.
  Relays often add neither for a client that connects over the
  network, spam filters score their absence, and some providers refuse
  a message without a Message-ID.
- Invitation subjects repeat the names people typed, and digest
  subjects repeat the prompt and end in an ellipsis. Raw UTF-8 in a
  header says nothing about its charset, so clients and relays that do
  not assume UTF-8 show the names garbled.
- The connector's generic sendMessage uses the whole first line of the
  content as the subject. A line over 998 characters breaks the header
  line limit, and a bare carriage return in it reached the wire.

Every message now has a Date in RFC 5322 form (UTC, "+0000") and a
random Message-ID under the sender's domain. Subject and display name
text that is not printable ASCII, contains "=?", or is longer than one
header line comfortably holds is sent as RFC 2047 encoded words of
UTF-8. Each word holds at most 45 bytes, so it stays within 75
characters. Words are cut on character boundaries and folded onto
continuation lines. Plain ASCII subjects and names go out as before.

New cases in test/smtp-message.test.ts read the captured DATA section
of a real SMTP dialog. They decode the encoded words with a decoder in
the test itself and check the Date, the Message-ID, the line limit and
the carriage return. The four cases for new behaviour fail without the
change.

Fixes useagenthq#418
@officialasishkumar

Copy link
Copy Markdown
Contributor Author

The one backend-test failure here is stop reaches delegated threads > stopping a later turn leaves the threads an earlier turn delegated (expected "queued", got "running"). This pull request only changes the header block in backend/src/connectors/email/smtp.ts and its test, and nothing in the stop or delegation path sends mail. The same test passed in the backend run for #419, which also changes only that file. All other 3,575 tests passed. Could a maintainer re-run the failed job? I don't have permission to.

# Conflicts:
#	backend/test/smtp-message.test.ts
@abhishekgahlot2
abhishekgahlot2 merged commit 4f424fd into useagenthq:main Nov 5, 2026
20 checks passed
@abhishekgahlot2

Copy link
Copy Markdown
Contributor

Merged, thanks @officialasishkumar. Encoding the subject also closes a header injection through a carriage return, which the new test pins down. I merged main into the branch to resolve the test-file conflict with #419.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Give every outgoing mail a Date, a Message-ID and an encoded subject

2 participants