Skip to content

Commit

Permalink
[FIX] replacement method was wrong if the catchall bounce email addre…
Browse files Browse the repository at this point in the history
…ss contained a dash
  • Loading branch information
Olivier-LAURENT committed Jul 31, 2015
1 parent cdccf9c commit 13479bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
5 changes: 3 additions & 2 deletions email_separator/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Uses plus sign instead of dash as technical email separator to build bounce return paths
========================================================================================

This module was written to force the mass mailing module to build "Return-Path" header with a plus sign as technical email separator instead of a dash.
This module was written to force the mass mailing module to build ```Return-Path``` header with a plus sign as technical email separator instead of a dash.

This transformation is mandatory for mails servers that treat the dash as a valid email account character (as in willem-alexander@koninkrijk .nl) instead of a technical escape character allowing to add optional parameters to the real email account (as in catchall.bounces-543-kremlin@vladimir-putin.ru).
This transformation is mandatory for mails servers that treat the dash as a valid email account character (as in ```willem-alexander@koninkrijk.nl```) instead of a technical escape character allowing to add optional parameters to the real email account (as in ```catchall-bounces-543-kremlin@vladimir-putin.ru```).

Credits
=======
Expand All @@ -14,6 +14,7 @@ Contributors
------------

* Jonathan Nemry (<jonathan.nemry@acsone.eu>)
* Olivier Laurent (<olivier.laurent@acsone.eu>)

Maintainer
----------
Expand Down
12 changes: 7 additions & 5 deletions email_separator/ir_mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def send_email(
cr, uid, 'mail.bounce.alias', context=context)
catchall_domain = self.pool['ir.config_parameter'].get_param(
cr, uid, 'mail.catchall.domain', context=context)
if bounce_alias and catchall_domain and 'Return-Path' in message:
if '-' in message['Return-Path'].split('@', 1)[0]:
rpath = message['Return-Path']
if bounce_alias and catchall_domain and rpath:
if '%s-' % bounce_alias in rpath.split('@', 1)[0]:
# Replace the first dash occurrence in the "identity" part of
# the return path (i.e. the bounce email address)
# by a plus sign, e.g.:
# catchall.bounces-543-kremlin@vladimir-putin.ru
# => catchall.bounces+543-kremlin@vladimir-putin.ru
rpath = message['Return-Path'].replace('-', '+', 1)
# catchall-bounces-543-kremlin@vladimir-putin.ru
# => catchall-bounces+543-kremlin@vladimir-putin.ru
rpath = rpath.replace(
'%s-' % bounce_alias, '%s+' % bounce_alias, 1)
del message['Return-Path']
message['Return-Path'] = rpath

Expand Down
9 changes: 6 additions & 3 deletions email_separator/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ def message_route_check_bounce(self, cr, uid, message, context=None):
email_to = decode_header(message, 'To')

if bounce_alias in email_to:
# Is a bounce ? Reuse the same technic as in super method in
# mass_mailing/models/mail_thread.py
bounce_re = re.compile(
r'%s\+(\d+)-?([\w.]+)?-?(\d+)?' % re.escape(bounce_alias),
re.UNICODE)
if bounce_re.search(email_to):
# Replace the first occurrence of a plus sign in the
# recipient address by a dash, e.g.:
# catchall.bounces+543-kremlin@vladimir-putin.ru
# => catchall.bounces-543-kremlin@vladimir-putin.ru
# catchall-bounces+543-kremlin@vladimir-putin.ru
# => catchall-bounces-543-kremlin@vladimir-putin.ru
del message['To']
message['To'] = email_to.replace('+', '-', 1)
message['To'] = email_to.replace(
'%s+' % bounce_alias, '%s-' % bounce_alias, 1)

return super(MailThread, self).message_route_check_bounce(
cr, uid, message, context=context)

0 comments on commit 13479bf

Please sign in to comment.