Bottom Page

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
Quote
#2
This is a common issue in Python 3. Your email_body variable is bytes, not a string. Bytes do not have an encoding, so if you want to use them as a string you have to take action to turn the bytes into a string with a given encoding. The way you'd do that here specifically is you'd call the decode method like so
m = email.message_from_string(email_body.decode())
Note that decode takes an encoding as a parameter, but it defaults to UTF-8, which is very likely fine for your purposes.

Generally speaking, when you run into the "must be str not bytes" kinda stuff, you want to call decode. For the opposite, you'd use encode.
Quote
#3
thanks, now I am facing new error now.

Error:
Traceback (most recent call last): File "eposDatadownload.py", line 28, in <module> if not os.path_exist(save_path): AttributeError: module 'os' has no attribute 'path_exist'
tried with os.path_exists also.this path is already exits, if I am commenting that,getting below error

Error:
Traceback (most recent call last): File "eposDatadownload.py", line 30, in <module> with open(os.path.join_path(save_path,filename), 'wb') as fp: AttributeError: module 'posixpath' has no attribute 'join_path'
whereas I am not getting error while checking indivisually

Python 3.6.0 (default, Jan 22 2018, 15:43:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> save_path = '/home/anna/scripts/ohfdata/downloads'
>>> print(os.path.exists(save_path))
True
Yoriz wrote May-16-2019, 07:44 AM:
If the original question is now resolved.
Please set this thread as solved and create a new thread with the new question.
Quote
#4
You're not calling the same function in those cases. In one, you're using os.path.exists and in the other you're using os.path_exist (which isn't a function). Similarly, it's os.path.join, not os.path.join_path.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pynput doesn't recognize shift button and special characters VirtualDreamer 0 111 Jul-17-2019, 11:55 AM
Last Post: VirtualDreamer
  Paste Special with multiple arguments with Dispatch CaptainCsaba 3 208 Jun-20-2019, 07:27 PM
Last Post: Larz60+
  smtplib mail without subject anna 2 201 Apr-24-2019, 05:44 AM
Last Post: anna
  problems with python script and special characters last08 1 287 Mar-29-2019, 09:28 AM
Last Post: Kebap
  How to get python to download YouTube videos in the background? Pythenx 11 750 Mar-25-2019, 04:57 AM
Last Post: samsonite
  Download multiple large json files at once halcynthis 0 255 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  Import Library but Download first? OscarBoots 15 1,345 Feb-07-2019, 03:07 AM
Last Post: snippsat
  Download data from webpage after POST request AlDe 0 235 Feb-02-2019, 06:26 AM
Last Post: AlDe
  run setup.exe from python script with special parameters asheru93 0 266 Feb-01-2019, 03:11 PM
Last Post: asheru93
  Mail sending Python - What it's wrong? airwawekz 1 302 Jan-30-2019, 03:20 PM
Last Post: gontajones

Forum Jump:


Users browsing this thread: 1 Guest(s)