• Stars
    star
    1,611
  • Rank 27,826 (Top 0.6 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python email address and Mime parsing library

Flanker - email address and MIME parsing for Python

image

image

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

Detailed documentation is provided in the User Manual as well as the API Reference. A Quickstart Guide is provided below.

Python Versions

Flanker is heavily used by Mailgun in production with Python 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 tests pass with Python 3.6, so use at your own risk. Feel free to report Python 3 specific 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:

# ubuntu 
sudo apt-get install python-dev
# fedora 
sudo 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 [email protected]:mailgun/flanker.git
cd flanker
pip install -e .

Address Parsing

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

>>> from flanker.addresslib import address
>>>
>>> address.parse('Foo [email protected]')
Foo <foo@example.com>

An invalid address is returned as `None`:

>>> from flanker.addresslib import address
>>>
>>> print address.parse('@example.com')
None

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

>>> from flanker.addresslib import address
>>>
>>> address.parse('[email protected]', addr_spec_only=True)
foo@example.com

To parse an address list:

>>> from flanker.addresslib import address
>>>
>>> address.parse_list(['[email protected], [email protected], @example.com'])
[foo@example.com, bar@example.com]

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

>>> from flanker.addresslib import address
>>>
>>> address.parse_list(['[email protected], [email protected], @example.com'], as_tuple=True)
[foo@example.com, bar@example.com], ['@example.com']

To parse an address list in strict mode:

>>> from flanker.addresslib import address
>>>
>>> address.parse_list(['[email protected], [email protected], @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):

>>> from flanker.addresslib import address
>>>
>>> address.validate_address('[email protected]')
foo@mailgun.com

To validate an address list:

>>> from flanker.addresslib import address
>>>
>>> address.validate_list(['[email protected], [email protected], @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.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

<p>Hello, <b>Alice</b></p>

--001a11c1d71697c7f004e6856996--

To parse a MIME message:

>>> from flanker import mime
>>>
>>> msg = mime.from_string(message_string)

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

>>> 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')]

Useful content_type member with predicates:

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

Decoded body of a message:

>>> 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: <p>Hello, <b>Alice</b></p>

>>> # None because no enclosed messages exist
>>> print msg.enclosed
None

More Repositories

1

transactional-email-templates

Responsive transactional HTML email templates
HTML
6,820
star
2

godebug

DEPRECATED! https://github.com/derekparker/delve
Go
2,507
star
3

talon

Python
1,235
star
4

mailgun-php

Mailgun's Official SDK for PHP
PHP
1,078
star
5

gubernator

High Performance Rate Limiting MicroService and Library
Go
944
star
6

mailgun-js-boland

A simple Node.js helper module for Mailgun API.
JavaScript
896
star
7

kafka-pixy

gRPC/REST proxy for Kafka
Go
752
star
8

mailgun-go

Go library for sending mail with the Mailgun API.
Go
676
star
9

mailgun.js

Javascript SDK for Mailgun
TypeScript
502
star
10

mailgun-ruby

Mailgun's Official Ruby Library
Ruby
460
star
11

groupcache

Clone of golang/groupcache with TTL and Item Removal support
Go
424
star
12

expiringdict

Dictionary with auto-expiring values for caching purposes.
Python
331
star
13

holster

A place to keep useful golang functions and small libraries
Go
277
star
14

validator-demo

Mailgun email address jquery validation plugin http://mailgun.github.io/validator-demo/
JavaScript
259
star
15

node-prelaunch

A Mailgun powered landing page to capture early sign ups
JavaScript
230
star
16

dnsq

DNS Query Tool
Python
107
star
17

documentation

Mailgun Documentation
CSS
79
star
18

scroll

Scroll is a lightweight library for building Go HTTP services at Mailgun.
Go
61
star
19

kafka-http

Kafka http endpoint
Scala
51
star
20

forge

email dataset for email signature parsing
51
star
21

wordpress-plugin

Mailgun's Wordpress Plugin
PHP
47
star
22

lemma

Mailgun Cryptographic Tools
Go
39
star
23

multibuf

Bytes buffer that implements seeking and partially persisting to disk
Go
37
star
24

ttlmap

In memory dictionary with TTLs
Go
22
star
25

frontend-best-practices

Guides for React and Javascript coding style and best practices
21
star
26

pong

Generates http servers that respond in predefined manner
Go
20
star
27

proxyproto

High performance implementation of V1 and V2 Proxy Protocol
Go
19
star
28

log

Go logging library used at Mailgun.
Go
19
star
29

mailgun-meteor-demo

Simple meteor-based emailer with geolocation and UA tracking
JavaScript
16
star
30

timetools

Go library with various time utilities used at Mailgun.
Go
11
star
31

mailgun-java

Java SDK for integrating with Mailgun
Java
11
star
32

pelican-protocol

In ancient Egypt the pelican was believed to possess the ability to prophesy safe passage in the underworld. Pelicans are ferocious eaters of fish.
Go
11
star
33

metrics

Go library for emitting metrics to StatsD.
Go
11
star
34

roman

Obtain, cache, and automatically reload TLS certificates from an ACME server
Go
10
star
35

sandra

Go library providing some convenience wrappers around gocql.
Go
10
star
36

iptools

Go library providing utilities for working with hosts' IP addresses.
Go
9
star
37

cfg

Go library for working with app's configuration files used at Mailgun.
Go
9
star
38

minheap

Slightly more user-friendly heap on top of containers/heap
Go
8
star
39

logrus-hooks

Go
8
star
40

pebblezgo

go example of the pebblez transport: protocol buffers over zeromq
Go
7
star
41

glogutils

Utils for working with google logging library
Go
7
star
42

pylemma

Mailgun Cryptographic Tools
Python
5
star
43

callqueue

Serialized call queues
Go
4
star
44

media

Logos and brand guidelines
4
star
45

sneakercopy

A tool for creating encrypted tar archives for transferring sensitive data.
Rust
4
star
46

scripts

Example scripts that show how to interact with the Mailgun API
Python
1
star
47

etcd3-slim

Thin wrapper around Etcd3 gRPC stubs
Python
1
star