attachment-downloader
attachment-downloader copied to clipboard
randomly fails with some emails
I have a problem with the script, it is failing randomly with some attachments and I don't know why, I can't find any logical reason, at first I thought it was failing because of strange characters that were not standard ASCII, but now I see that some attachments with spaces, accents, etc... download them and others don't. These are the failures when I run the script, (I have modified some data for security) I have the last versions python in a Windows 2012 server
C:\Users\Administrador.empresa\AppData\Local\Programs\Python\Python39\Scripts>py .\attachment-downloader --host 192.168.58.3 --username facturas --password password --imap-folder Inbox --output d:\Factu2 --unsecure --date-after="2021-11-30T14:00:00" --delete --delete-copy-folder Inbox/Procesado 2021-11-30 18:14:21,735 - INFO - Logging in to: '192.168.58.3' as 'facturas' 2021-11-30 18:14:22,609 - INFO - Connected to IMAP Server with user facturas on 192.168.58.3 2021-11-30 18:14:22,609 - INFO - Listing messages matching the following criteri a: folder=Inbox, date__gt=2021-11-30 2021-11-30 18:14:22,609 - INFO - Fetch list of messages from folder 'Inbox' 2021-11-30 18:14:22,640 - INFO - Downloaded and parsed mail 'iberdon INFORMS' with 0 attachments 2021-11-30 18:14:22,640 - WARNING - Skipping message 'b'29656'' subject 'IBERDRO LA INFORMS' because it is before 2021-11-30 14:00:00+00:00 2021-11-30 18:14:22,656 - INFO - Downloaded and parsed mail 'ASUNTO: EXPEDIENTE PENDIENTE DE PAGO' with 0 attachments 2021-11-30 18:14:22,656 - WARNING - Skipping message 'b'29657'' subject 'ASUNTO: EXPEDIENTE PENDIENTE DE PAGO' because it is before 2021-11-30 14:00:00+00:00 2021-11-30 18:14:22,718 - INFO - Downloaded and parsed mail 'Facturas Noviembre 2021' with 2 attachments 2021-11-30 18:14:22,718 - WARNING - Skipping message 'b'29661'' subject 'Factura s Noviembre 2021' because it is before 2021-11-30 14:00:00+00:00 2021-11-30 18:14:22,734 - INFO - Downloaded and parsed mail 'ASUNTO: EXPEDIENTE PENDIENTE DE PAGO' with 0 attachments 2021-11-30 18:14:22,734 - INFO - Processing message 'b'29663'' subject 'ASUNTO: EXPEDIENTE PENDIENTE DE PAGO' 2021-11-30 18:14:22,765 - INFO - Downloaded and parsed mail 'Factura Servicios A AAA zacilities, S.L.U.' with 1 attachments 2021-11-30 18:14:22,765 - INFO - Processing message 'b'29664'' subject 'Factura Servicios AAA zacilities, S.L.U.' 2021-11-30 18:14:22,765 - INFO - Downloading attachment '' to path d:\Factu2\ 2021-11-30 18:14:22,765 - ERROR - [Errno 2] No such file or directory: 'd:\\Fact u2\\' Traceback (most recent call last): File "C:\Users\Administrador.empresa\AppData\Local\Programs\Python\Python39\S cripts\attachment-downloader", line 182, in <module> with open(download_path, "wb") as fp: FileNotFoundError: [Errno 2] No such file or directory: 'd:\\Factu2\\' 2021-11-30 18:14:22,765 - ERROR - Error saving file. Continuing... 2021-11-30 18:14:22,796 - INFO - Downloaded and parsed mail 'Factura Servicios A AAA zacilities, S.L.U.' with 1 attachments 2021-11-30 18:14:22,796 - INFO - Processing message 'b'29665'' subject 'Factura Servicios AAA zacilities, S.L.U.' 2021-11-30 18:14:22,796 - INFO - Downloading attachment '' to path d:\Factu2\ 2021-11-30 18:14:22,796 - ERROR - [Errno 2] No such file or directory: 'd:\\Fact u2\\' Traceback (most recent call last): File "C:\Users\Administrador.empresa\AppData\Local\Programs\Python\Python39\S cripts\attachment-downloader", line 182, in <module> with open(download_path, "wb") as fp: FileNotFoundError: [Errno 2] No such file or directory: 'd:\\Factu2\\' 2021-11-30 18:14:22,812 - ERROR - Error saving file. Continuing... 2021-11-30 18:14:22,812 - INFO - Downloaded and parsed mail 'ASUNTO: EXPEDIENTE PENDIENTE DE PAGO' with 0 attachments 2021-11-30 18:14:22,812 - INFO - Processing message 'b'29666'' subject 'ASUNTO: EXPEDIENTE PENDIENTE DE PAGO' 2021-11-30 18:14:22,827 - INFO - Finished processing messages 2021-11-30 18:14:22,827 - INFO - Logging out of: 192.168.58.3 2021-11-30 18:14:22,827 - INFO - Disconnected from IMAP Server [email protected]. 58.3 2021-11-30 18:14:22,827 - INFO - Done
Sorry, I don't know if more data is needed, if it has not been possible to look at it or if the project is simply abandoned, I would like to ask for some kind of answer please.
The error in the logs suggests the download folder doesn't exist, or there are permission issues that mean that the script can't write to it
I did note you use the argument --output d:\Factu2 but the logs show d:\\Fact u2\\. Not sure where the additional space is coming from?
I wrote this script years ago to solve a problem I had at the time. I no longer have an ongoing need for this, so I can't justify spending my time on issues that aren't easy to reporduce. I do however welcome pull requests from anyone who wants to submit features or bug fixes
First of all thank you and thank you, thank you for answering and thank you for this script and merry christmas.
From James to James, the d:\factu2 folder exists and has the permissions, the bug is random, there are emails that it processes and moves attachments to this folder and others that it does not. The script works under Windows, in the paths that I can control I do not use any strange characters (spaces, ! , ñ, etc.. ) but in the mails that come in and attachments I can not control this, I suspect that the failure comes from there, but I do not know what I can do in the script so that it does not fail in these mails.
I copy and paste the output of the script that I am running now, in the last week it has processed 60 mails correctly and 60 I have had to set aside because it could not process it, it gave error, curiously setting aside these mails to another folder the system works again, it is as if some of them "stuck" it.
I will give you an output of the error that occurs with these 60 emails that I have set aside because it does not process them, I can not see anything abnormal.
Output with several e-mails, including one that does get processed.
c:\Users\Administrador.EMPRESA\AppData\Local\Programs\Python\Python39\Scripts>p
y ./attachment-downloader --host 192.168.x.x --username facturas --password Tei
password --imap-folder Inbox --output "d:\facturas" --unsecure --date-after="2
021-12-01T01:00:00" --delete --delete-copy-folder Inbox/Procesado
2021-12-22 12:22:32,759 - INFO - Logging in to: '192.168.x.x' as 'facturas'
2021-12-22 12:22:33,306 - INFO - Connected to IMAP Server with user facturas on
192.168.x.x
2021-12-22 12:22:33,308 - INFO - Listing messages matching the following criteri
a: folder=Inbox, date__gt=2021-12-01
2021-12-22 12:22:33,315 - INFO - Fetch list of messages from folder 'Inbox'
2021-12-22 12:22:33,361 - INFO - Downloaded and parsed mail 'Facturas Enviadas p
or E-Mail Empresa: Sentry Cia. Espec. Control y Serv. S.L.
Factura: 22847 Fecha: 30/11/2021' with 0 attachments
2021-12-22 12:22:33,365 - INFO - Processing message 'b'30284'' subject 'Facturas
Enviadas por E-Mail Empresa: Sentry Cia. Espec. Control y Serv. S.L.
Factura: 22847 Fecha: 30/11/2021'
2021-12-22 12:22:33,423 - INFO - Downloaded and parsed mail 'C/ DE VI1, 1
11-113 002808 ' with 1 attachments
2021-12-22 12:22:33,425 - INFO - Processing message 'b'30285'' subject 'C/ DE VI
1, 111-113 002808 '
2021-12-22 12:22:33,427 - INFO - Downloading attachment '' to path d:\facturas\
2021-12-22 12:22:33,428 - ERROR - [Errno 2] No such file or directory: 'd:\\fact
uras\\'
Traceback (most recent call last):
File "c:\Users\Administrador.EMPRESA\AppData\Local\Programs\Python\Python39\S
cripts\attachment-downloader", line 182, in <module>
with open(download_path, "wb") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'd:\\facturas\\'
2021-12-22 12:22:33,432 - ERROR - Error saving file. Continuing...
2021-12-22 12:22:33,570 - INFO - Downloaded and parsed mail 'Parte Ascensors Sal
es Dast, S.L.' with 1 attachments
2021-12-22 12:22:33,572 - INFO - Processing message 'b'30286'' subject 'Parte As
censors Sales Dast, S.L.'
2021-12-22 12:22:33,574 - INFO - Downloading attachment '' to path d:\facturas\
2021-12-22 12:22:33,575 - ERROR - [Errno 2] No such file or directory: 'd:\\fact
uras\\'
Traceback (most recent call last):
File "c:\Users\Administrador.EMPRESA\AppData\Local\Programs\Python\Python39\S
cripts\attachment-downloader", line 182, in <module>
with open(download_path, "wb") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'd:\\facturas\\'
2021-12-22 12:22:33,578 - ERROR - Error saving file. Continuing...
Traceback (most recent call last):
File "c:\Users\Administrador.EMPRESA\AppData\Local\Programs\Python\Python39\S
cripts\attachment-downloader", line 140, in <module>
for (uid, message) in messages:
File "c:\users\administrador.EMPRESA\appdata\local\programs\python\python39\l
ib\site-packages\imbox\messages.py", line 55, in _fetch_email_list
yield uid, self._fetch_email(uid)
File "c:\users\administrador.EMPRESA\appdata\local\programs\python\python39\l
ib\site-packages\imbox\messages.py", line 42, in _fetch_email
return fetch_email_by_uid(uid=uid,
File "c:\users\administrador.EMPRESA\appdata\local\programs\python\python39\l
ib\site-packages\imbox\parser.py", line 155, in fetch_email_by_uid
email_object = parse_email(raw_email, policy=parser_policy)
File "c:\users\administrador.EMPRESA\appdata\local\programs\python\python39\l
ib\site-packages\imbox\parser.py", line 212, in parse_email
attachment = parse_attachment(part)
File "c:\users\administrador.EMPRESA\appdata\local\programs\python\python39\l
ib\site-packages\imbox\parser.py", line 122, in parse_attachment
filename_parts.insert(int(s_name[1]),value[1:-1] if value.startswith('"') el
se value)
ValueError: invalid literal for int() with base 10: ''
c:\Users\Administrador.EMPRESA\AppData\Local\Programs\Python\Python39\Scripts>
leaving only the mail that works
c:\Users\Administrador.EMPRESA\AppData\Local\Programs\Python\Python39\Scripts>p
y ./attachment-downloader --host 192.168.x.x --username facturas --password Tei
password --imap-folder Inbox --output "d:\facturas" --unsecure --date-after="2
021-12-01T01:00:00" --delete --delete-copy-folder Inbox/Procesado
2021-12-22 12:23:39,191 - INFO - Logging in to: '192.168.x.x' as 'facturas'
2021-12-22 12:23:39,342 - INFO - Connected to IMAP Server with user facturas on
192.168.x.x
2021-12-22 12:23:39,344 - INFO - Listing messages matching the following criteri
a: folder=Inbox, date__gt=2021-12-01
2021-12-22 12:23:39,351 - INFO - Fetch list of messages from folder 'Inbox'
2021-12-22 12:23:39,399 - INFO - Downloaded and parsed mail 'RV: DEV SUMINISTRES
22-12-2021' with 4 attachments
2021-12-22 12:23:39,401 - INFO - Processing message 'b'30294'' subject 'RV: DEV
SUMINISTRES 22-12-2021'
2021-12-22 12:23:39,403 - INFO - Downloading attachment '1C22D001.S9D_DT (3).pdf
' to path d:\facturas\1C22D001.S9D_DT (3).pdf
2021-12-22 12:23:39,405 - INFO - Copy UID 30294 to "Inbox/Procesado" folder
2021-12-22 12:23:39,434 - INFO - Mark UID 30294 with \Deleted FLAG and expunge.
2021-12-22 12:23:39,479 - INFO - Downloading attachment '1C22D001.S9D_DT (2).pdf
' to path d:\facturas\1C22D001.S9D_DT (2).pdf
2021-12-22 12:23:39,481 - INFO - Copy UID 30294 to "Inbox/Procesado" folder
2021-12-22 12:23:39,493 - INFO - Mark UID 30294 with \Deleted FLAG and expunge.
2021-12-22 12:23:39,515 - INFO - Downloading attachment '1C22D001.S9D_DT (1).pdf
' to path d:\facturas\1C22D001.S9D_DT (1).pdf
2021-12-22 12:23:39,517 - INFO - Copy UID 30294 to "Inbox/Procesado" folder
2021-12-22 12:23:39,523 - INFO - Mark UID 30294 with \Deleted FLAG and expunge.
2021-12-22 12:23:39,545 - INFO - Downloading attachment '1C22D001.S9D_DT.pdf' to
path d:\facturas\1C22D001.S9D_DT.pdf
2021-12-22 12:23:39,547 - INFO - Copy UID 30294 to "Inbox/Procesado" folder
2021-12-22 12:23:39,553 - INFO - Mark UID 30294 with \Deleted FLAG and expunge.
2021-12-22 12:23:39,575 - INFO - Finished processing messages
2021-12-22 12:23:39,576 - INFO - Logging out of: 192.168.x.x
Maybe Windows, python and paths are not welcome ???
thank you
I think this part of the log is quite telling:
2021-12-22 12:22:33,425 - INFO - Processing message 'b'30285'' subject 'C/ DE VI
1, 111-113 002808 '
2021-12-22 12:22:33,427 - INFO - Downloading attachment '' to path d:\facturas\
What attachments does that email have? What are they called? For some reason it looks like the imbox library isn't able to pull back the filename?
sorry James, I didn't see your answer (my name is Jaime too, James in Spanish).
Well yes, it seems to have something to do with the name of the attachments, mysteriously some get through and others don't.
Right now I have a lot of stopped emails again, in the end what I have to do is to remove emails until it "hooks" and downloads them, but I can't find any relation other than that the attachments come with spaces, strange characters, etc... but some go through and others don't... ¿??
I don't know if I can or you can send me a private to talk to you.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.