python 电子邮件_Python的电子邮件过滤器第1部分

python 电子邮件

Local mail servers like Postfix and Sendmail as well as various mail processing solutions like Procmail and Formail utilize external mail filters by way of piping (stdin) an entire mail envelope to a program, allowing it to process, and then reading the new filtered envelope that streams out (stdout).

诸如Postfix和Sendmail之类的本地邮件服务器以及诸如Procmail和Formail之类的各种邮件处理解决方案通过将整个邮件信封管道化(stdin)到程序,允许其处理,然后读取新的过滤后的信封来利用外部邮件过滤器。流出(stdout)。

In a less restricted environment many *NIX users can utilize a user home directory dotfile named ~/.forward to define a program, mailbox, or alternate email address for a local address.

在较少限制的环境中,许多* NIX用户可以使用名为~/.forward的用户主目录点文件来为本地地址定义程序,邮箱或备用电子邮件地址。

With Postfix and Sendmail you can typically use plussed + email addresses like user+logs@example.org and create ~/.forward+logs in the users home directory to specifically handle forwarding and handling of email related to the logs extension of the email address.

使用Postfix和Sendmail,您通常可以使用加号+电子邮件地址,例如user+logs@example.org并在用户主目录中创建~/.forward+logs ,以专门处理转发和处理与电子邮件地址的日志扩展名相关的电子邮件。

If ~/.forward* is present and matches the envelope of the message then mail received by Postfix or Sendmail is typically processed filtered through the program, sent to the mailbox, or forwarded to the email address present in that file.

如果存在~/.forward*并与邮件的信封匹配,则通常通过程序过滤,处理Postfix或Sendmail接收的邮件,将其发送到邮箱或转发到该文件中存在的电子邮件地址。

Absent any ~/.forward* — if the user home directory dotfile named ~/.procmailrc is present then Procmail or Sendmail will typically spin up a Procmail process that reads the mail headers and steps the mail through a series of rules present in that configuration file.

不存在任何~/.forward* -如果存在名为~/.procmailrc的用户主目录点文件,则Procmail或Sendmail通常会启动Procmail进程,该进程读取邮件头并通过该配置中存在的一系列规则逐步处理邮件文件。

With Procmail you have a lot of options and continuations to mail rule and filter processing that could include storing a copy of a message, processing it through a filter, storing a copy of the processed message, ultimately sending the message through the /sbin/sendmail process toward another user or mailbox (like a filter would) and then archiving any mail that didn’t match a rule. Procmail is a powerful mail processing solution that is typically configured per user as required and is capable of using scripts written in Python as an RFC 5322 streaming message filter.

使用Procmail,您可以对邮件规则和过滤器处理有很多选择和延续,包括存储消息的副本,通过过滤器处理消息,存储已处理的消息的副本,最终通过/sbin/sendmail发送消息处理另一个用户或邮箱(例如过滤器),然后将所有不符合规则的邮件存档。 Procmail是一种功能强大的邮件处理解决方案,通常根据需要按用户配置,并且能够使用以Python编写的脚本作为RFC 5322流消息过滤器。

You thought you were here to learn about how Python deals with all this? I suppose we should get down to it.

您以为是在这里学习Python如何处理所有这些问题? 我想我们应该付诸实践。

Key things to keep in mind:

要记住的关键事项:

  • The stream will need to be read to memory and therefore message size should really be.. well.. less than the total available memory.

    流将需要读取到内存中,因此消息大小实际上应该小于总可用内存。
  • Delivering 1 message can result in 1 process being spun up by the OS for Procmail and then N extra processes being spun up for filter processes like Python. Size accordingly or use Python as socket based milter/lmtp server (not covered here).

    传递1条消息可能会导致OS为Procmail旋转1个进程,然后为诸如Python之类的筛选器进程旋转N个额外的进程。 相应地调整大小,或将Python用作基于套接字的milter / lmtp服务器(此处未涵盖)。

  • The Python object representing the email message will be in memory as well. Make sure you aren’t holding on to any big strings in memory.

    表示电子邮件的Python对象也将在内存中。 确保您没有占用内存中的任何大字符串。
  • Codecs are used when reading email using the standard Python email module and we will default to using some safe UTF capable standards in this article.

    使用标准的Python电子邮件模块阅读电子邮件时,会使用编解码器,并且在本文中,我们将默认使用一些安全且支持UTF的标准。

