Extraire les composants d'un contenu textuel
Bonjour à vous,
Mon objectif est d'extraire les différents composants d'un email. Comme entré j'ai des contenus textuels qui correspondent à des emails et je veux identifier la personne qui a envoyé l'email, la personne qui l'a réçu, objet, corps et signature du mail.
Pour cela, j'ai fait ce petit code. Toute autre idée est le bienvenue.
Mon problème actuel est dans la définition de l'instance de classe que je comprends pas pourquoi ça bugue.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
import email
class Email_Components:
def Extract_From(raw_content):
sender = email.message_from_string(raw_content)['from']
receiver = email.message_from_string(raw_content)['to']
b = email.message_from_string(raw_content)
body = ""
if b.is_multipart():
for part in b.walk():
ctype = part.get_content_type()
cdispo = str(part.get('Content-Disposition'))
# skip any text/plain (txt) attachments
if ctype == 'text/plain' and 'attachment' not in cdispo:
body = part.get_payload(decode=True) # decode
break
# not multipart - i.e. plain text, no attachments, keeping fingers crossed
else:
body = b.get_payload(decode=True)
yield sender, receiver, body
def __init__(self, raw_content):
for sender, receiver, body in self.Extract_From(raw_content):
self.sender= sender
self.receiver= receiver
self.body= body
if __name__ == '__main__':
a = """From root@a1.local.tld Thu Jul 25 19:28:59 2013
Received: from a1.local.tld (localhost [127.0.0.1])
by a1.local.tld (8.14.4/8.14.4) with ESMTP id r6Q2SxeQ003866
for <ooo@a1.local.tld>; Thu, 25 Jul 2013 19:28:59 -0700
Received: (from root@localhost)
by a1.local.tld (8.14.4/8.14.4/Submit) id r6Q2Sxbh003865;
Thu, 25 Jul 2013 19:28:59 -0700
From: root@a1.local.tld
Subject: oooooooooooooooo
To: ooo@a1.local.tld
Cc:
X-Originating-IP: 192.168.15.127
X-Mailer: Webmin 1.420
Message-Id: <1374805739.3861@a1>
Date: Thu, 25 Jul 2013 19:28:59 -0700 (PDT)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="bound1374805739"
This is a multi-part message in MIME format.
--bound1374805739
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
ooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooo
--bound1374805739--"""
b=""""""
email_compo = Email_Components(b) |
Voilà l'erreur que j'ai
Code:
1 2
|
NameError: name 'Email_Components' is not defined |
Merci Beaucoup d'avance