Skip to content

Instantly share code, notes, and snippets.

@bradwright
Created March 15, 2009 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradwright/79494 to your computer and use it in GitHub Desktop.
Save bradwright/79494 to your computer and use it in GitHub Desktop.
email_body = """From someone@gmail.com Sun Mar 15 01:04:01 2009
Return-Path: <someone@gmail.com>
X-Original-To: someone@somewhere.com
Delivered-To: someone@somewhere.com
Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=209.85.218.176; \
helo=mail-bw0-f176.google.com; envelope-f$
X-DKIM: Sendmail DKIM Filter v2.8.1 sth-ubuntu 289CF31836A
Authentication-Results: sth-ubuntu; dkim=pass (1024-bit key)
header.i=@gmail.com; dkim-adsp=none
Received: from mail-bw0-f176.google.com (mail-bw0-f176.google.com [209.85.218.176])
by sth-ubuntu (Postfix) with ESMTP id 289CF31836A
for <someone@somewhere.com>; Sun, 15 Mar 2009 01:04:01 +0000 (GMT)
Received: by bwz24 with SMTP id 24so577769bwz.36
for <someone@somewhere.com>; Sat, 14 Mar 2009 18:04:00 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=gamma;
h=domainkey-signature:mime-version:received:date:message-id:subject
:from:to:content-type;
bh=VT5A/8PKj6A/kR4H5fBL0q93EEdaTMm9WS0QOKrkqNY=;
b=ktI1CS4hEh7F31pTQqNVnU7GudFpjnJi8TsGpd2rdLRR5T4lCaYiDENJS2Tx/VFC0x
I6n4T7/VTFqujTAqm6ud+XywrkHELxNZmIv2fmBjo3BjxyxCuhDabQBtESUnDgvl9sfo
m+pp2WmkbfnwGVwIqOgyal6Wd7NavbXT7sdiw=
DomainKey-Signature: a=rsa-sha1; c=nofws;
d=gmail.com; s=gamma;
h=mime-version:date:message-id:subject:from:to:content-type;
b=fVMt5O2UxxunpZDC0AYPE6X1eDgwK0uFw8N7yh52L6rmbnNQDwe9nKQn1bMkLeTgpU
TM9eHm5/y1qWNheK1b9lCA6ok0cn5yQ8eJvSqSGzDJMUKtJ7Gsxg7cpgBXCVgxFB+Ki/
KXv2Cr7g8FokZFpLqgXwvZ15NQORTWzQutnTE=
MIME-Version: 1.0
Received: by 10.103.227.13 with SMTP id e13mr1376219mur.20.1237079040537; Sat,
14 Mar 2009 18:04:00 -0700 (PDT)
Date: Sun, 15 Mar 2009 01:04:00 +0000
Message-ID: <a33f028c0903141804y3a1e2caaqf6674561905db44e@mail.gmail.com>
Subject: Test
From: Me <someone@gmail.com>
To: metc@79.97.116.138
Content-Type: multipart/mixed; boundary=001636765a6ac5370f04651de87b
--001636765a6ac5370f04651de87b
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
--
me
--001636765a6ac5370f04651de87b
Content-Type: application/octet-stream;
name="filename.file"
Content-Disposition: attachment;
filename="filename.file"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_fsb0wyiy0
ZDg6YW5ub3VuY2U0MDpodHRwOi8vdHJhY2tlci50aGVwaXJhdGViYXkub3JnL2Fubm91bmNlMTM6
YW5ub3VuY2UtbGlzdGxsNDA6aHR0cDovL3RyYWNrZXIudGhlcGlyYXRlYmF5Lm9yZy9hbm5vdW5j
ZWVsNDI6dWRwOi8vdHJhY2tlci50aGVwaXJhdGViYXkub3JnOjgwL2Fubm91bmNlZWw0MjpodHRw
Oi8vZGVuaXMuc3RhbGtlci5oM3EuY29tOjY5NjkvYW5ub3VuY2VlbDQxOnVkcDovL2RlbmlzLnN0
YWxrZXIuaDNxLmNvbTo2OTY5L2Fubm91bmNlZWw0Mzp1ZHA6Ly90cmFja2VyMi50b3JyZW50Ym94
[snipped the rest]
--001636765a6ac5370f04651de87b--
"""
import email
import re
rx = re.compile(r'(?P<name>.*)<(?P<email>.*)>')
e = email.message_from_string(email_body)
matches = rx.match(e.get('from'))
from_addr = matches.group('email')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment