You can also achieve what you want using the email
module HeaderParser.parsestr()
function (the same API as Parser, but not worried about the body of the message) and parseaddr()
function .
>>> from email.parser import HeaderParser
>>> from email.utils import parseaddr
>>> hp = HeaderParser()
>>> response = [('1 (BODY[HEADER.FIELDS (MESSAGE-ID)] {78}',
'Message-ID: <actualmessageid@mail.mail.gmail.com>\r\n\r\n'), ')']
>>> header_string = response[0][1]
>>> header_string
'Message-ID: <actualmessageid@mail.mail.gmail.com>\r\n\r\n'
>>> header = hp.parsestr(header_string)
>>> header
<email.message.Message instance at 0x023A6198>
>>> header['message-id']
'<actualmessageid@mail.mail.gmail.com>'
>>> msg_id = parseaddr(header['message-id'])
>>> msg_id
('', 'actualmessageid@mail.mail.gmail.com')
>>> msg_id[1]
'actualmessageid@mail.mail.gmail.com'
In this way:
from email.parser import HeaderParser
from email.utils import parseaddr
hp = HeaderParser()
def get_id(response):
header_string = response[0][1]
header = hp.parsestr(header_string)
return parseaddr(header['message-id'])[1]
response = [('1 (BODY[HEADER.FIELDS (MESSAGE-ID)] {78}',
'Message-ID: <actualmessageid@mail.mail.gmail.com>\r\n\r\n'), ')']
print(get_id(response))
returns:
actualmessageid@mail.mail.gmail.com
Acorn source
share