[FIX] mail: fix bounce email addressing

- The "From" should not be the administrator user but
  the postmaster/system, and should be an address that
  ignores double-bounces. Using the default bounce
  email accomplishes both.

- The "To" recipient should be the "envelope from"
  sender when available, usually as provided by
  the "Return-Path" header set by the upstream MTA.
  If not present, fall back to the "From" of
  the original email.

- adapt tests to verify the behavior wrt Return-Path
This commit is contained in:
Olivier Dony
2016-04-21 17:26:45 +02:00
parent 3f43d3056b
commit 74d8cbc190
2 changed files with 10 additions and 5 deletions
+8 -3
View File
@@ -798,15 +798,20 @@ class MailThread(models.AbstractModel):
record_set = None
def _create_bounce_email():
self.env['mail.mail'].create({
bounce_to = decode_header(message, 'Return-Path') or email_from
bounce_mail_values = {
'body_html': '<div><p>Hello,</p>'
'<p>The following email sent to %s cannot be accepted because this is '
'a private email address. Only allowed people can contact us at this address.</p></div>'
'<blockquote>%s</blockquote>' % (message.get('to'), message_dict.get('body')),
'subject': 'Re: %s' % message.get('subject'),
'email_to': message.get('from'),
'email_to': bounce_to,
'auto_delete': True,
}).send()
}
bounce_from = self.env['ir.mail_server']._get_default_bounce_address()
if bounce_from:
bounce_mail_values['email_from'] = 'MAILER-DAEMON <%s>' % bounce_from
self.env['mail.mail'].create(bounce_mail_values).send()
def _warn(message):
_logger.info('Routing mail with Message-Id %s: route %s: %s',
+2 -2
View File
@@ -313,8 +313,8 @@ class TestMailgateway(TestMail):
'message_process: incoming email on Partners alias should send a bounce email')
self.assertIn('New Frogs', self._mails[0].get('subject'),
'message_process: bounce email on Partners alias should contain the original subject')
self.assertIn(formataddr(('Sylvie Lelitre', 'test.sylvie.lelitre@agrolait.com')), self._mails[0].get('email_to'),
'message_process: bounce email on Partners alias should have original email sender as recipient')
self.assertIn('whatever-2a840@postmaster.twitter.com', self._mails[0].get('email_to'),
'message_process: bounce email on Partners alias should go to Return-Path address')
@mute_logger('openerp.addons.mail.models.mail_thread', 'openerp.models', 'openerp.addons.mail.models.mail_mail')
def test_message_process_alias_followers_bounce(self):