In this article we will be focused on using Procmail to match a specific extension named pythonme(username+pythonme@localhost) and continue regular delivery of all other unmatched messages. Google has the goods on how to configure Procmail but here is the initial .procmailrc we will be using:

在本文中,我们将重点介绍如何使用Procmail匹配名为pythonme (用户名+ pythonme @localhost)的特定扩展名,并继续常规传送所有其他不匹配的消息。 Google提供了有关如何配置Procmail的商品,但这是我们将使用的初始.procmailrc

And here’s a quick test session where we have an empty ~/Maildir directory and use Postfix and Procmail to deliver a message using the pythonme extension to the local user:

这是一个快速测试会话,其中我们有一个空的~/Maildir目录,并使用Postfix和Procmail使用pythonme扩展名向本地用户传递消息:

That did what we expect. The filtered message is just the output of python --version and the original message was properly backed up to the ~/Maildir/00-pythonme folder.

做到了我们所期望的。 过滤的消息只是python --version的输出,原始消息已正确备份到~/Maildir/00-pythonme文件夹。

Sending a message to just spencersr@localhost.localdomain will skip processing and store the message in ~/Maildir/CATCHALL.

仅向spencersr@localhost.localdomain发送消息将跳过处理并将消息存储在~/Maildir/CATCHALL

Ok. Now lets set up the base of a very simple script that parses the email using the Python standard email library and then generates an output of that message after modifying the subject line.

好。 现在,让我们建立一个非常简单的脚本的基础,该脚本使用Python标准的电子邮件库解析电子邮件,然后在修改主题行之后生成该消息的输出。

This is fairly straight forward. Create a generator that knows to write to sys.stdout.buffer using a UTF8 email policy available with Python and then read the mail message as bytes from sys.stdin.buffer and replace the Subject header using pointed functions rather than assignments. Assignments can get a little iffy when dealing with headers where multiple headers are allowed to exist and it’s best to write your own functions to assure only one header is available when you expect that to be the case. In my case I often subclass email.message.EmailMessage and add a supplant_header function to deal with this.

这是相当简单的。 创建一个知道可使用Python可用的UTF8电子邮件策略写入sys.stdout.buffer的生成器,然后从sys.stdin.buffer以字节为单位读取邮件消息,并使用指向函数(而不是赋值)替换Subject标头。 在处理允许存在多个标头的标头时,分配可能会有些麻烦,最好编写自己的函数,以确保在可能的情况下只有一个标头可用。 以我email.message.EmailMessage ,我经常将email.message.EmailMessage子类化,并添加supplant_header函数来处理此问题。

Modify ~/.procmailrc to use the pythonme.py script.

修改~/.procmailrc以使用pythonme.py脚本。

Updated to use pythonme.py
更新为使用pythonme.py

And shall we do a test run?

我们是否应该进行试运行?

Well that seemed to work just dandy.

好吧,这似乎只是花花公子。

In my next article I will be reviewing traversing the EmailMessage object to find attachments and subsequently extract the contents to make use of them.

在我的下一篇文章中,我将审阅遍历EmailMessage对象以查找附件,然后提取内容以使用它们。

As it stands for now this is a good starting point for anybody looking to modify headers. Using Procmail you can now also add a line that forces delivery to modified To: headers as well if any rewriting is needed.

就目前而言,这是任何希望修改标头的人的良好起点。 现在,使用Procmail,您还可以添加一行,如果需要任何重写,也可以强制将其传递到修改后的To:标头。

翻译自: https://medium.com/python-in-plain-english/python-an-email-filter-part-1-fd9d4c3c87d8

python 电子邮件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值