From 2535a521779d5803e9cbb89941d61a919e45ccdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 8 Oct 2019 15:16:45 +0000 Subject: [PATCH] [IMP] mail, portal: use formataddr to format email Somehow followup of dbb6cfdbf803d2ab86ba11450b5928abf5ab658b . Purpose is to use formataddr from tools instead of manually handcrafting the full email address format. Indeed the tools one correctly encapsulates and format name and email. Task ID 2068986 --- addons/crm/models/crm_lead.py | 2 +- addons/hr_recruitment/models/hr_recruitment.py | 4 ++-- addons/mail/models/mail_message.py | 1 - addons/mail/models/mail_thread.py | 4 ++-- addons/portal/wizard/portal_wizard.py | 2 +- addons/test_mail/tests/test_mail_gateway.py | 8 ++++---- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 546c52452dd..7613c33969c 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1287,7 +1287,7 @@ class Lead(models.Model): emails = email_re.findall(partner_info['full_name'] or '') email = emails and emails[0] or '' if email and self.email_from and email.lower() == self.email_from.lower(): - partner_info['full_name'] = '%s <%s>' % (self.contact_name or self.partner_name, email) + partner_info['full_name'] = tools.formataddr((self.contact_name or self.partner_name, email)) break return result diff --git a/addons/hr_recruitment/models/hr_recruitment.py b/addons/hr_recruitment/models/hr_recruitment.py index 30bb75b4f4a..cc3838b51ff 100644 --- a/addons/hr_recruitment/models/hr_recruitment.py +++ b/addons/hr_recruitment/models/hr_recruitment.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models, SUPERUSER_ID +from odoo import api, fields, models, tools, SUPERUSER_ID from odoo.tools.translate import _ from odoo.exceptions import UserError @@ -397,7 +397,7 @@ class Applicant(models.Model): elif applicant.email_from: email_from = applicant.email_from if applicant.partner_name: - email_from = '%s<%s>' % (applicant.partner_name, email_from) + email_from = tools.formataddr((applicant.partner_name, email_from)) applicant._message_add_suggested_recipient(recipients, email=email_from, reason=_('Contact Email')) return recipients diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 24819054f2e..60cbf956620 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -7,7 +7,6 @@ import re from binascii import Error as binascii_error from collections import defaultdict from operator import itemgetter -from email.utils import formataddr from odoo.http import request from odoo import _, api, fields, models, modules, tools diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 04108614808..260fbfc9c24 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -679,7 +679,7 @@ class MailThread(models.AbstractModel): } bounce_from = self.env['ir.mail_server']._get_default_bounce_address() if bounce_from: - bounce_mail_values['email_from'] = 'MAILER-DAEMON <%s>' % bounce_from + bounce_mail_values['email_from'] = tools.formataddr(('MAILER-DAEMON', bounce_from)) bounce_mail_values.update(mail_values) self.env['mail.mail'].create(bounce_mail_values).send() @@ -1483,7 +1483,7 @@ class MailThread(models.AbstractModel): if partner and partner.id in [val[0] for val in result[self.ids[0]]]: # already existing partner ID -> skip return result if partner and partner.email: # complete profile: id, name - result[self.ids[0]].append((partner.id, '%s<%s>' % (partner.name, partner.email), reason)) + result[self.ids[0]].append((partner.id, tools.formataddr((partner.name, partner.email)), reason)) elif partner: # incomplete profile: id, name result[self.ids[0]].append((partner.id, '%s' % (partner.name), reason)) else: # unknown partner, we are probably managing an email address diff --git a/addons/portal/wizard/portal_wizard.py b/addons/portal/wizard/portal_wizard.py index 86aa84115bd..306a3f9ad9b 100644 --- a/addons/portal/wizard/portal_wizard.py +++ b/addons/portal/wizard/portal_wizard.py @@ -99,7 +99,7 @@ class PortalWizardUser(models.TransientModel): '\n- '.join(partners_error_emails.mapped('email')))) if partners_error_user: error_msg.append("%s\n- %s" % (_("Some contacts have the same email as an existing portal user:"), - '\n- '.join(['%s <%s>' % (p.display_name, p.email) for p in partners_error_user]))) + '\n- '.join([p.email_formatted for p in partners_error_user]))) if error_msg: error_msg.append(_("To resolve this error, you can: \n" "- Correct the emails of the relevant contacts\n" diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index 440085af206..da27c2b4b18 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -271,7 +271,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): # Test bounce email self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON ') + self.assertEqual(self._mails[0].get('email_from'), '"MAILER-DAEMON" ') @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models.unlink', 'odoo.addons.mail.models.mail_mail') def test_message_process_alias_followers_bounce(self): @@ -289,7 +289,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): 'message_process: incoming email on Followers alias should send a bounce email') self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON ') + self.assertEqual(self._mails[0].get('email_from'), '"MAILER-DAEMON" ') # Test: partner on followers alias -> bounce self._init_mock_build_email() @@ -299,7 +299,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): 'message_process: incoming email on Followers alias should send a bounce email') self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON ') + self.assertEqual(self._mails[0].get('email_from'), '"MAILER-DAEMON" ') @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models') def test_message_process_alias_partner(self): @@ -423,7 +423,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): # Test bounce email self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON ') + self.assertEqual(self._mails[0].get('email_from'), '"MAILER-DAEMON" ') @mute_logger('odoo.addons.mail.models.mail_thread') def test_message_process_bounce_alias(self):