Skip to main content

Simple SMTP daemon which save incoming emails to MongoDB

Project description


Simple SMTP daemon which can save incoming emails to MongoDB and send it to WebSocket.

You should own domain name, MX record and nginx.


$ pip3 install mail2mongo


$ mail2mongo --help
usage: [-h] [-m MONGO_URI] [-db DB_NAME] [-cn COL_NAME] [-ap API_PORT]
               [-sp SMTP_PORT] -d DOMAINS [DOMAINS ...]

Save incoming emails to mongodb

optional arguments:
  -h, --help            show this help message and exit
  -m MONGO_URI, --mongo MONGO_URI
                        Mongo URI (default: mongodb://localhost)
  -db DB_NAME, --db-name DB_NAME
                        Mongo data base (default: mail2mongo)
  -cn COL_NAME, --col-name COL_NAME
                        Mongo collection name (default: emails)
  -ap API_PORT, --api-port API_PORT
                        API port (default: 8080)
  -sp SMTP_PORT, --smtp-port SMTP_PORT
                        SMTPD port (default: 8025)
  -d DOMAINS [DOMAINS ...], --domains DOMAINS [DOMAINS ...]
                        Allowed domains (default: [])

Argument -d/--domains required! It's a domain names list which you own.

From python package

$ mail2mongo -d -m mongodb://

From Docker image

$ docker run -p 8080:8080 -p 8025:8025 inn0kenty/mail2mongo -d -m mongodb://


File /etc/nginx/nginx.conf should contains:

mail {
    server_name <Your MX record>;

    auth_http <local ip:port>/nginx-auth;

    proxy_pass_error_message off;

    server {
        listen 25;
        protocol smtp;
        proxy on;
        smtp_auth none;
        xclient off;

The MX record usually has the form where your domain name. The must be resolved to your ip address.

Local ip:port - local ip address and port (api_port, default 8080).


You can use WebSocket on /ws url. Also you should provide email address.


import asyncio
import aiohttp

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.ws_connect('ws://') as ws:
            async for msg in ws:
                if msg.type == aiohttp.WSMsgType.TEXT:

loop = asyncio.get_event_loop()

You can get messages of two types:

If something go wrong:

{'type': 'error', 'payload': {'msg': 'Error message'}}

If you receive new email:

{'type': 'new_mail', 'payload': {'from': '', 'to': '', 'subject': 'Foo bar', 'text': 'Some message', 'timestamp': '1970-01-01T00:00:00.000000+00:00', '_id': '5ae0988c754ea76f22935378'}}

_id - ObjectId in MongoDB.

Similar payload saved to MongoDB.

If for some reason MongoDB is down, service will continue try to save payload to mongo. At each next step service will be double sleep time (default is 60 seconds).

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
mail2mongo-0.3.3-py3-none-any.whl (12.5 kB) Copy SHA256 hash SHA256 Wheel py3
mail2mongo-0.3.3.tar.gz (6.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page