How hard is it to build an Email client? - Python
Asked Answered
A

5

27

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 :)

Agoraphobia answered 13/4, 2011 at 10:8 Comment(4)
I removed the C++ tag, it doesn't seem C++ related at allExeunt
Python: Batteries included: smtp, imap, pop3 - Internet Protocols, Internet Data HandlingDeform
Do you want to build something with a UI, or not? If so, and it's the UI that is the thing you want to play with, just build a frontend to an existing email client. Decide what you want to learn the details of: headers, protocols and bytes, or buttons and notifications?Vyky
It's hard to find the proper code to sanitize the email body. The few examples of code I've found for sanitizing the email body break a lot of emails. Displaying emails properly will be the hardest part.Hola
O
32

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).

Office answered 13/4, 2011 at 12:6 Comment(0)
C
8

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. :)

Crockery answered 29/7, 2011 at 3:23 Comment(0)
U
6

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:

Unfurl answered 1/10, 2022 at 17:44 Comment(0)
H
2

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.

Heres answered 13/4, 2011 at 10:15 Comment(2)
Not shure: the source of any in productiona and maintained e-mail client such as thunderbird will be overwhelmingly huge for one to be able to grasp on how to proceed, or learn anything from there. He'd neeed to get an "example" e-mail client...which is ecatly what he is intending to write.Office
I agree. This may be multiplied since Thunderbird is probably done in C++, and if they didn't document it, the code can seem something completely different than many C++-like languages. This gets even worse if they used a lot practices you see that are geared for C programsTopical
H
1

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

Hardpan answered 11/12, 2016 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.