StnBnu
StnBnu

Reputation: 75

Extract first line of email body using python

I can open emails using python (imap and mail modules), mostly following the advice here: How can I get an email message's text content using python?

But I need to print only the first line of every email body - how could i do that?

    for part in email_message.walk():
    # each part is a either non-multipart, or another multipart message
    # that contains further parts... Message is organized like a tree
    if part.get_content_type() == 'text/plain':
        print part.get_payload() # prints the raw text

this is what i currently have to print the body, any ideas how I could restrict that to the first line of the email?

Upvotes: 2

Views: 2841

Answers (2)

Todor Minakov
Todor Minakov

Reputation: 20077

There's a method in the string library exactly for this operation - splitlines(), which takes care of the different line endings (\n, or \r\n). From the doc:

For example, 'ab c\n\nde fg\rkl\r\n'.splitlines() returns ['ab c', '', 'de fg', 'kl'],

As it returns an array, getting the first element is trivial - [0]. It also doesn't return an extra empty string if the last element ends with a newline character, unlike split('n').

Plus, you'd better use get_payload(decode=True), it'll take care of base64 etc decoding for you. Finally, here's your example updated:

for part in email_message.walk():
# each part is a either non-multipart, or another multipart message
# that contains further parts... Message is organized like a tree
if part.get_content_type() == 'text/plain':
    # you may want to break it out in 2 statements for readability
    print part.get_payload(decode=True).splitlines()[0] # prints the first line

BTW, text attachments are also of 'text/plain' content type and may mess up your expected data; you might want to skip those - see my post here (self quoting, xaxax).

HTH

Upvotes: 3

Marco
Marco

Reputation: 2020

According to docs get_payload() should return a string so this should work.

for part in email_message.walk():
    # each part is a either non-multipart, or another multipart message
    # that contains further parts... Message is organized like a tree
    if part.get_content_type() == 'text/plain':
        lines=part.get_payload().split("\n")
        print lines[0]

Upvotes: 1

Related Questions