How hard is it to build an Email client? – Python

Question:

I’m venturing in unknown territory here…

I am trying to work out how hard it could be to implement an Email client using Python:

  • Email retrieval
  • Email sending
  • Email formatting
  • Email rendering

Also I’m wondering if all protocols are easy/hard to support e.g. SMTP, IMAP, POP3, …


Hopefully someone could point me in the right direction 🙂

Asked By: RadiantHex

||

Answers:

If I were you, I’d check out the source code of existing email-clients to get an idea: thunderbird, sylpheed-claws, mutt…

Depending on the set of features you want to support, it is a big project.

Answered By: static_rtti

The Python language does offer raw support for the needed protocols in its standard library. Properly using then, and, properly parsing and assembling a "modern day" e-mail message, however can be tough to do.

Also, you didn’t say if you want to create a graphical interface for your e-mail client — if you want to have a proper graphical interface — up to the point of being usable, it is quite a lot of work.

Local e-mail storage would be the easier part – unless you want to properly implement an mbox file format RFC-4155 so that other software can easily read/write the messgaes you have fetched, you can store them in as Python Objects using an ORM or an Object Oriented database, such as ZODB, or MongoDB.

If you want more than a toy e-mail app, you will have a lot of work – properly encoding e-mail headers, for example, server authentication and secure authentication and transport layers, decoding of the e-mail text body itself for non ASCII messages. Although the modules on the Python standard library do implement a lot of that, their documentation falls short on examples – and a complete e-mail client would have to use all of then.

Certainly the place to start an e-mail client, even a toy one, would be taking a look on the most recent RFC’s for e-mail (and you will have to pick then from here http://www.ietf.org/rfc/rfc-index since just looking for "email rfc" on google gives a poor result).

Answered By: jsbueno

I think you will find much of the clients important parts prepackaged:

Email retrieval – I think that is covered by many of the Python libraries.

Email sending – This would not be hard and it is most likely covered as well.

Email formatting – I know this is covered because I just used it to parse single and multipart emails for a client.

Email rendering – I would shoot for an HTML renderer of some sort. There is a Python interface to the renderer from the Mozilla project. I would guess there are other rendering engines that have python interfaces as well. I know wxWidgets has some simple HTML facilities and would be a lot lighter weight. Come to think about it the Mozilla engine may have a bunch of the other functions you would need as well. You would have to research each of the parts.

There is lot more to it than what is listed above. Like anything worth while it won’t be built in a day. I would lay out precisely what you want it to do. Then start putting together a prototype. Just build a simple framework that does basic things. Like only have it support the text part of a message with no html. Then build on that.

I am amazed at the wealth of coding modules available with Python. I needed to filter html email messages, parse stylesheets, embed styles, and whole host of other things. I found just about every function I needed in a Python library somewhere. I was especially happy when I found out that some css sheets are gzipped that there was a module for that!

So if you are serious about then dig in. You will learn a LOT. 🙂

Answered By: Demolishun

Depends to what level you want to build the client. You can quickly whip something up with libraries like smtplib for handling conection/data. And tk for a GUI. But again it all depends on the level of finish your after.

A quick basic tool for yourself: Easy. (With libraries)
Writing a full-feutured email client: Hard.

Instead of using a library, you can also find an open source project you can contribute to. I’d recommend having a look at Mailpile

Answered By: Bruno

I have made two libraries that solve some of those problems easily:

Here is a short example of both:

from redbox import EmailBox
from redmail import EmailSender

USERNAME = "[email protected]"
PASSWORD = "<PASSWORD>"

box = EmailBox(
    host="imap.example.com", 
    port=993,
    username=USERNAME,
    password=PASSWORD
)

sender = EmailSender(
    host="smtp.example.com", 
    port=587,
    username=USERNAME,
    password=PASSWORD
)

Then you can send emails:

email.send(
    subject='email subject',
    sender="[email protected]",
    receivers=['[email protected]'],
    text="Hi, this is an email.",
    html="""
        <h1>Hi,</h1>
        <p>this is an email.</p>
    """,
    attachments={
        'data.csv': Path('path/to/file.csv'),
        'raw_file.html': '<h1>Just some HTML</h1>',
    }
)

Or read emails:

from redbox.query import UNSEEN, FROM

# Select an email folder
inbox = box["INBOX"]

# Search and process messages
for msg in inbox.search(UNSEEN & FROM('[email protected]')):

    # Process the message
    print(msg.headers)
    print(msg.from_)
    print(msg.to)
    print(msg.subject)
    print(msg.text_body)
    print(msg.html_body)

    # Set the message as read/seen
    msg.read()

Red Box fully supports logical operations using the query language if you need complex logical operations. You can also easily access various parts of the messages.

Links, Red Mail:

Links, Red Box:

Answered By: miksus