diff --git a/emailtunnel/__init__.py b/emailtunnel/__init__.py index b713772..a742b65 100644 --- a/emailtunnel/__init__.py +++ b/emailtunnel/__init__.py @@ -28,6 +28,7 @@ import sys import logging import datetime +import itertools import email import email.mime.multipart @@ -42,6 +43,22 @@ import smtplib +def abbreviate_recipient_list(recipients): + if all('@' in rcpt for rcpt in recipients): + parts = [rcpt.split('@', 1) for rcpt in recipients] + parts.sort(key=lambda x: (x[1].lower(), x[0].lower())) + by_domain = [ + (domain, [a[0] for a in aa]) + for domain, aa in itertools.groupby( + parts, key=lambda x: x[1]) + ] + return ', '.join( + '<%s@%s>' % (','.join(aa), domain) + for domain, aa in by_domain) + else: + return ', '.join('<%s>' % x for x in recipients) + + def _fix_eols(data): if isinstance(data, six.string_types): return re.sub(r'(?:\r\n|\n|\r)', "\r\n", data) diff --git a/tkmail/server.py b/tkmail/server.py index 12962e3..b067d1b 100644 --- a/tkmail/server.py +++ b/tkmail/server.py @@ -10,6 +10,7 @@ import email.header +import emailtunnel from emailtunnel import SMTPForwarder, Message import tkmail.address @@ -90,19 +91,7 @@ def log_receipt(self, peer, envelope): % (str(message.subject), sender, recipients)) def log_delivery(self, message, recipients, sender): - if all('@' in rcpt for rcpt in recipients): - parts = [rcpt.split('@', 1) for rcpt in recipients] - parts.sort(key=lambda x: (x[1].lower(), x[0].lower())) - by_domain = [ - (domain, [a[0] for a in aa]) - for domain, aa in itertools.groupby( - parts, key=lambda x: x[1]) - ] - recipients_string = ', '.join( - '<%s@%s>' % (','.join(aa), domain) - for domain, aa in by_domain) - else: - recipients_string = ', '.join('<%s>' % x for x in recipients) + recipients_string = emailtunnel.abbreviate_recipient_list(recipients) if len(recipients_string) > 200: age = self.deliver_recipients.get(recipients_string)