1

I wrote a python code to download email attachments and this code works fine when I start it manually (just double click/open with python).

However, when I tried setting up a task in task scheduler, the job keeps running forever and no output is seen (no attachments in the output folder). What have I done wrong here?

I tried all combination of scheduler settings I could think of (run only when user is logged on/ run whether or not user is logged on; run with highest privileges)

I also tried defining actions in different ways:

-script: C:\Windows\System32\cmd.exe
-argument: "C:\ProgramData\Anaconda3\python.exe "C:\Users\LN\Documents\PythonScripts\outlookdownload.py""

-script: C:\ProgramData\Anaconda3\python.exe
-argument: "C:\Users\LN\Documents\PythonScripts\outlookdownload.py"

-script: C:\ProgramData\Anaconda3\python.exe
-argument: "C:\Users\LN\Documents\PythonScripts\outlookdownload.py"
-start in: C:\Users\LN\Documents\PythonScripts\

-script: C:\ProgramData\Anaconda3\python.exe
-argument: "C:\Users\LN\Documents\PythonScripts\outlookdownload.py"
-start in: C:\Users\LN\Documents\PythonScripts\"

Below is the python code:


    outpfolder= [output folder link]

    import datetime
    import win32com.client
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.Folders('Elo Inventory').Folders('Inbox')
    messages = inbox.Items
    message = messages.GetLast()
    sentdate = message.senton.date()
    datelimit = (datetime.date.today () - datetime.timedelta (days=7))

    #define function to save attachment:
    def OL_Attsave(outpath, name, filenamefilter):
        attachments = message.Attachments
        att_count = attachments.Count
        for i in range(1,att_count+1):
            attachment = attachments.Item(i)
            if filenamefilter in attachment.FileName and "image" not in attachment.FileName:
                    attachment.SaveASFile(outpath + str(sentdate)+"_"+name+"_" +str(attachment.Filename)) 
        return "saved " + str(att_count) + " files for " + name +" to " + outpath

    while sentdate>datelimit:
        try:

            #Company1:
            if message.SenderEmailAddress == "abc@company1.com" and message.subject == "INV ELO":
                outpath = outpfolder + "company1\\"
                name = 'company1'
                OL_Attsave(outpath, name ,'') 

            #Company2:
            elif "@company2.com" in message.SenderEmailAddress:
                outpath = outpfolder + "company2\\"
                name = 'company2'
                OL_Attsave(outpath, name, 'INV')                

            message = messages.GetPrevious()
            sentdate = message.senton.date()
        except:
            message = messages.GetPrevious()
            sentdate = message.senton.date()

  • Do you need: #!/usr/bin/env python . Do you need: chmod +x – mccurcio Feb 18 '20 at 17:39
  • I tried adding chmod +x before the link in argument and the task scheduler open command prompt, but then nothing else happened. I suppose #!/usr/bin/env python is not needed because I only have 1 version of python and I'm pointing directly to that python.exe file in anaconda folder. (I also tried installing Python outside of Anaconda but will delete one version before use). C:\ProgramData\Anaconda3\python.exe "C:\Users\LN\Documents\PythonScripts\outlookdownload.py" in cmd prompt or double click on the .py file worked but somehow couldn't set task scheduler up to run it properly. – Linh Nguyen Feb 18 '20 at 21:07
  • You always need '#!/usr/bin/env python' bc it tells the shell where to look for the interpreter. – mccurcio Feb 18 '20 at 23:31
  • What office & OS are you using? – 0m3r Feb 19 '20 at 00:37
  • I'm using Microsoft Office 365 32 bit. My laptop system type is x64-based PC and system directory is C:\WINDOWS\system32 – Linh Nguyen Feb 20 '20 at 15:15
  • I have also used #!/usr/bin/env python in the script but it didn't change anything – Linh Nguyen Feb 20 '20 at 15:18

0 Answers0