Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : ( msg = email.message_from_string(aaa) ) values are returning ( None ) when trying to parse stuff from raw e-mail source

let's execute the script

python b.wsgi

result is:

None
None

that is the problem and here is the full script b.wsgi

aaa = """
From [email protected] 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 <[email protected]>; 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: [email protected]
Subject: oooooooooooooooo
To: [email protected]
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

--bound1374805739--
"""

import email
msg = email.message_from_string(aaa)
print msg['From']
print msg['To']

i tried changing it to

print msg['from']
print msg['to']

same problem.

what might be the issue here ?

is it possible PYTHON knows this "raw" string was manually edited by my hands ?

very sneaky stuff going on here.


1 Answers

The \n at the beginning and end of the string are causing the problem. Try this

>>> msg = email.message_from_string(aaa.strip())
>>> msg.keys()
['Received', 'Received', 'From', 'Subject', 'To', 'Cc', 'X-Originating-IP', 'X-Mailer', 'Message-Id', 'Date', 'MIME-Version', 'Content-Type']
>>> msg['From']
'[email protected]'
like image 61
Sukrit Kalra Avatar answered Feb 15 '26 12:02

Sukrit Kalra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!