MailFlatDocs
Documentation/Common test cases/One-time codes

Extracting one-time codes from email

MailFlat pulls the verification code out of the message for you and hands it over as a field, so your test never runs a regex against an email body.

How it works

Every incoming message is scanned for a one-time code. The result lands in otp_code, and every client exposes it directly. Codes are only accepted when a context word (code, OTP, PIN, verification) sits right next to them, so a stray number in a footer or a year in a copyright line is never mistaken for a code.

Read the code

The waiting helpers return the code itself, so this is usually the whole of it.
otp = inbox.wait_for_otp(timeout=30)
assert otp == "482913"

Read it off a message you already have

If you fetched the message for other assertions, the code is already on it.
msg = inbox.latest()
assert msg.otp is not None, "no code was extracted"

Worth knowing

Encrypted inboxes cannot expose codes
On an end-to-end encrypted inbox the server never sees the body, so otp_code comes back empty and the response carries encrypted: true with a note. Inboxes opened through the API are always plain text for exactly this reason.