Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python email address and Mime parsing library

License

NotificationsYou must be signed in to change notification settings

mailgun/flanker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

https://travis-ci.org/mailgun/flanker.svg?branch=masterhttps://coveralls.io/repos/github/mailgun/flanker/badge.svg?branch=master

Flanker is an open source parsing library written in Python by the Mailgun Team.Flanker currently consists of an address parsing library (flanker.addresslib) aswell as a MIME parsing library (flanker.mime).

Detailed documentation is provided in theUser Manual as well as theAPI Reference. A Quickstart Guide is provided below.

Python Versions

Flanker is heavily used byMailgun in production withPython 2.7. The current production version is v0.8.5.

Support for Python 3 was added in v0.9.0 by popular demand from the community.We are not using Flanker with Python 3 in the house. All we know is that testspass with Python 3.6, so use at your own risk. Feel free to report Python 3specific issues if you see any.

Installing

You can install flanker via pip or clone the repo from GitHub.

You'll need Python headers files before you start working with flanker, so install them first:

# ubuntusudo apt-get install python-dev# fedorasudo yum install python-devel

If you are using pip, simply type:

pip install flanker

If you are cloning from GitHub, you can type:

git clone git@github.com:mailgun/flanker.gitcd flankerpip install -e.

Address Parsing

To parse a single mailbox (display name as well as email address):

>>>fromflanker.addresslibimportaddress>>>>>>address.parse('Foo foo@example.com')Foo<foo@example.com>

An invalid address is returned as None:

>>>fromflanker.addresslibimportaddress>>>>>>printaddress.parse('@example.com')None

To parse a single email address (no display name):

>>>fromflanker.addresslibimportaddress>>>>>>address.parse('foo@example.com',addr_spec_only=True)foo@example.com

To parse an address list:

>>>fromflanker.addresslibimportaddress>>>>>>address.parse_list(['foo@example.com, bar@example.com, @example.com'])[foo@example.com,bar@example.com]

To parse an address list as well as return a tuple containing the parsedaddresses and the unparsable portions

>>>fromflanker.addresslibimportaddress>>>>>>address.parse_list(['foo@example.com, bar@example.com, @example.com'],as_tuple=True)[foo@example.com,bar@example.com], ['@example.com']

To parse an address list in strict mode:

>>>fromflanker.addresslibimportaddress>>>>>>address.parse_list(['foo@example.com, bar@example.com, @example.com'],strict=True)[foo@example.com,bar@example.com]

To validate an email address (parse as well as DNS, MX existence, and ESP grammar checks):

>>>fromflanker.addresslibimportaddress>>>>>>address.validate_address('foo@mailgun.com')foo@mailgun.com

To validate an address list:

>>>fromflanker.addresslibimportaddress>>>>>>address.validate_list(['foo@mailgun.com, bar@mailgun.com, @mailgun.com'],as_tuple=True)([foo@mailgun.com,bar@mailgun.com], ['@mailgun.com'])

MIME Parsing

For the following examples, message_string will be set to the following MIME message:

MIME-Version: 1.0Content-Type: multipart/alternative; boundary=001a11c1d71697c7f004e6856996From: Bob <bob@example.com>To: Alice <alice@example.com>Subject: hello, worldDate: Mon, 16 Sep 2013 12:43:03 -0700--001a11c1d71697c7f004e6856996Content-Type: text/plain; charset=us-asciiHello, *Alice*--001a11c1d71697c7f004e6856996Content-Type: text/html; charset=us-ascii<p>Hello, <b>Alice</b></p>--001a11c1d71697c7f004e6856996--

To parse a MIME message:

>>>fromflankerimportmime>>>>>>msg=mime.from_string(message_string)

MIME message headers (unicode multi-value dictionary with headers):

>>>fromflankerimportmime>>>>>>msg=mime.from_string(message_string)>>>msg.headers.items()[('Mime-Version','1.0'), ('Content-Type',  ('multipart/alternative', {'boundary':u'001a11c1d71697c7f004e6856996'})), ('From','Bob <bob@example.com>'), ('To','Alice <alice@example.com>'), ('Subject','hello, world'), ('Date','Mon, 16 Sep 2013 12:43:03 -0700')]

Useful content_type member with predicates:

>>>fromflankerimportmime>>>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

Decoded body of a message:

>>>fromflankerimportmime>>>msg=mime.from_string(message_string)>>>>>># None because message is multipart>>>printmsg.bodyNone>>>>>>forpartinmsg.parts:print'Content-Type: {} Body: {}'.format(part,part.body)Content-Type: (text/plain)Body:Hello,*Alice*Content-Type: (text/html)Body:<p>Hello,<b>Alice</b></p>>>># None because no enclosed messages exist>>>printmsg.enclosedNone

About

Python email address and Mime parsing library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors46

Languages


[8]ページ先頭

©2009-2025 Movatter.jp