[FIX] mail: to/from/reply-to addresses formated according to RFC2822

Use formataddr method from email.utils lib, which do the job correctly
This commit is contained in:
Denis Ledoux 2014-08-12 13:40:45 +02:00
parent 2abddd5311
commit 245bb4ebdf
4 changed files with 14 additions and 13 deletions

View File

@ -231,7 +231,7 @@ class test_message_compose(TestMailBase):
# force send: take email_recipients into account # force send: take email_recipients into account
email_template.send_mail(cr, uid, email_template_id, self.group_pigs_id, force_send=True, context=context) email_template.send_mail(cr, uid, email_template_id, self.group_pigs_id, force_send=True, context=context)
sent_emails = self._build_email_kwargs_list sent_emails = self._build_email_kwargs_list
email_to_lst = ['"Followers of Pigs" <admin@example.com>', '"Followers of Pigs" <raoul@raoul.fr>', '"Followers of Pigs" <bert@bert.fr>'] email_to_lst = ['Followers of Pigs <admin@example.com>', 'Followers of Pigs <raoul@raoul.fr>', 'Followers of Pigs <bert@bert.fr>']
self.assertEqual(len(sent_emails), 3, 'email_template: send_mail: 3 valid email recipients -> should send 3 emails') self.assertEqual(len(sent_emails), 3, 'email_template: send_mail: 3 valid email recipients -> should send 3 emails')
for email in sent_emails: for email in sent_emails:
self.assertEqual(len(email['email_to']), 1, 'email_template: send_mail: email_recipient should send email to one recipient at a time') self.assertEqual(len(email['email_to']), 1, 'email_template: send_mail: email_recipient should send email to one recipient at a time')

View File

@ -18,6 +18,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/> # along with this program. If not, see <http://www.gnu.org/licenses/>
# #
############################################################################## ##############################################################################
from email.utils import formataddr
from openerp.osv import osv, fields from openerp.osv import osv, fields
from openerp import tools, SUPERUSER_ID from openerp import tools, SUPERUSER_ID
@ -155,9 +157,9 @@ class mail_notification(osv.Model):
# email_from: partner-user alias or partner email or mail.message email_from # email_from: partner-user alias or partner email or mail.message email_from
if msg.author_id and msg.author_id.user_ids and msg.author_id.user_ids[0].alias_domain and msg.author_id.user_ids[0].alias_name: if msg.author_id and msg.author_id.user_ids and msg.author_id.user_ids[0].alias_domain and msg.author_id.user_ids[0].alias_name:
email_from = '%s <%s@%s>' % (msg.author_id.name, msg.author_id.user_ids[0].alias_name, msg.author_id.user_ids[0].alias_domain) email_from = formataddr((msg.author_id.name, '%s@%s' % (msg.author_id.user_ids[0].alias_name, msg.author_id.user_ids[0].alias_domain)))
elif msg.author_id: elif msg.author_id:
email_from = '%s <%s>' % (msg.author_id.name, msg.author_id.email) email_from = formataddr((msg.author_id.name, msg.author_id.email))
else: else:
email_from = msg.email_from email_from = msg.email_from

View File

@ -22,6 +22,7 @@
import base64 import base64
import logging import logging
import re import re
from email.utils import formataddr
from urllib import urlencode from urllib import urlencode
from urlparse import urljoin from urlparse import urljoin
@ -216,10 +217,9 @@ class mail_mail(osv.Model):
if email_reply_to and mail.model and mail.res_id: if email_reply_to and mail.model and mail.res_id:
document_name = self.pool.get(mail.model).name_get(cr, SUPERUSER_ID, [mail.res_id], context=context)[0] document_name = self.pool.get(mail.model).name_get(cr, SUPERUSER_ID, [mail.res_id], context=context)[0]
if document_name: if document_name:
# sanitize document name
sanitized_doc_name = re.sub(r'[^\w+.]+', '-', document_name[1])
# generate reply to # generate reply to
email_reply_to = _('"Followers of %s" <%s>') % (sanitized_doc_name, email_reply_to)
email_reply_to = formataddr((_('Followers of %s') % document_name[1], email_reply_to))
return email_reply_to return email_reply_to
@ -240,10 +240,9 @@ class mail_mail(osv.Model):
# 2. if 'partner' is specified, but no related document: Partner Name <email> # 2. if 'partner' is specified, but no related document: Partner Name <email>
# 3; fallback on mail.email_to that we split to have an email addresses list # 3; fallback on mail.email_to that we split to have an email addresses list
if partner and mail.record_name: if partner and mail.record_name:
sanitized_record_name = re.sub(r'[^\w+.]+', '-', mail.record_name) email_to = [formataddr((_('Followers of %s') % mail.record_name, partner.email))]
email_to = [_('"Followers of %s" <%s>') % (sanitized_record_name, partner.email)]
elif partner: elif partner:
email_to = ['%s <%s>' % (partner.name, partner.email)] email_to = [formataddr((partner.name, partner.email))]
else: else:
email_to = tools.email_split(mail.email_to) email_to = tools.email_split(mail.email_to)

