Python Forum
o365 special subject mail download issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
o365 special subject mail download issue
#1
Hi,

I am getting specific mail in Inbox with subject 'ACTIVE DSL DUMP EPOS' on daily basis, with extension .gz. I am trying to download attachment, however I am getting error. Below is the my script, post reading some online docs.


import imaplib
import email
import os

save_path = '/home/downloads'

mail = imaplib.IMAP4_SSL('outlook.office365.com')
mail.login("xyz.com", "Abc1@2015")
mail.select("Inbox")

typ, msgs = mail.search(None, '(SUBJECT "ACTIVE DSL DUMP EPOS")')
msgs = msgs[0].split()

for emailid in msgs:
    resp, data = mail.fetch(emailid, "(RFC822)")
    email_body = data[0][1]
    m = email.message_from_string(email_body)

    if m.get_content_maintype() != 'multipart':
        continue

    for part in m.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue
        filename = part.get_filename()
        if not os.path_exists(save_path):
            os.makedirs(save_path)
        with open(os.path.join_path(save_path,filename), 'wb') as fp:
             fp.write(part.get_payload(decode=True))
Error:
Traceback (most recent call last): File "eposDatadownload.py", line 17, in <module> m = email.message_from_string(email_body) File "/usr/local/lib/python3.6/email/__init__.py", line 38, in message_from_string return Parser(*args, **kws).parsestr(s) File "/usr/local/lib/python3.6/email/parser.py", line 68, in parsestr return self.parse(StringIO(text), headersonly=headersonly) TypeError: initial_value must be str or None, not bytes
Reply


Messages In This Thread
o365 special subject mail download issue - by anna - May-15-2019, 07:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  download with internet download manager coral_raha 0 2,942 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  Mail issue Mihil 3 2,652 Dec-03-2020, 05:25 AM
Last Post: Mihil
  Does O365 work with Office 2016 KipCarter 7 4,633 Jan-17-2020, 04:28 PM
Last Post: buran
  smtplib mail without subject anna 2 2,468 Apr-24-2019, 05:44 AM
Last Post: anna
  How can read and download the attachment from the mail using while loop in IMAPlib Py Samjith 0 4,276 Oct-11-2018, 07:15 AM
Last Post: Samjith
  Mail Faiyaz 1 25,756 Sep-24-2018, 02:29 PM
Last Post: Larz60+
  Outlook mail watcher g_shanmuga 1 5,053 Mar-26-2018, 04:09 PM
Last Post: nilamo
  Issue with special characters Tiskolin 2 3,084 Mar-19-2018, 11:21 AM
Last Post: snippsat
  Simple e-mail sender, that automatically changes subject for every e-mail joker 2 3,154 Aug-24-2017, 09:45 AM
Last Post: joker
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,432 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020