mirror of
https://github.com/ciur/papermerge.git
synced 2026-04-25 12:05:58 +03:00
[GH-ISSUE #233] IMAP Consumption - Mails imported multiple times and not marked as read or deleted #185
Labels
No labels
2.1
3.0
3.0.1
3.0.2
3.0.3
3.0.3
3.1
3.2
3.2
3.3
3.5
3.x
Fixed. Waiting for feedback.
Fixed. Waiting for feedback.
UX
Version 2.1 - alpha
XSS
announcement
beta
blocker
bug
cannot reproduce
confirmed
confirmed
critical
demo
dependencies
deployment
detchnical debt
discussion
docker
documentation
donations
duplicate
enhancement
feature request
frontend
fundraising
good first issue
good issue
help wanted
high
implemented
important
improvement
incomplete
invalid
investigation
kubernetes
low
low impact
medium
medium
medium impact
migration from 2.0
migration from 2.1
missing-language
missing-ocr-language
no-activity
note
ocr
outofscope
packaging
performance
popular request
pull-request
pypi
question
raspberry pi
roadmap
search
security
setup
status
task
technical debt
updates
user xp
version 1.4.0 - demo
will be implemented
will not be implemented
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/papermerge#185
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @l4rm4nd on GitHub (Nov 28, 2020).
Original GitHub issue: https://github.com/ciur/papermerge/issues/233
Originally assigned to: @ciur on GitHub.
Info
I am currently using the docker image provided by https://github.com/linuxserver/docker-papermerge. However, I guess it is a logical bug not based on docker itself. Papermerge is running on my Raspberry Pi 4.
Expected Behavior
Using the
IMPORT_MAIL_DELETEoption inpapermerge.conf.pyshould delete processed emails from the IMAP account and the email should be imported once into the inbox of the superuser papermerge account. Further, the mail should be marked as read if not deleted .. or how does the papermerge workers differentiate new vs. old mails?Current Behavior
The email is successfully processed by the worker and imported into the papermerge web application. However, emails are not marked as read or deleted. This leads to an issue, where the same email is being imported over and over again into the inbox folder of papermerge's web application.
Steps to Reproduce
papermerge.conf.pyto process emailsEnvironment
Raspberry Pi 4 - ARM
Linux omv 4.19.75-v7l+ #1271 armv7l GNU/Linux
Docker version 19.03.13, build 4484c46
linuxserver/papermerge:latest
@ciur commented on GitHub (Feb 22, 2021):
@l4rm4nd, I refactoring the code and added more detailed documentation for docs consumtion via IMAP.
I have just tested this feature and it works (i.e. with
IMPORT_MAIL_DELETE=Trueemail is deleted and withIMPORT_MAIL_DELETE=Falseit is not) for latest development version (soon to be out version 2.0).@l4rm4nd commented on GitHub (Feb 25, 2021):
Hey @ciur, I've upgraded to version 2.0. Looks amazing, nicely done!
However, the IMAP consumption still doesn't work for me. The mails are properly processed and the attached documents are imported into Papermerge. So far, so good. I use the following config:
But the processed mail is not marked as read nor deleted and imported over and over again into Papermerge's inbox.
@l4rm4nd commented on GitHub (Mar 8, 2021):
I've also tested around and noticed that the following line of code can successfully mark my mails as read:
server.add_flags(uid, br'\Seen')The following flag marks an email as deleted. However, the mail is hereby not marked as seen nor moved from INBOX to TRASH for my mail provider Strato:
server.add_flags(uid, br'\Deleted')Tested these things against https://github.com/ciur/papermerge/blob/stable/1.5.x/papermerge/core/importers/imap.py (line 136).
Marking an imported e-mail as seen with the code above would fix the multiple mail imports.
@l4rm4nd commented on GitHub (Mar 8, 2021):
Here the basic code for my tests:
@ciur commented on GitHub (Mar 9, 2021):
@l4rm4nd, oh, man, I am impressed 👍 . I didn't know about
server.add_flags(uid, br'\Seen')! Thank you for the tip! I will review/test/have a look at the issue in couple of days. Right now, I am recording screencasts and writing blog posts about Paparmerge features. If you want (maybe you need fix urgently, like today :) ), you may create a PR with the changes.In 2.0 the whole IMAP code was refactored taking into account import by user and import by secret features; imho now code in
papermerge.core.importers.imapmodule is cleaner and easier to read. The entry point for importing document attachments is here, which as you can see is part of different repository (papermerge-core is a reusable django app).I like you security acumen 🥇 , maybe you may wear, for a day or two, developer hat 🎩 as well :)
Also, you might be interested in new (again in 2.0, thus it is in papermerge-core repository) management command called imap, I wrote it to quickly troubleshoot IMAP related issues. You use it like any standard django command:
@l4rm4nd commented on GitHub (Mar 9, 2021):
Hah, funny. I've looked at your code to understand why mails are not deleted for my Strato IMAP account.
According to the IMAPClient documentation, the function
delete_messages()only sets theDELETEDflag for an email message viaadd_flags().As tested by myself above, flagging an email as deleted won't do anything for Strato accounts.
So it seems that this is an individual bug for the provider Strato. I'll research more, maybe I can find a fix.Turns out some mail providers are RFC compliant and only delete flagged mails after callingexpunge(). See IMAPv4 RFC and IMAPClient documentation.@l4rm4nd commented on GitHub (Mar 9, 2021):
Pushed a new pull request for papermerge-core.
This fixes the multiple import of mails by setting the SEEN flag via
add_flags(uid, br'\Seen'). Further, it fixes the issue where mails are not deleted for some mail providers. Issue is that not all mail providers are RFC compliant and act differently on flagged mails. IMAPClient'sdelete_messages()function only marks mails with theDeletedflag. However, in order to delete flagged mails, one have to callexpunge(). If both functions are used, we are RFC compliant and all processed mails should be successfully deleted across various mail providers.Further, I recommend a new approach for deleting mails. For the current release of papermerge-core, if a user specifies the consumption optionIMPORT_MAIL_DELETE=Trueto delete mails after processing, the mails are permanently deleted. You won't be able to find the mail again, since it is not moved to a folder likeTrashor so. This can be kinda weird + the deletion might not even work for some mail providers (e.g. STRATO).New approach is:- Mark all processed mails as seen/read to indicate successful import- Move mails to a folder likeTrashif a user enabledIMPORT_MAIL_DELETEas consumption optionUsers might specify a custom folder for 'deleted' mails in future releases as consumption option (e.g.IMPORT_MAIL_DELETE_FOLDER). This is not implemented yet, since the variabletrash_folderinemail_iterator()is currently not overwritten by a user's variabe and per default set to the stringTrash.@l4rm4nd commented on GitHub (Mar 9, 2021):
Another interesting resource states, that IMAPClient's
delete_messages()has to additionally useexpunge()to take effect.@ciur commented on GitHub (Mar 10, 2021):
Now fix is in Papermerge Core 2.0.0rc48.
During next couple of days I will:
It will take some time, but finally will be done right.
(now I manually build and publish docker image, which is a pain)
@ciur commented on GitHub (Mar 12, 2021):
@l4rm4nd, release 2.0.0rc48 is out and it contains your IMAP related fixes! 🎉
@l4rm4nd commented on GitHub (Mar 12, 2021):
Hey @ciur , thanks for your new release!
Unfortunately, I am getting some internal server errors with the newest version.
See #345
@l4rm4nd commented on GitHub (Mar 15, 2021):
Fixed. See https://github.com/linuxserver/docker-papermerge/issues/32