View File

@ -336,7 +336,7 @@ class test_mail(TestMailBase):
# Test: notifications emails: to a and b, c is email only, r is author # Test: notifications emails: to a and b, c is email only, r is author
# test_emailto = ['Administrator <a@a>', 'Bert Tartopoils <b@b>'] # test_emailto = ['Administrator <a@a>', 'Bert Tartopoils <b@b>']
test_emailto = ['"Followers of -Pigs-" <a@a>', '"Followers of -Pigs-" <b@b>'] test_emailto = [u'"Followers of \\"Pigs\\" !\xf9 $%-" <a@a>', u'"Followers of \\"Pigs\\" !\xf9 $%-" <b@b>']
self.assertEqual(len(sent_emails), 2, self.assertEqual(len(sent_emails), 2,
'message_post: notification emails wrong number of send emails') 'message_post: notification emails wrong number of send emails')
self.assertEqual(set([m['email_to'][0] for m in sent_emails]), set(test_emailto), self.assertEqual(set([m['email_to'][0] for m in sent_emails]), set(test_emailto),
@ -348,7 +348,7 @@ class test_mail(TestMailBase):
'message_post: notification email sent to more than one email address instead of a precise partner') 'message_post: notification email sent to more than one email address instead of a precise partner')
self.assertIn(sent_email['email_to'][0], test_emailto, self.assertIn(sent_email['email_to'][0], test_emailto,
'message_post: notification email email_to incorrect') 'message_post: notification email email_to incorrect')
self.assertEqual(sent_email['reply_to'], '"Followers of -Pigs-" <group+pigs@schlouby.fr>', self.assertEqual(sent_email['reply_to'], u'"Followers of \\"Pigs\\" !\xf9 $%-" <group+pigs@schlouby.fr>',
'message_post: notification email reply_to incorrect') 'message_post: notification email reply_to incorrect')
self.assertEqual(_subject, sent_email['subject'], self.assertEqual(_subject, sent_email['subject'],
'message_post: notification email subject incorrect') 'message_post: notification email subject incorrect')
@ -410,7 +410,7 @@ class test_mail(TestMailBase):
# Test: emails send by server (to a, b, c, d) # Test: emails send by server (to a, b, c, d)
# test_emailto = [u'Administrator <a@a>', u'Bert Tartopoils <b@b>', u'Carine Poilvache <c@c>', u'D\xe9d\xe9 Grosbedon <d@d>'] # test_emailto = [u'Administrator <a@a>', u'Bert Tartopoils <b@b>', u'Carine Poilvache <c@c>', u'D\xe9d\xe9 Grosbedon <d@d>']
test_emailto = [u'"Followers of Pigs" <a@a>', u'"Followers of Pigs" <b@b>', u'"Followers of Pigs" <c@c>', u'"Followers of Pigs" <d@d>'] test_emailto = [u'Followers of Pigs <a@a>', u'Followers of Pigs <b@b>', u'Followers of Pigs <c@c>', u'Followers of Pigs <d@d>']
# self.assertEqual(len(sent_emails), 3, 'sent_email number of sent emails incorrect') # self.assertEqual(len(sent_emails), 3, 'sent_email number of sent emails incorrect')
for sent_email in sent_emails: for sent_email in sent_emails:
self.assertEqual(sent_email['email_from'], 'Raoul Grosbedon <r@r>', self.assertEqual(sent_email['email_from'], 'Raoul Grosbedon <r@r>',
@ -419,7 +419,7 @@ class test_mail(TestMailBase):
'message_post: notification email sent to more than one email address instead of a precise partner') 'message_post: notification email sent to more than one email address instead of a precise partner')
self.assertIn(sent_email['email_to'][0], test_emailto, self.assertIn(sent_email['email_to'][0], test_emailto,
'message_post: notification email email_to incorrect') 'message_post: notification email email_to incorrect')
self.assertEqual(sent_email['reply_to'], '"Followers of Pigs" <r@r>', self.assertEqual(sent_email['reply_to'], 'Followers of Pigs <r@r>',
'message_post: notification email reply_to incorrect: should name Followers of Pigs, and have raoul email') 'message_post: notification email reply_to incorrect: should name Followers of Pigs, and have raoul email')
self.assertEqual(_mail_subject, sent_email['subject'], self.assertEqual(_mail_subject, sent_email['subject'],
'message_post: notification email subject incorrect') 'message_post: notification email subject incorrect')