From RFC 1036, 822:
In order to conform to RFC-822, the
Message-ID must have the format:
<unique@full_domain_name>
So the actual message ID would be between < and > The domain part is part of the ID.
I'd probably strip the string, then split on the < character, verify it ends with > and then cut that off.
I can't really work out a good solution with your data (is there a typo in it at the end?), but if it looks like the following I'd parse it something like this
# Note: my list does not end with , ")"]
messageparts = [('1 (BODY[HEADER.FIELDS (MESSAGE-ID)] {78}',
'Message-ID: <actualmessageid@mail.mail.gmail.com>\r\n\r\n')]
for envelope, data in messageparts:
# data: the part with Message-ID in it
# data.strip(): Newlines removed
# .split("<"): Break in 2 parts, left of < and right of <. Removes <
# .rstrip(">") remove > from the end of the line until there is
# no > there anymore;
# "x>>>".rstrip() -> "x"
print "The message ID is: ", data.strip().split("<")[1].rstrip(">")
# Short alternative version:
messageids = [data.strip().split("<")[1].rstrip(">") \
for env,data in messageparts]
print messageids
Output:
The message ID is: actualmessageid@mail.mail.gmail.com
['actualmessageid@mail.mail.gmail.com']
I splitted some lines using '\' to make it a bit more readable here, and the code assumes the headers are all valid.