0

i have a trouble with a script to get the subject from my mailbox using imaplib.

Here, is my script:

    for mail in response[0].split():
    typ, response = imap.fetch(mail, '(RFC822)')
    for response_part in response:
        if isinstance(response_part, tuple):
            msg = email.message_from_string(response_part[1])
            for header in [ 'subject']:
                print msg["subject"]

But i get this Output:

[astreinte] [2015.06.09 07:36:04] blurp : TIMESTAMP ALERT
 Disaster is PROBLEM

My problem is the EndOfLine after "TIMESTAMP ALERT", if i get msg.items() i can view that:

('Subject', '[astreinte] [2015.06.09 07:36:04] ankdo2ga22 : DOFUS TIMESTAMP ALERT\r\n Disaster is PROBLEM')

So, i don't understand why the \r\n is present ? Is it present in the metadata. But not added when zabbix send the message. I use office365.

Thanks a lot for help.

  • Do you mean "Although I have a message with a line ending in the subject the line ending doesn't appear when the message is sent"? Not sure what zabbix and Office 365 have to do with this as written – holdenweb Jun 09 '15 at 10:42
  • I am pretty sure it had to have been sent like that. It may have been an accident, and even not noticed when sent. Your python is correct. – JosiahDaniels Jun 10 '15 at 18:31
  • This looks like Header Folding. I would've thought the email parsing library would handle this for you. I don't have a solution off the top of my head for you. – Max Nov 09 '16 at 16:25

1 Answers1

0

Shameless plug: Try using my library: https://pypi.python.org/pypi/O365

If you are using Office 365 it should make your life much more simple.

In answering you question of why is the \r\n present, it is because Office365 is based on windows. Windows uses \r\n for it's line breaks for legacy reasons.

Narcolapser
  • 5,895
  • 15
  • 46
  • 56
  • Umm, IMAP, SMTP, and Email Format are all mandated to use \r\n line endings. This has nothing to do with windows. – Max Nov 09 '16 at 16:24