[FIX] tools, mail: do not strip name from email_to

When sending a mail.mail with email_to, the processing split the email_to into
a list of addresses. However if the found addresses use the form name <email>
the name if lost in the process. A new email_split_and_format method is introduced
in tools and used to avoid loosing that information.
This commit is contained in:
Thibault Delavallée 2015-12-10 17:31:01 +01:00
parent d4a1eb4435
commit 3f758e2fab
4 changed files with 19 additions and 5 deletions

View File

@ -198,7 +198,7 @@ class mail_mail(osv.Model):
if partner:
email_to = [formataddr((partner.name, partner.email))]
else:
email_to = tools.email_split(mail.email_to)
email_to = tools.email_split_and_format(mail.email_to)
return email_to
def send_get_email_dict(self, cr, uid, mail, partner=None, context=None):

View File

@ -19,6 +19,8 @@
#
##############################################################################
from email.utils import formataddr
from .common import TestMail
from openerp.tools import mute_logger
import socket
@ -298,7 +300,7 @@ class TestMailgateway(TestMail):
'message_process: incoming email on Partners alias should send a bounce email')
self.assertIn('Frogs', sent_emails[0].get('subject'),
'message_process: bounce email on Partners alias should contain the original subject')
self.assertIn('test.sylvie.lelitre@agrolait.com', sent_emails[0].get('email_to'),
self.assertIn(formataddr(('Sylvie Lelitre', 'test.sylvie.lelitre@agrolait.com')), sent_emails[0].get('email_to'),
'message_process: bounce email on Partners alias should have original email sender as recipient')
# Do: incoming email from an unknown partner on a Followers only alias -> bounce
@ -313,7 +315,7 @@ class TestMailgateway(TestMail):
'message_process: incoming email on Followers alias should send a bounce email')
self.assertIn('Frogs', sent_emails[0].get('subject'),
'message_process: bounce email on Followers alias should contain the original subject')
self.assertIn('test.sylvie.lelitre@agrolait.com', sent_emails[0].get('email_to'),
self.assertIn(formataddr(('Sylvie Lelitre', 'test.sylvie.lelitre@agrolait.com')), sent_emails[0].get('email_to'),
'message_process: bounce email on Followers alias should have original email sender as recipient')
# Do: incoming email from a known partner on a Partners alias -> ok (+ test on alias.user_id)

View File

@ -103,7 +103,7 @@ class TestMailGroup(TestMail):
for email in sent_emails:
self.assertEqual(
set(email['email_to']),
set([self.user_raoul.email, self.user_bert.email]))
set([formataddr((self.user_raoul.name, self.user_raoul.email)), formataddr((self.user_bert.name, self.user_bert.email))]))
def test_mail_group_notification_recipients_separated(self):
# Remove alias, should trigger classic behavior of mail group

View File

@ -29,7 +29,7 @@ import re
import socket
import threading
import time
from email.utils import getaddresses
from email.utils import getaddresses, formataddr
import openerp
from openerp.loglevels import ustr
@ -680,3 +680,15 @@ def email_split(text):
# is strictly required in RFC2822's `addr-spec`.
if addr[1]
if '@' in addr[1]]
def email_split_and_format(text):
""" Return a list of email addresses found in ``text``, formatted using
formataddr. """
if not text:
return []
return [formataddr((addr[0], addr[1])) for addr in getaddresses([text])
# getaddresses() returns '' when email parsing fails, and
# sometimes returns emails without at least '@'. The '@'
# is strictly required in RFC2822's `addr-spec`.
if addr[1]
if '@' in addr[1]]