From 2b44724d825e3bcb231639a632069934b8d9d18e Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Thu, 25 Apr 2024 13:00:31 +0200 Subject: [PATCH] [IMP] fetchmail_.._folder: optionally archive messages automatically --- fetchmail_attach_from_folder/README.rst | 6 +- .../models/fetchmail_server_folder.py | 27 + fetchmail_attach_from_folder/readme/USAGE.md | 4 + .../static/description/index.html | 481 ++++++++++++++++++ .../views/fetchmail_server.xml | 2 + 5 files changed, 519 insertions(+), 1 deletion(-) create mode 100644 fetchmail_attach_from_folder/static/description/index.html diff --git a/fetchmail_attach_from_folder/README.rst b/fetchmail_attach_from_folder/README.rst index 5e0873f7a..431c9a66c 100644 --- a/fetchmail_attach_from_folder/README.rst +++ b/fetchmail_attach_from_folder/README.rst @@ -7,7 +7,7 @@ Email gateway - folders !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:95f6645118da34dd962fa794f5fca9c8797579a9a585d80228407f4997e9ba91 + !! source digest: sha256:907101d997473fcc276317ee6d3bf95f2f005eab22b585dd59644a757959ca10 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -91,6 +91,10 @@ Odoo and attached to the partner in question. After some testing, you might want to check ``Delete matches`` in your folder configuration so that this folder doesn't grow indefinitely. +Another way to prevent having to process ever more messages from the +folder to read is to automatically move all processed messages to an +archive folder that can be specified. + Bug Tracker =========== diff --git a/fetchmail_attach_from_folder/models/fetchmail_server_folder.py b/fetchmail_attach_from_folder/models/fetchmail_server_folder.py index a73359675..cd7e36b48 100644 --- a/fetchmail_attach_from_folder/models/fetchmail_server_folder.py +++ b/fetchmail_attach_from_folder/models/fetchmail_server_folder.py @@ -31,6 +31,9 @@ class FetchmailServerFolder(models.Model): help="The path to your mail folder." " Typically would be something like 'INBOX.myfolder'", ) + archive_path = fields.Char( + help="The path where successfully retrieved messages will be stored.", + ) model_id = fields.Many2one( comodel_name="ir.model", required=True, @@ -123,6 +126,7 @@ class FetchmailServerFolder(models.Model): try: # New connection per folder connection = this.server_id.connect() + this.check_imap_archive_folder(connection) this.retrieve_imap_folder(connection) connection.close() except Exception: @@ -141,6 +145,20 @@ class FetchmailServerFolder(models.Model): if connection: connection.logout() + def check_imap_archive_folder(self, connection): + """If archive folder specified, check existance and create when needed.""" + self.ensure_one() + server = self.server_id + if not self.archive_path: + return + if connection.select(self.archive_path)[0] != "OK": + connection.create(self.archive_path) + if connection.select(self.archive_path)[0] != "OK": + raise UserError( + _("Could not create archive folder %(folder)s on server %(server)s") + % {"folder": self.archive_path, "server": server.name} + ) + def retrieve_imap_folder(self, connection): """Retrieve all mails for one IMAP folder.""" self.ensure_one() @@ -204,6 +222,8 @@ class FetchmailServerFolder(models.Model): ) matched = True if thread_id else False self.update_msg(connection, msgid, matched=matched) + if self.archive_path: + self._archive_msg(connection, msgid) def fetch_msg(self, connection, msgid): """Select a single message from a folder.""" @@ -227,3 +247,10 @@ class FetchmailServerFolder(models.Model): else: if self.flag_nonmatching: connection.store(msgid, "+FLAGS", "\\FLAGGED") + + def _archive_msg(self, connection, msgid): + """Archive message. Folder should already have been created.""" + self.ensure_one() + connection.copy(msgid, self.archive_path) + connection.store(msgid, "+FLAGS", "\\Deleted") + connection.expunge() diff --git a/fetchmail_attach_from_folder/readme/USAGE.md b/fetchmail_attach_from_folder/readme/USAGE.md index fc8b98e59..0c7af37f7 100644 --- a/fetchmail_attach_from_folder/readme/USAGE.md +++ b/fetchmail_attach_from_folder/readme/USAGE.md @@ -10,3 +10,7 @@ Now when your users drop mails into this folder, they will be fetched by Odoo and attached to the partner in question. After some testing, you might want to check `Delete matches` in your folder configuration so that this folder doesn't grow indefinitely. + +Another way to prevent having to process ever more messages from the folder +to read is to automatically move all processed messages to an archive folder +that can be specified. diff --git a/fetchmail_attach_from_folder/static/description/index.html b/fetchmail_attach_from_folder/static/description/index.html new file mode 100644 index 000000000..a040a917e --- /dev/null +++ b/fetchmail_attach_from_folder/static/description/index.html @@ -0,0 +1,481 @@ + + + + + + +Email gateway - folders + + + +
+

Email gateway - folders

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

Adds the possibility to attach emails from a certain IMAP folder to +objects, ie partners. Matching is done via several algorithms, ie email +address, email address’s domain or the original Odoo algorithm.

+

This gives a simple possibility to archive emails in Odoo without a mail +client integration.

+

Table of contents

+ +
+

Configuration

+

In your fetchmail configuration, you’ll find a new list field +Folders to  monitor. Add your folders here in IMAP notation (usually +something like INBOX.your_folder_name.your_subfolder_name), choose a +model to attach mails to and a matching algorithm to use.

+
+

Exact mailaddress

+

Fill in a field to search for the email address in Field (model). +For partners, this would be email. Also fill in the header field +from the email to look at in Field (email). If you want to match +incoming mails from your customers, this would be from. You can also +list header fields, so to match partners receiving this email, you might +fill in to,cc,bcc.

+
+
+

Domain of email addresses

+

Match the domain of the email address(es) found in Field (email). +This would attach a mail to test1@example.com to a record with +Field (model) set to test2@example.com. Given that this is a +fuzzy match, you probably want to check Use 1st match, because +otherwise nothing happens if multiple possible matches are found.

+
+
+

Odoo standard

+

This is stricly speaking no matching algorithm, but calls the model’s +standard action on new incoming mail, which is usually creating a new +record.

+
+
+
+

Usage

+

A widespread configuration is to have a shared mailbox with several +folders, i.e. one where users drop mails they want to attach to +partners. Let this folder be called From partners. Then create a +folder configuration for your server with path "INBOX.From partners" +(note the quotes because of the space, this is server dependent). Choose +model Partners, set Field (model) to email and +Field (email) to from. In Domain, you could fill in +[('customer', '=', True)] to be sure to only match customer records.

+

Now when your users drop mails into this folder, they will be fetched by +Odoo and attached to the partner in question. After some testing, you +might want to check Delete matches in your folder configuration so +that this folder doesn’t grow indefinitely.

+

Another way to prevent having to process ever more messages from the +folder to read is to automatically move all processed messages to an +archive folder that can be specified.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Therp BV
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/server-tools project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/fetchmail_attach_from_folder/views/fetchmail_server.xml b/fetchmail_attach_from_folder/views/fetchmail_server.xml index 47a383e3f..9cf93de4b 100644 --- a/fetchmail_attach_from_folder/views/fetchmail_server.xml +++ b/fetchmail_attach_from_folder/views/fetchmail_server.xml @@ -24,6 +24,7 @@ + @@ -98,6 +99,7 @@ +