To make things

It was a stagnant ditch of despair, unrefreshing. In an age when only state officials are allowed to set fire to lights, let’s take a few simple lines of Python code and throw some more junk into the stagnant water.

Our goal is to make the Internet more messy and smelly, so we’re not bound by rules. Today, it’s the mail system.

Spam, a lot of people suffer from it, but that’s not what we’re going to do today. To think in the opposite way, we don’t hate it, and we welcome it. For example, I need to batch up 10,000 Github accounts. Github successfully registers through email verification. At this time, we need a mail server. The specific process is as follows:

At this point, I thought, can we automate the email receiving part? Automation involves two things: 1) having an infinite number of mailboxes at your disposal and 2) making it very easy to get your mail content to parse

After a bit of thinking, a solution emerges in my mind, which is super simple.

2) The mail system can accept any mail with a certain suffix. 3) A simple REST interface is provided to return the mail JSON for parsing

So let’s get straight to the point and talk about how to do that.

Domain Name Resolution Configuration

Open domain name management, take Ali Cloud as an example, add two information

Add an A record that points to the service IP address we will deploy

A mx 6 x. 216.2 xx. XxCopy the code

Add an MX record pointing to the domain address for receiving mail configured above.

MX	*	mx.sayhiai.com
Copy the code

Note the * sign, which means that all domain names, including secondary domain names, will be received. Such as [email protected] and [email protected]. Is it endless?

Write the SMTPD server

It is very easy to implement an SMTPD server using Python, using the AIosmtpd library. By default, the message is BASE64 encoded after receiving it, and it is divided into many parts and types, which is more troublesome to parse. You need to guess the code and recursively splice the messages and so on. The code snippet is as follows:

def decode_str(s):
    value, charset = decode_header(s)[0]
    if charset:
        value = value.decode(charset)
    return value
def guess_charset(msg):
    charset = msg.get_charset()
    if charset is None:
        content_type = msg.get('Content-Type'.' ').lower()
        pos = content_type.find('charset=')
        if pos >= 0:
            charset = content_type[pos + 8:].strip()
    return charset
def print_part(msg):
    rs = ""
    content_type = msg.get_content_type()
    if content_type == 'text/plain' or content_type == 'text/html':
        content = msg.get_payload(decode=True)
        charset = guess_charset(msg)
        if charset:
            content = content.decode(charset)
        rs = rs + str(content)
    else:
        rs = rs + str(content_type)
    return rs
def print_info(msg):
    rs = ""
    if (msg.is_multipart()):
        parts = msg.get_payload()
        for n, part in enumerate(parts):
            if part.is_multipart():
                rs = rs + print_info(part)
            else:
                rs = rs + print_part(part)
    else:
        return print_part(msg)
    return rs
Copy the code

Writing REST services

After parsing the message, we put the content in SQlite3. The next step is to write the data interface.

Our goal is to make mail retrieval as easy as possible, and REST+ JSON is preferred. Python’s Flask library is easily the simplest and most suitable.

import json
from flask import Flask
from flask import send_file
from data import dataInstance
app = Flask(__name__)
dao = dataInstance
def web_start(host, port):
    app.run(host=host, port=port)
@app.route('/')
def index(a):
    return send_file('static/index.html')
@app.route('/all')
def msg_all(a):
    rows = dao.read_all()
    return json.dumps(rows)
@app.route('/from/<addr>')
def msg_from(addr):
    rows = dao.read_from(addr)
    return json.dumps(rows)
@app.route('/to/<addr>')
def msg_to(addr):
    rows = dao.read_to(addr)
    return json.dumps(rows)
Copy the code

As you can see, three interfaces are provided: 1) /all gets all messages 2) /from/{addr} finds messages by sender 3) /to/{addr} finds messages by receiver

Each query returns 100 more entries, which you don’t need anyway.

test

Set up an account using [email protected]. Use curl or your browser to retrieve email information:

curl -XGET http://sayhiai.com:14000/to/[email protected]
Copy the code

Or use sayhiai.com:14000/ check it out online.

Remember to build one yourself. Don’t forget that the SMTPD port is 25. It is not possible to bind other ones

At the end

At this point, a perfect loop is complete. I remember a while ago some students were still unhappy about Microsoft’s acquisition of Github, it’s time to give you a vent, pay attention to the IP.

Look at that. Even with github as big as it is, it’s easy to imagine how many other sites on the web could use the same approach.

I have put the code on github 🙂 🙂 🙂 🙂 github.com/lycying/cra…

If you do something bad, don’t leave your name