Skip to content

[17.0][ADD] fetchmail_local_mailbox #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions fetchmail_local_mailbox/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
=============================
Email gateway - local mailbox
=============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:9395f3619462d9e3a6bdf38f4a10c88c64ae5729d606e6234389efc408b82b82
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github
:target: https://github.com/OCA/server-backend/tree/17.0/fetchmail_local_mailbox
:alt: OCA/server-backend
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-backend-17-0/server-backend-17-0-fetchmail_local_mailbox
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-backend&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to retrieve mails from a mailbox on the local file
system.

That's useful when your Odoo instance runs on a server with a local
mailserver that delivers mails into a mailbox accessible by the user
Odoo is running as, because it allows you to fetch mails without any
credentials or TCP/connection overhead.

**Table of contents**

.. contents::
:local:

Usage
=====

To use this module, you need to:

1. select server type ``Local mailbox`` in a fetchmail configuration
2. fill in the full path to the mailbox in the server's filesystem
3. in case you want to delete mails from the mailbox after processing,
check ``Delete processed mails from mailbox``

Known issues / Roadmap
======================

- support other mailbox formats than maildir

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-backend/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 <https://github.com/OCA/server-backend/issues/new?body=module:%20fetchmail_local_mailbox%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
-------

* Hunki Enterprises BV

Contributors
------------

- Holger Brunn <[email protected]>
(https://hunki-enterprises.com)

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

.. |maintainer-hbrunn| image:: https://github.com/hbrunn.png?size=40px
:target: https://github.com/hbrunn
:alt: hbrunn

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-hbrunn|

This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/17.0/fetchmail_local_mailbox>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions fetchmail_local_mailbox/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
14 changes: 14 additions & 0 deletions fetchmail_local_mailbox/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Email gateway - local mailbox",
"summary": "Fetch emails from local mailboxes",
"version": "17.0.1.0.0",
"development_status": "Beta",
"category": "Extra Tools",
"website": "https://github.com/OCA/server-backend",
"author": "Hunki Enterprises BV, Odoo Community Association (OCA)",
"maintainers": ["hbrunn"],
"license": "AGPL-3",
"depends": ["mail"],
"data": ["views/fetchmail_server.xml"],
}
9 changes: 9 additions & 0 deletions fetchmail_local_mailbox/examples/test_fetchmail_local_mailbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
From: root@fetchmail_local_mailbox_test
To: partner@fetchmail_local_mailbox_test
Subject: A new partner
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-Id: <42@fetchmail_local_mailbox_test>

This mail should create a new partner
3 changes: 3 additions & 0 deletions fetchmail_local_mailbox/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import fetchmail_server
100 changes: 100 additions & 0 deletions fetchmail_local_mailbox/models/fetchmail_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2020 Hunki Enterprises BV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import logging
import mailbox

from odoo import fields, models

_logger = logging.getLogger(__name__)


class FetchmailServer(models.Model):
_inherit = "fetchmail.server"

server_type = fields.Selection(
selection_add=[("local_mailbox", "Local mailbox")],
ondelete={"local_mailbox": "cascade"},
)
mailbox_type = fields.Selection([("maildir", "Maildir")], default="maildir")
mailbox_path = fields.Char()
mailbox_delete_processed = fields.Boolean("Delete processed mails from mailbox")

def connect(self, allow_archived=False):
self.ensure_one()
if self.server_type == "local_mailbox":
mailbox_type2class = {
"maildir": mailbox.Maildir,
}
return mailbox_type2class[self.mailbox_type](self.mailbox_path or "")
return super().connect(allow_archived=allow_archived)

Check warning on line 30 in fetchmail_local_mailbox/models/fetchmail_server.py

View check run for this annotation

Codecov / codecov/patch

fetchmail_local_mailbox/models/fetchmail_server.py#L30

Added line #L30 was not covered by tests

def fetch_mail(self):
for this in self:
if this.server_type != "local_mailbox":
continue

Check warning on line 35 in fetchmail_local_mailbox/models/fetchmail_server.py

View check run for this annotation

Codecov / codecov/patch

fetchmail_local_mailbox/models/fetchmail_server.py#L35

Added line #L35 was not covered by tests
_logger.info("start checking for new emails in %s", this.mailbox_path)
extra_context = {
"fetchmail_cron_running": True,
"fetchmail_server_id": this.id,
"server_type": this.server_type,
}
processing_func = getattr(
this.with_context(**extra_context),
"_process_mail_%s" % this.mailbox_type,
)
try:
count = failed = 0
mail_box = this.connect()
mail_box.lock()
for key, message in mail_box.items():
try:
processing_func(mail_box, key, message)
except Exception:
failed += 1
_logger.info(
"Failed to process mail %s from %s.",
key,
this.mailbox_path,
exc_info=True,
)
count += 1
# pylint: disable=invalid-commit
self.env.cr.commit()
mail_box.close()
_logger.info(
"Fetched %d email(s) from %s; %d succeeded, %d failed.",
count,
this.mailbox_path,
(count - failed),
failed,
)
except Exception:
_logger.info(
"General failure when trying to fetch mail from %s.",
this.mailbox_path,
exc_info=True,
)
this.write({"date": fields.Datetime.now()})
return super(
FetchmailServer,
self.filtered(lambda x: x.server_type != "local_mailbox"),
).fetch_mail()

def _process_mail_maildir(self, mail_box, key, message):
"""Process a mail from a maildir mailbox"""
if not self.mailbox_delete_processed and "S" in message.get_flags():
return
if message.get_subdir() == "new":
message.set_subdir("cur")
result = self.env["mail.thread"].message_process(
self.object_id.model,
message.as_string(),
save_original=self.original,
strip_attachments=(not self.attach),
)
message.add_flag("S")
mail_box[key] = message
if self.mailbox_delete_processed:
mail_box.discard(key)
return result
3 changes: 3 additions & 0 deletions fetchmail_local_mailbox/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions fetchmail_local_mailbox/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Holger Brunn \<<[email protected]>\>
(<https://hunki-enterprises.com>)
7 changes: 7 additions & 0 deletions fetchmail_local_mailbox/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This module allows to retrieve mails from a mailbox on the local file
system.

That's useful when your Odoo instance runs on a server with a local
mailserver that delivers mails into a mailbox accessible by the user
Odoo is running as, because it allows you to fetch mails without any
credentials or TCP/connection overhead.
1 change: 1 addition & 0 deletions fetchmail_local_mailbox/readme/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- support other mailbox formats than maildir
6 changes: 6 additions & 0 deletions fetchmail_local_mailbox/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To use this module, you need to:

1. select server type `Local mailbox` in a fetchmail configuration
2. fill in the full path to the mailbox in the server's filesystem
3. in case you want to delete mails from the mailbox after processing,
check `Delete processed mails from mailbox`
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading