
.. 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は、MailgunチームによってPythonで書かれたオープンソースのパースライブラリです。
Flankerは現在、アドレス解析ライブラリ(flanker.addresslib)とMIME解析ライブラリ(flanker.mime)で構成されています。
詳細なドキュメントは ユーザーマニュアル <https://github.com/mailgun/flanker/blob/master/docs/User%20Manual.md>_ および APIリファレンス <https://github.com/mailgun/flanker/blob/master/docs/API%20Reference.md>_ に記載されています。クイックスタートガイドは以下に示します。
Flankerは、Python 2.7を使用して本番環境で Mailgun <www.mailgun.com>_ によって広く利用されています。現在の本番バージョンはv0.8.5です。
Python 3のサポートは、コミュニティからの要望に応えてv0.9.0で追加されました。 私たちは社内ではPython 3でFlankerを使用していません。私たちが知っているのは、テストがPython 3.6で通るということだけなので、自己責任で使用してください。Python 3固有の問題を見つけた場合は、遠慮なく報告してください。
flankerはpipでインストールするか、GitHubからリポジトリをクローンできます。
flankerを使い始める前にPythonヘッダーファイルが必要です。最初にインストールしてください:
.. code-block:: bash
sudo apt-get install python-dev
sudo yum install python-devel
pipを使用する場合は、次のように入力します:
.. code-block:: bash
pip install flanker
GitHubからクローンする場合は、次のように入力できます:
.. code-block:: bash
git clone [email protected]:mailgun/flanker.git cd flanker pip install -e .
単一のメールボックス(表示名と電子メールアドレスの両方)をパースするには:
.. code-block:: py
from flanker.addresslib import address
address.parse('Foo [email protected]') Foo [email protected]
無効なアドレスはNoneとして返されます:
.. code-block:: py
from flanker.addresslib import address
print address.parse('@example.com') None
単一の電子メールアドレス(表示名なし)をパースするには:
.. code-block:: py
from flanker.addresslib import address
address.parse('[email protected]', addr_spec_only=True) [email protected]
アドレスリストをパースするには:
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com']) [[email protected], [email protected]]
アドレスリストをパースし、パース済みのアドレスとパースできない部分を含むタプルを返すには:
.. 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']
厳密モードでアドレスリストをパースするには:
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com'], strict=True) [[email protected], [email protected]]
電子メールアドレスを検証するには(パースに加えて、DNS、MXの存在、ESPの文法チェック):
.. code-block:: py
from flanker.addresslib import address
address.validate_address('[email protected]') [email protected]
アドレスリストを検証するには:
.. 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'])
以下の例では、message_string は次のMIMEメッセージに設定されます:
::
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--
MIMEメッセージをパースするには:
.. code-block:: py
from flanker import mime
msg = mime.from_string(message_string)
MIMEメッセージのヘッダー(ヘッダーを持つunicodeマルチバリュー辞書):
.. 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')]
述語付きの便利なcontent_typeメンバー:
.. 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
メッセージのデコード済みボディ:
.. 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