
Python-Bibliothek zum Parsen von E-Mail-Adressen und Mime
.. image:: https://travis-ci.org/mailgun/flanker.svg?branch=master :target: https://travis-ci.org/mailgun/flanker
.. image:: https://coveralls.io/repos/github/mailgun/flanker/badge.svg?branch=master :target: https://coveralls.io/github/mailgun/flanker?branch=master
Flanker ist eine Open-Source-Parsing-Bibliothek, die vom Mailgun-Team in Python geschrieben wurde.
Flanker besteht derzeit aus einer Adress-Parsing-Bibliothek (flanker.addresslib) sowie einer MIME-Parsing-Bibliothek (flanker.mime).
Eine ausführliche Dokumentation finden Sie im Benutzerhandbuch <https://github.com/mailgun/flanker/blob/master/docs/User%20Manual.md>_ sowie in der API-Referenz <https://github.com/mailgun/flanker/blob/master/docs/API%20Reference.md>_. Eine Schnellstart-Anleitung finden Sie unten.
Flanker wird von Mailgun <www.mailgun.com>_ in der Produktion intensiv mit Python 2.7 eingesetzt. Die aktuelle Produktionsversion ist v0.8.5.
Die Unterstützung für Python 3 wurde in v0.9.0 auf vielfachen Wunsch der Community hinzugefügt. Wir selbst verwenden Flanker hausintern nicht mit Python 3. Wir wissen nur, dass die Tests mit Python 3.6 bestehen, also verwenden Sie es auf eigenes Risiko. Melden Sie uns gerne Python-3-spezifische Probleme, falls Ihnen welche auffallen.
Sie können Flanker über pip installieren oder das Repository von GitHub klonen.
Sie benötigen Python-Header-Dateien, bevor Sie mit Flanker arbeiten können. Installieren Sie diese also zuerst:
.. code-block:: bash
sudo apt-get install python-dev
sudo yum install python-devel
Wenn Sie pip verwenden, geben Sie einfach Folgendes ein:
.. code-block:: bash
pip install flanker
Wenn Sie von GitHub klonen, können Sie Folgendes eingeben:
.. code-block:: bash
git clone [email protected]:mailgun/flanker.git cd flanker pip install -e .
Um eine einzelne Mailbox zu parsen (Anzeigename sowie E-Mail-Adresse):
.. code-block:: py
from flanker.addresslib import address
address.parse('Foo [email protected]') Foo [email protected]
Eine ungültige Adresse wird als None zurückgegeben:
.. code-block:: py
from flanker.addresslib import address
print address.parse('@example.com') None
Um eine einzelne E-Mail-Adresse zu parsen (ohne Anzeigename):
.. code-block:: py
from flanker.addresslib import address
address.parse('[email protected]', addr_spec_only=True) [email protected]
Um eine Adressliste zu parsen:
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com']) [[email protected], [email protected]]
Um eine Adressliste zu parsen und zusätzlich ein Tupel mit den geparsten Adressen und den nicht parsebaren Teilen zurückzugeben
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com'], as_tuple=True) [[email protected], [email protected]], ['@example.com']
Um eine Adressliste im strikten Modus zu parsen:
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com'], strict=True) [[email protected], [email protected]]
Um eine E-Mail-Adresse zu validieren (Parsen sowie DNS-, MX-Existenz- und ESP-Grammatikprüfungen):
.. code-block:: py
from flanker.addresslib import address
address.validate_address('[email protected]') [email protected]
Um eine Adressliste zu validieren:
.. code-block:: py
from flanker.addresslib import address
address.validate_list(['[email protected], [email protected], @mailgun.com'], as_tuple=True) ([[email protected], [email protected]], ['@mailgun.com'])
Für die folgenden Beispiele wird message_string auf die folgende MIME-Nachricht gesetzt:
::
MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=001a11c1d71697c7f004e6856996 From: Bob [email protected] To: Alice [email protected] Subject: hello, world Date: Mon, 16 Sep 2013 12:43:03 -0700
--001a11c1d71697c7f004e6856996 Content-Type: text/plain; charset=us-ascii
Hello, Alice
--001a11c1d71697c7f004e6856996 Content-Type: text/html; charset=us-ascii
Hello, Alice
--001a11c1d71697c7f004e6856996--
Um eine MIME-Nachricht zu parsen:
.. code-block:: py
from flanker import mime
msg = mime.from_string(message_string)
MIME-Nachrichtenkopfzeilen (Unicode-Mehrwert-Wörterbuch mit Kopfzeilen):
.. code-block:: py
from flanker import mime
msg = mime.from_string(message_string) msg.headers.items() [('Mime-Version', '1.0'), ('Content-Type', ('multipart/alternative', {'boundary': u'001a11c1d71697c7f004e6856996'})), ('From', 'Bob [email protected]'), ('To', 'Alice [email protected]'), ('Subject', 'hello, world'), ('Date', 'Mon, 16 Sep 2013 12:43:03 -0700')]
Nützliches content_type-Attribut mit Prädikaten:
.. code-block:: py
from flanker import mime msg = mime.from_string(message_string)
msg.content_type.is_multipart() True
msg.content_type.is_singlepart() False
msg.content_type.is_message_container() False
Dekodierter Textkörper (Body) einer Nachricht:
.. code-block:: py
from flanker import mime msg = mime.from_string(message_string)
None because message is multipart
print msg.body None
for part in msg.parts: print 'Content-Type: {} Body: {}'.format(part, part.body)
Content-Type: (text/plain) Body: Hello, Alice Content-Type: (text/html) Body:
Hello, Alice
None because no enclosed messages exist
print msg.enclosed None