Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active February 25, 2023 13:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariocesar/3056386 to your computer and use it in GitHub Desktop.
Save mariocesar/3056386 to your computer and use it in GitHub Desktop.
Parse a raw email
"""
$ cat source.email | python email_parse.py
"""
import fileinput
import email
from email.Utils import parseaddr
def parse():
raw_message = ''.join([line for line in fileinput.input()])
message = email.message_from_string(raw_message)
text_plain = None
text_html = None
for part in message.walk():
if part.get_content_type() == 'text/plain' and text_plain is None:
text_plain = part.get_payload()
if part.get_content_type() == 'text/html' and text_html is None:
text_html = part.get_payload()
return {
'to': parseaddr(message.get('To'))[1],
'from': parseaddr(message.get('From'))[1],
'delivered to': parseaddr(message.get('Delivered-To'))[1],
'subject': message.get('Subject'),
'text_plain': text_plain,
'text_html': text_html,
}
if __name__ == '__main__':
print parse()
MIME-Version: 1.0
Received: by 10.152.6.130 with HTTP; Wed, 4 Jul 2012 17:10:25 -0700 (PDT)
Date: Wed, 4 Jul 2012 20:10:25 -0400
Delivered-To: user1@gmail.com
Message-ID: <CAB2OXrM6oChuZdcWrRtf8btjadXtZfDxd3R+Pto6jLY6C3We3w@mail.gmail.com>
Subject: prueba
From: Favio <user1@gmail.com>
To: Favio <user1@gmail.com>
Content-Type: multipart/mixed; boundary=f46d043745a175980904c409f962
--f46d043745a175980904c409f962
Content-Type: multipart/alternative; boundary=f46d043745a175980204c409f960
--f46d043745a175980204c409f960
Content-Type: text/plain; charset=ISO-8859-1
probando
--f46d043745a175980204c409f960
Content-Type: text/html; charset=ISO-8859-1
probando
--f46d043745a175980204c409f960--
--f46d043745a175980904c409f962
Content-Type: text/plain; charset=US-ASCII; name="adj.txt"
Content-Disposition: attachment; filename="adj.txt"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_h492x3111
YWRqCg==
--f46d043745a175980904c409f962
Content-Type: text/html; charset=US-ASCII; name="adj.html"
Content-Disposition: attachment; filename="adj.html"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_h492wyx10
PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PjxwPmFkajwvcD48L2JvZHk+PC9odG1sPgo=
--f46d043745a175980904c409f962--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment