构建一个电子邮件客户端有多难? - Python

发布于 2024-11-01 08:05:00 字数 209 浏览 6 评论 0原文

我在这里冒险……

我试图弄清楚使用Python实现电子邮件客户端有多难:

  • 电子邮件检索
  • 电子邮件发送
  • 电子邮件格式化
  • 电子邮件渲染

另外我想知道是否所有协议都是简单/困难的支持例如 SMTP、IMAP、POP3...


希望有人能指出我正确的方向:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城°AllureLove 2024-11-08 08:05:00

Python 语言确实为其标准库中所需的协议提供了原始支持。然而,正确使用、正确解析和组装“现代”电子邮件可能很难做到。

另外,你没有说是否要为你的电子邮件客户端创建一个图形界面——如果你想要一个合适的图形界面——直到可用为止,这是相当多的工作。

本地电子邮件存储将是更容易的部分 - 除非您想正确实现 mbox 文件格式 RFC -4155 以便其他软件可以轻松读取/写入您获取的消息,您可以使用 ORM 或面向对象的数据库(例如 ZODB 或 MongoDB)将它们存储为 Python 对象。

如果您想要的不仅仅是一个玩具电子邮件应用程序,那么您将需要做很多工作 - 正确编码电子邮件标头,例如,服务器身份验证和安全身份验证和传输层、对电子邮件文本正文本身进行解码以实现非ASCII 消息。尽管 Python 标准库上的模块确实实现了很多功能,但它们的文档缺乏示例 - 完整的电子邮件客户端必须使用所有这些。

当然,启动电子邮件客户端(即使是玩具客户端)的地方应该是查看最新的电子邮件 RFC(并且您必须从这里进行选择 http://www.ietf.org/rfc/rfc-index 因为在谷歌上查找“email rfc”给出的结果很差结果)。

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

那请放手 2024-11-08 08:05:00

我认为您会发现许多客户端重要部分都已预先打包:

电子邮件检索 - 我认为许多 Python 库都涵盖了这一点。

电子邮件发送 - 这并不难,而且很可能也已涵盖。

电子邮件格式 - 我知道这已被涵盖,因为我只是用它来为客户解析单个和多部分电子邮件。

电子邮件渲染 - 我会尝试某种 HTML 渲染器。 Mozilla 项目有一个渲染器的 Python 接口。我猜想还有其他渲染引擎也有 python 接口。我知道 wxWidgets 有一些简单的 HTML 功能并且重量会轻很多。想想看,Mozilla 引擎可能还有许多您需要的其他功能。您必须研究每个部分。

它的内容比上面列出的要多得多。就像任何有价值的东西都不是一天建成的一样。我会准确地列出你想要它做什么。然后开始组装原型。只需构建一个执行基本操作的简单框架即可。比如只支持消息的文本部分,不支持 html。然后以此为基础。

我对 Python 提供的丰富编码模块感到惊讶。我需要过滤 html 电子邮件、解析样式表、嵌入样式以及其他所有内容。我在某个 Python 库中找到了几乎所有我需要的函数。当我发现一些 css 表被压缩并且有一个模块时,我特别高兴!

因此,如果您认真对待,那么就深入研究。您会学到很多东西。 :)

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

绝影如岚 2024-11-08 08:05:00

我制作了两个库可以轻松解决其中一些问题:

这是两者的简短示例:

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
)

然后您可以发送电子邮件:

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>',
    }
)

或者阅读电子邮件:

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 完全支持使用 如果您需要复杂的逻辑运算,请使用查询语言。您还可以轻松访问消息的各个部分

链接,红色邮件:

链接,红框:

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:

揽清风入怀 2024-11-08 08:05:00

如果我是你,我会检查现有电子邮件客户端的源代码来了解一下:thunderbird、sylpheed-claws、mutt...

根据你想要支持的功能集,这是一个大项目。

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.

请爱~陌生人 2024-11-08 08:05:00

取决于你想建立什么级别的客户端。您可以使用 smtplib 等库快速创建一些内容来处理连接/数据。以及 GUI 的 tk 。但这一切都取决于你的完成程度。

适合自己的快速基本工具:简单。 (与图书馆)
编写一个功能齐全的电子邮件客户端:很难。

您还可以找到可以贡献的开源项目,而不是使用库。我建议您查看 Mailpile

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文