Monitor a Temp Mail Inbox With a Bot: What Works Today

Monitor a Temp Mail Inbox With a Bot: What Works Today

Monitor a Temp Mail Inbox With a Bot: What Works Today

If you want a message the moment mail arrives, start our Telegram bot. If you want a script that reads codes in tests, point it at a local mail catcher, because the MTempMail API is switched off.

The first version of this post built a Python bot on our API, partly as a screenshot that no longer loads. Here are two ways that work without it.

Option 1: the Telegram bot, for people

  1. Open @mtempmail_bot in Telegram and press Start.
  2. It gives you an address ending in @mtempmail.com.
  3. Use the address on a site.
  4. New mail shows up in the chat, so you don't have to keep a page open.

All commands and limits are here. Two things to know: messages are shown inside Telegram, so they also pass through Telegram's servers, and the inbox rules are the same as on the website (receive only, 24 hours, no password).

Prefer no Telegram? The Android app can send a push notification, and the website checks by itself while it is open.

Option 2: a small watcher, for tests

In a test environment you control where the mail goes. Let your app send it to Mailpit, a free mail catcher with a web page and an API. Its integration page documents a convenient address: /view/latest.txt returns the newest message as text, a search can be added, and 404 means that nothing was found.

Start it with Docker:

docker run -d --name mailpit -p 8025:8025 -p 1025:1025 axllent/mailpit

Then wait for a mail to a certain recipient:

import re, time, urllib.parse, urllib.request, urllib.error

def wait_for_code(recipient, seconds=30, base="http://localhost:8025"):
    q = urllib.parse.quote(f"to:{recipient}")
    url = f"{base}/view/latest.txt?query={q}"
    end = time.time() + seconds
    while time.time() < end:
        try:
            text = urllib.request.urlopen(url, timeout=5).read().decode()
            found = re.search(r"\b(\d{6})\b", text)
            if found:
                return found.group(1)
        except urllib.error.HTTPError as e:
            if e.code != 404:
                raise
        time.sleep(1)
    raise TimeoutError(f"no code for {recipient}")

print(wait_for_code("[email protected]"))

It uses only Python's standard library. Notes from practice:

  • Use a new recipient per test, for example with a timestamp. Otherwise you read yesterday's code.
  • Set a limit. A watcher without an end blocks your pipeline.
  • Wait a second between tries. Local delivery is quick, and hammering helps nobody.
  • Print the message when it fails. Nine times out of ten the template changed and the code is now seven digits or inside a link.
  • To send an alert, replace the last line with a call to your chat tool's webhook.

Why not watch a public inbox with a script?

  • Pages change and the script breaks without notice.
  • Constant requests slow the service down for others and can be blocked.
  • Anybody who knows the address sees the same mail. Tokens from a staging system don't belong there.
  • Scripts that open accounts elsewhere in large numbers break those sites' rules, and we don't support that.

A bot that monitors an inbox you own is easier and more honest: an ordinary mailbox with IMAP, read by the imaplib module that comes with Python. A catch-all on a subdomain of yours gives you any number of test addresses.

Which one do I need?

GoalUse
Read a code on my phoneTelegram bot or Android app
End-to-end test in CIMailpit and the script above
Test in PostmanPostman with Mailpit
Receive mail from the internet automaticallyOwn domain with IMAP, or a paid testing service

Common questions

Does the Telegram bot need the API?

No. It is a separate part of our service and works while the public API is off.

Can the bot tell me about mail for several addresses?

Look at the bot's own menu for what it offers today. We'd rather not list features that may change.

Is there a token that protects my inbox?

No. Our inboxes have no token or password. The old version of this post said otherwise.

Tags:
#temp mail bot # monitor temp mail inbox # telegram temp mail bot # python email monitor test
Zubair Rahmani
Written by
Zubair Rahmani
Zubair is an app and website developer with 7 years of experience. Zubair built and runs MTempMail, a free temporary email service. Contact
Categories