From 5cc2404889e03a3560f7cc5daab453560f4d8ef3 Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Thu, 19 Nov 2020 10:17:50 +0000 Subject: [PATCH] [FIX] ir_mail_server: Use IDNA for smtp_user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is a user whose email login is "joe@examplé.com", notice the latin "é" in the domain. This domain is a valid IDNA-2008 domain but the smtplib of python is incompatible with such domain and raises a UnicodeError because the character is not ascii. Note the removed comment about bytestring is a leftover of a dark python2 age and is no more valid. Closes #61972 closes odoo/odoo#62026 X-original-commit: 89a1989bd271f49a9222d5c255e03d4877045621 Signed-off-by: Raphael Collet (rco) Signed-off-by: Julien Castiaux --- odoo/addons/base/models/ir_mail_server.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/odoo/addons/base/models/ir_mail_server.py b/odoo/addons/base/models/ir_mail_server.py index a1a402e54fc..fd4d3290053 100644 --- a/odoo/addons/base/models/ir_mail_server.py +++ b/odoo/addons/base/models/ir_mail_server.py @@ -15,6 +15,7 @@ import sys import threading import html2text +import idna from odoo import api, fields, models, tools, _ from odoo.exceptions import UserError @@ -131,7 +132,7 @@ class IrMailServer(models.Model): except UserError as e: # let UserErrors (messages) bubble up raise e - except UnicodeError as e: + except (UnicodeError, idna.core.InvalidCodepoint) as e: raise UserError(_("Invalid server name !\n %s", ustr(e))) except (gaierror, timeout) as e: raise UserError(_("No response received. Check server address and port number.\n %s", ustr(e))) @@ -233,12 +234,9 @@ class IrMailServer(models.Model): if smtp_user: # Attempt authentication - will raise if AUTH service not supported - # The user/password must be converted to bytestrings in order to be usable for - # certain hashing schemes, like HMAC. - # See also bug #597143 and python issue #5285 - smtp_user = pycompat.to_text(ustr(smtp_user)) - smtp_password = pycompat.to_text(ustr(smtp_password)) - connection.login(smtp_user, smtp_password) + local, at, domain = smtp_user.rpartition('@') + domain = idna.encode(domain).decode('ascii') + connection.login(f"{local}{at}{domain}", smtp_password or '') # Some methods of SMTP don't check whether EHLO/HELO was sent. # Anyway, as it may have been sent by login(), all subsequent usages should consider this command as sent.