Prevent archiving in-use mail servers by displaying an error message that lists where it is still used, allowing to easily identify what need to be updated before being able to archive the mail server. Additionally, - prevent the use of archived server as a fall-safe - when duplicating a mailing with an archived mail server, replace mail server by the default one Detailed explanation: 1. A check has been added that raise an exception when trying to connect to the smtp server or send an email when the server is archived. With that solution, - testing the connection of an archived server displays an error telling that an archived server cannot be used. - if a mail is still sent with an archived server, mail are in error : "Connection failed (outgoing mail server problem)" This fail-safe ensures that no mail will be sent through an archived mail server and that the user will get some feedback about it. The same fail-safe for the incoming mail server has been added. Notes: - the connection will outlive the archiving of a mail server still allowing to send email through the archived server until the connection is closed. But connection are not kept for long so this shouldn't be a problem. - it cannot be tested because the connect method return immediately in test mode. 2. When a mail server is archived, an user error is raised if it is in-use. The implementation relies on each module to override the method "_active_usages_compute" in "ir_mail_server" to complete the list with user-friendly message describing the active elements that could send mail through the mail server. This has been implemented for: - l10n_it_edi: server used to send e-invoice - mail: optional server configured for template - mass_mailing: -- default mail server -- active server configured for mailing Mail server are referenced in other elements but are not active anymore, it is just for temporary or history purpose. Those references doesn’t prevent the archiving of the mail server: - mail_message - wizard survey_invite and compose_message - res_config_settings Task-2821516 closes odoo/odoo#91240 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
275 lines
13 KiB
Python
275 lines
13 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import logging
|
|
import poplib
|
|
import socket
|
|
|
|
from imaplib import IMAP4, IMAP4_SSL
|
|
from poplib import POP3, POP3_SSL
|
|
from socket import gaierror, timeout
|
|
from ssl import SSLError
|
|
|
|
from odoo import api, fields, models, tools, _
|
|
from odoo.exceptions import UserError
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
MAX_POP_MESSAGES = 50
|
|
MAIL_TIMEOUT = 60
|
|
|
|
# Workaround for Python 2.7.8 bug https://bugs.python.org/issue23906
|
|
poplib._MAXLINE = 65536
|
|
|
|
# Add timeout to IMAP connections
|
|
# HACK https://bugs.python.org/issue38615
|
|
# TODO: clean in Python 3.9
|
|
IMAP4._create_socket = lambda self, timeout=MAIL_TIMEOUT: socket.create_connection((self.host or None, self.port), timeout)
|
|
|
|
|
|
class FetchmailServer(models.Model):
|
|
"""Incoming POP/IMAP mail server account"""
|
|
|
|
_name = 'fetchmail.server'
|
|
_description = 'Incoming Mail Server'
|
|
_order = 'priority'
|
|
|
|
name = fields.Char('Name', required=True)
|
|
active = fields.Boolean('Active', default=True)
|
|
state = fields.Selection([
|
|
('draft', 'Not Confirmed'),
|
|
('done', 'Confirmed'),
|
|
], string='Status', index=True, readonly=True, copy=False, default='draft')
|
|
server = fields.Char(string='Server Name', readonly=True, help="Hostname or IP of the mail server", states={'draft': [('readonly', False)]})
|
|
port = fields.Integer(readonly=True, states={'draft': [('readonly', False)]})
|
|
server_type = fields.Selection([
|
|
('imap', 'IMAP Server'),
|
|
('pop', 'POP Server'),
|
|
('local', 'Local Server'),
|
|
], string='Server Type', index=True, required=True, default='pop')
|
|
server_type_info = fields.Text('Server Type Info', compute='_compute_server_type_info')
|
|
is_ssl = fields.Boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)")
|
|
attach = fields.Boolean('Keep Attachments', help="Whether attachments should be downloaded. "
|
|
"If not enabled, incoming emails will be stripped of any attachments before being processed", default=True)
|
|
original = fields.Boolean('Keep Original', help="Whether a full original copy of each email should be kept for reference "
|
|
"and attached to each processed message. This will usually double the size of your message database.")
|
|
date = fields.Datetime(string='Last Fetch Date', readonly=True)
|
|
user = fields.Char(string='Username', readonly=True, states={'draft': [('readonly', False)]})
|
|
password = fields.Char(readonly=True, states={'draft': [('readonly', False)]})
|
|
object_id = fields.Many2one('ir.model', string="Create a New Record", help="Process each incoming mail as part of a conversation "
|
|
"corresponding to this document type. This will create "
|
|
"new documents for new conversations, or attach follow-up "
|
|
"emails to the existing conversations (documents).")
|
|
priority = fields.Integer(string='Server Priority', readonly=True, states={'draft': [('readonly', False)]}, help="Defines the order of processing, lower values mean higher priority", default=5)
|
|
message_ids = fields.One2many('mail.mail', 'fetchmail_server_id', string='Messages', readonly=True)
|
|
configuration = fields.Text('Configuration', readonly=True)
|
|
script = fields.Char(readonly=True, default='/mail/static/scripts/odoo-mailgate.py')
|
|
|
|
@api.depends('server_type')
|
|
def _compute_server_type_info(self):
|
|
for server in self:
|
|
if server.server_type == 'local':
|
|
server.server_type_info = _('Use a local script to fetch your emails and create new records.')
|
|
else:
|
|
server.server_type_info = False
|
|
|
|
@api.onchange('server_type', 'is_ssl', 'object_id')
|
|
def onchange_server_type(self):
|
|
self.port = 0
|
|
if self.server_type == 'pop':
|
|
self.port = self.is_ssl and 995 or 110
|
|
elif self.server_type == 'imap':
|
|
self.port = self.is_ssl and 993 or 143
|
|
|
|
conf = {
|
|
'dbname': self.env.cr.dbname,
|
|
'uid': self.env.uid,
|
|
'model': self.object_id.model if self.object_id else 'MODELNAME'
|
|
}
|
|
self.configuration = """Use the below script with the following command line options with your Mail Transport Agent (MTA)
|
|
odoo-mailgate.py --host=HOSTNAME --port=PORT -u %(uid)d -p PASSWORD -d %(dbname)s
|
|
Example configuration for the postfix mta running locally:
|
|
/etc/postfix/virtual_aliases: @youdomain odoo_mailgate@localhost
|
|
/etc/aliases:
|
|
odoo_mailgate: "|/path/to/odoo-mailgate.py --host=localhost -u %(uid)d -p PASSWORD -d %(dbname)s"
|
|
""" % conf
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
res = super(FetchmailServer, self).create(vals_list)
|
|
self._update_cron()
|
|
return res
|
|
|
|
def write(self, values):
|
|
res = super(FetchmailServer, self).write(values)
|
|
self._update_cron()
|
|
return res
|
|
|
|
def unlink(self):
|
|
res = super(FetchmailServer, self).unlink()
|
|
self._update_cron()
|
|
return res
|
|
|
|
def set_draft(self):
|
|
self.write({'state': 'draft'})
|
|
return True
|
|
|
|
def connect(self, allow_archived=False):
|
|
"""
|
|
:param bool allow_archived: by default (False), an exception is raised when calling this method on an
|
|
archived record. It can be set to True for testing so that the exception is no longer raised.
|
|
"""
|
|
self.ensure_one()
|
|
if not allow_archived and not self.active:
|
|
raise UserError(_('The server "%s" cannot be used because it is archived.', self.display_name))
|
|
connection_type = self._get_connection_type()
|
|
if connection_type == 'imap':
|
|
if self.is_ssl:
|
|
connection = IMAP4_SSL(self.server, int(self.port))
|
|
else:
|
|
connection = IMAP4(self.server, int(self.port))
|
|
self._imap_login(connection)
|
|
elif connection_type == 'pop':
|
|
if self.is_ssl:
|
|
connection = POP3_SSL(self.server, int(self.port), timeout=MAIL_TIMEOUT)
|
|
else:
|
|
connection = POP3(self.server, int(self.port), timeout=MAIL_TIMEOUT)
|
|
#TODO: use this to remove only unread messages
|
|
#connection.user("recent:"+server.user)
|
|
connection.user(self.user)
|
|
connection.pass_(self.password)
|
|
return connection
|
|
|
|
def _imap_login(self, connection):
|
|
"""Authenticate the IMAP connection.
|
|
|
|
Can be overridden in other module for different authentication methods.
|
|
|
|
:param connection: The IMAP connection to authenticate
|
|
"""
|
|
self.ensure_one()
|
|
connection.login(self.user, self.password)
|
|
|
|
def button_confirm_login(self):
|
|
for server in self:
|
|
try:
|
|
connection = server.connect(allow_archived=True)
|
|
server.write({'state': 'done'})
|
|
except UnicodeError as e:
|
|
raise UserError(_("Invalid server name !\n %s", tools.ustr(e)))
|
|
except (gaierror, timeout, IMAP4.abort) as e:
|
|
raise UserError(_("No response received. Check server information.\n %s", tools.ustr(e)))
|
|
except (IMAP4.error, poplib.error_proto) as err:
|
|
raise UserError(_("Server replied with following exception:\n %s", tools.ustr(err)))
|
|
except SSLError as e:
|
|
raise UserError(_("An SSL exception occurred. Check SSL/TLS configuration on server port.\n %s", tools.ustr(e)))
|
|
except (OSError, Exception) as err:
|
|
_logger.info("Failed to connect to %s server %s.", server.server_type, server.name, exc_info=True)
|
|
raise UserError(_("Connection test failed: %s", tools.ustr(err)))
|
|
finally:
|
|
try:
|
|
if connection:
|
|
connection_type = server._get_connection_type()
|
|
if connection_type == 'imap':
|
|
connection.close()
|
|
elif connection_type == 'pop':
|
|
connection.quit()
|
|
except Exception:
|
|
# ignored, just a consequence of the previous exception
|
|
pass
|
|
return True
|
|
|
|
@api.model
|
|
def _fetch_mails(self):
|
|
""" Method called by cron to fetch mails from servers """
|
|
return self.search([('state', '=', 'done'), ('server_type', '!=', 'local')]).fetch_mail()
|
|
|
|
def fetch_mail(self):
|
|
""" WARNING: meant for cron usage only - will commit() after each email! """
|
|
additionnal_context = {
|
|
'fetchmail_cron_running': True
|
|
}
|
|
MailThread = self.env['mail.thread']
|
|
for server in self:
|
|
_logger.info('start checking for new emails on %s server %s', server.server_type, server.name)
|
|
additionnal_context['default_fetchmail_server_id'] = server.id
|
|
count, failed = 0, 0
|
|
imap_server = None
|
|
pop_server = None
|
|
connection_type = server._get_connection_type()
|
|
if connection_type == 'imap':
|
|
try:
|
|
imap_server = server.connect()
|
|
imap_server.select()
|
|
result, data = imap_server.search(None, '(UNSEEN)')
|
|
for num in data[0].split():
|
|
res_id = None
|
|
result, data = imap_server.fetch(num, '(RFC822)')
|
|
imap_server.store(num, '-FLAGS', '\\Seen')
|
|
try:
|
|
res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, data[0][1], save_original=server.original, strip_attachments=(not server.attach))
|
|
except Exception:
|
|
_logger.info('Failed to process mail from %s server %s.', server.server_type, server.name, exc_info=True)
|
|
failed += 1
|
|
imap_server.store(num, '+FLAGS', '\\Seen')
|
|
self._cr.commit()
|
|
count += 1
|
|
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", count, server.server_type, server.name, (count - failed), failed)
|
|
except Exception:
|
|
_logger.info("General failure when trying to fetch mail from %s server %s.", server.server_type, server.name, exc_info=True)
|
|
finally:
|
|
if imap_server:
|
|
imap_server.close()
|
|
imap_server.logout()
|
|
elif connection_type == 'pop':
|
|
try:
|
|
while True:
|
|
failed_in_loop = 0
|
|
num = 0
|
|
pop_server = server.connect()
|
|
(num_messages, total_size) = pop_server.stat()
|
|
pop_server.list()
|
|
for num in range(1, min(MAX_POP_MESSAGES, num_messages) + 1):
|
|
(header, messages, octets) = pop_server.retr(num)
|
|
message = (b'\n').join(messages)
|
|
res_id = None
|
|
try:
|
|
res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, message, save_original=server.original, strip_attachments=(not server.attach))
|
|
pop_server.dele(num)
|
|
except Exception:
|
|
_logger.info('Failed to process mail from %s server %s.', server.server_type, server.name, exc_info=True)
|
|
failed += 1
|
|
failed_in_loop += 1
|
|
self.env.cr.commit()
|
|
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", num, server.server_type, server.name, (num - failed_in_loop), failed_in_loop)
|
|
# Stop if (1) no more message left or (2) all messages have failed
|
|
if num_messages < MAX_POP_MESSAGES or failed_in_loop == num:
|
|
break
|
|
pop_server.quit()
|
|
except Exception:
|
|
_logger.info("General failure when trying to fetch mail from %s server %s.", server.server_type, server.name, exc_info=True)
|
|
finally:
|
|
if pop_server:
|
|
pop_server.quit()
|
|
server.write({'date': fields.Datetime.now()})
|
|
return True
|
|
|
|
def _get_connection_type(self):
|
|
"""Return which connection must be used for this mail server (IMAP or POP).
|
|
Can be overridden in sub-module to define which connection to use for a specific
|
|
"server_type" (e.g. Gmail server).
|
|
"""
|
|
self.ensure_one()
|
|
return self.server_type
|
|
|
|
@api.model
|
|
def _update_cron(self):
|
|
if self.env.context.get('fetchmail_cron_running'):
|
|
return
|
|
try:
|
|
# Enabled/Disable cron based on the number of 'done' server of type pop or imap
|
|
cron = self.env.ref('mail.ir_cron_mail_gateway_action')
|
|
cron.toggle(model=self._name, domain=[('state', '=', 'done'), ('server_type', '!=', 'local')])
|
|
except ValueError:
|
|
pass
|