Skip to content

Commit

Permalink
Add emailtunnel.abbreviate_recipient_list
Browse files Browse the repository at this point in the history
  • Loading branch information
Mortal committed May 1, 2015
1 parent 648d3e6 commit ae039b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
17 changes: 17 additions & 0 deletions emailtunnel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import sys
import logging
import datetime
import itertools

import email
import email.mime.multipart
Expand All @@ -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)
Expand Down
15 changes: 2 additions & 13 deletions tkmail/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import email.header

import emailtunnel
from emailtunnel import SMTPForwarder, Message

import tkmail.address
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ae039b1

Please sign in to comment.