Skip to content

Commit

Permalink
Merge PR #1762 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by JordiBForgeFlow
  • Loading branch information
OCA-git-bot committed Apr 16, 2020
2 parents bb50570 + 92d3db6 commit 8497641
Show file tree
Hide file tree
Showing 10 changed files with 627 additions and 0 deletions.
76 changes: 76 additions & 0 deletions mail_server_relay_disallowed/README.rst
@@ -0,0 +1,76 @@
============================
Mail Server Relay Disallowed
============================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/13.0/mail_server_relay_disallowed
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-13-0/server-tools-13-0-mail_server_relay_disallowed
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/13.0
:alt: Try me on Runbot

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

Fixes the issue of using a mail client where relaying is disallowed.
See:
https://www.odoo.com/es_ES/forum/help-1/question/zoho-com-connection-unexpectedly-closed-51162

**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20mail_server_relay_disallowed%0Aversion:%2013.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
~~~~~~~

* ForgeFlow
* Project Expert Team

Contributors
~~~~~~~~~~~~

* ForgeFlow <http://www.eficent.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.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/13.0/mail_server_relay_disallowed>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions mail_server_relay_disallowed/__init__.py
@@ -0,0 +1 @@
from . import models
13 changes: 13 additions & 0 deletions mail_server_relay_disallowed/__manifest__.py
@@ -0,0 +1,13 @@
# Copyright 2020 ForgeFlow, S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Mail Server Relay Disallowed",
"version": "13.0.1.0.0",
"author": "ForgeFlow, Project Expert Team, Odoo Community Association (OCA)",
"contributors": ["Jordi Ballester <jordi.ballester@forgeflow.com>"],
"website": "https://github.com/OCA/server-tools",
"license": "AGPL-3",
"category": "Social Network",
"depends": ["base"],
"installable": True,
}
1 change: 1 addition & 0 deletions mail_server_relay_disallowed/models/__init__.py
@@ -0,0 +1 @@
from . import ir_mail_server
132 changes: 132 additions & 0 deletions mail_server_relay_disallowed/models/ir_mail_server.py
@@ -0,0 +1,132 @@
# Copyright 2020 ForgeFlow, S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import logging
import smtplib
import threading

from odoo import api, models
from odoo.tools import ustr
from odoo.tools.translate import _

from odoo.addons.base.models.ir_mail_server import (
MailDeliveryException,
extract_rfc2822_addresses,
)

_logger = logging.getLogger(__name__)
_test_logger = logging.getLogger("odoo.tests")


class IrMailServer(models.Model):
_inherit = "ir.mail_server"

NO_VALID_RECIPIENT = (
"At least one valid recipient address should be "
"specified for outgoing emails (To/Cc/Bcc)"
)

@api.model
def send_email(
self,
message,
mail_server_id=None,
smtp_server=None,
smtp_port=None,
smtp_user=None,
smtp_password=None,
smtp_encryption=None,
smtp_debug=False,
smtp_session=None,
):
"""Override the standard method to fix the issue of using a mail
client where relaying is disallowed."""
# Use the default bounce address **only if** no Return-Path was
# provided by caller. Caller may be using Variable Envelope Return
# Path (VERP) to detect no-longer valid email addresses.
smtp_from = (
message["Return-Path"]
or self._get_default_bounce_address()
or message["From"]
)
assert (
smtp_from
), "The Return-Path or From header is required for any outbound email"

# The email's "Envelope From" (Return-Path), and all recipient
# addresses must only contain ASCII characters.
from_rfc2822 = extract_rfc2822_addresses(smtp_from)
assert from_rfc2822, (
"Malformed 'Return-Path' or 'From' address: "
"%r - "
"It should contain one valid plain ASCII "
"email"
) % smtp_from
# use last extracted email, to support rarities like 'Support@MyComp
# <support@mycompany.com>'
smtp_from = from_rfc2822[-1]
email_to = message["To"]
email_cc = message["Cc"]
email_bcc = message["Bcc"]
del message["Bcc"]

smtp_to_list = [
address
for base in [email_to, email_cc, email_bcc]
for address in extract_rfc2822_addresses(base)
if address
]
assert smtp_to_list, self.NO_VALID_RECIPIENT

x_forge_to = message["X-Forge-To"]
if x_forge_to:
# `To:` header forged, e.g. for posting on mail.channels,
# to avoid confusion
del message["X-Forge-To"]
del message["To"] # avoid multiple To: headers!
message["To"] = x_forge_to

# Do not actually send emails in testing mode!
if (
getattr(threading.currentThread(), "testing", False)
or self.env.registry.in_test_mode()
):
_test_logger.info("skip sending email in test mode")
return message["Message-Id"]

try:
message_id = message["Message-Id"]

# START OF CODE ADDED
smtp = self.connect(
smtp_server,
smtp_port,
smtp_user,
smtp_password,
smtp_encryption or False,
smtp_debug,
)

from email.utils import parseaddr, formataddr

# exact name and address
(oldname, oldemail) = parseaddr(message["From"])
# use original name with new address
newfrom = formataddr((oldname, smtp.user))
# need to use replace_header instead '=' to prevent
# double field
message.replace_header("From", newfrom)
smtp.sendmail(smtp.user, smtp_to_list, message.as_string())
# END OF CODE ADDED

# do not quit() a pre-established smtp_session
if not smtp_session:
smtp.quit()
except smtplib.SMTPServerDisconnected:
raise
except Exception as e:
params = (ustr(smtp_server), e.__class__.__name__, ustr(e))
msg = _("Mail delivery failed via SMTP server '%s'.\n%s: %s") % params
_logger.info(msg)
raise MailDeliveryException(_("Mail Delivery Failed"), msg)
return message_id
1 change: 1 addition & 0 deletions mail_server_relay_disallowed/readme/CONTRIBUTORS.rst
@@ -0,0 +1 @@
* ForgeFlow <http://www.forgeflow.com>
3 changes: 3 additions & 0 deletions mail_server_relay_disallowed/readme/DESCRIPTION.rst
@@ -0,0 +1,3 @@
Fixes the issue of using a mail client where relaying is disallowed.
See:
https://www.odoo.com/es_ES/forum/help-1/question/zoho-com-connection-unexpectedly-closed-51162
Empty file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8497641

Please sign in to comment.