changeset 1846:349a0ca30a75 beta

Changed default recipients separator for mails to ', '
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 07 Jan 2012 23:55:24 +0200
parents a048d0c6da3c
children c3237c468544 3f5be4dbbd57
files rhodecode/lib/rcmail/exceptions.py rhodecode/lib/rcmail/message.py rhodecode/lib/rcmail/response.py rhodecode/lib/rcmail/smtp_mailer.py
diffstat 4 files changed, 28 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/rcmail/exceptions.py	Sat Jan 07 22:22:16 2012 +0200
+++ b/rhodecode/lib/rcmail/exceptions.py	Sat Jan 07 23:55:24 2012 +0200
@@ -1,3 +1,4 @@
+
 
 class InvalidMessage(RuntimeError):
     """
@@ -5,6 +6,7 @@
     as recipients or sender address.
     """
 
+
 class BadHeaders(RuntimeError):
     """
     Raised if message contains newlines in headers.
--- a/rhodecode/lib/rcmail/message.py	Sat Jan 07 22:22:16 2012 +0200
+++ b/rhodecode/lib/rcmail/message.py	Sat Jan 07 23:55:24 2012 +0200
@@ -45,6 +45,8 @@
     :param bcc: BCC list
     :param extra_headers: dict of extra email headers
     :param attachments: list of Attachment instances
+    :param recipients_separator: alternative separator for any of
+        'From', 'To', 'Delivered-To', 'Cc', 'Bcc' fields
     """
 
     def __init__(self,
@@ -56,8 +58,8 @@
                  cc=None,
                  bcc=None,
                  extra_headers=None,
-                 attachments=None):
-
+                 attachments=None,
+                 recipients_separator="; "):
 
         self.subject = subject or ''
         self.sender = sender
@@ -70,6 +72,8 @@
         self.bcc = bcc or []
         self.extra_headers = extra_headers or {}
 
+        self.recipients_separator = recipients_separator
+
     @property
     def send_to(self):
         return set(self.recipients) | set(self.bcc or ()) | set(self.cc or ())
@@ -92,7 +96,8 @@
                                 To=self.recipients,
                                 From=self.sender,
                                 Body=self.body,
-                                Html=self.html)
+                                Html=self.html,
+                                separator=self.recipients_separator)
 
         if self.bcc:
             response.base['Bcc'] = self.bcc
--- a/rhodecode/lib/rcmail/response.py	Sat Jan 07 22:22:16 2012 +0200
+++ b/rhodecode/lib/rcmail/response.py	Sat Jan 07 23:55:24 2012 +0200
@@ -141,12 +141,14 @@
     MailResponse.to_message.  This lets you change it and work with it, then
     send it out when it's ready.
     """
-    def __init__(self, To=None, From=None, Subject=None, Body=None, Html=None):
+    def __init__(self, To=None, From=None, Subject=None, Body=None, Html=None, 
+                 separator="; "):
         self.Body = Body
         self.Html = Html
         self.base = MailBase([('To', To), ('From', From), ('Subject', Subject)])
         self.multipart = self.Body and self.Html
         self.attachments = []
+        self.separator = separator
 
     def __contains__(self, key):
         return self.base.__contains__(key)
@@ -298,7 +300,7 @@
             self.base.body = self.Html
             self.base.content_encoding['Content-Type'] = ('text/html', {})
 
-        return to_message(self.base)
+        return to_message(self.base, separator=self.separator)
 
     def all_parts(self):
         """
@@ -310,7 +312,7 @@
     def keys(self):
         return self.base.keys()
 
-def to_message(mail):
+def to_message(mail, separator="; "):
     """
     Given a MailBase message, this will construct a MIMEPart
     that is canonicalized for use with the Python email API.
@@ -339,10 +341,16 @@
 
     for k in mail.keys():
         if k in ADDRESS_HEADERS_WHITELIST:
-            out[k.encode('ascii')] = header_to_mime_encoding(mail[k])
+            out[k.encode('ascii')] = header_to_mime_encoding(
+                                         mail[k],
+                                         not_email=False,
+                                         separator=separator
+                                     )
         else:
-            out[k.encode('ascii')] = header_to_mime_encoding(mail[k],
-                                                             not_email=True)
+            out[k.encode('ascii')] = header_to_mime_encoding(
+                                         mail[k],
+                                         not_email=True
+                                    )
 
     out.extract_payload(mail)
 
@@ -403,12 +411,12 @@
             self.is_multipart())
 
 
-def header_to_mime_encoding(value, not_email=False):
+def header_to_mime_encoding(value, not_email=False, separator=", "):
     if not value: return ""
 
     encoder = Charset(DEFAULT_ENCODING)
     if type(value) == list:
-        return "; ".join(properly_encode_header(
+        return separator.join(properly_encode_header(
             v, encoder, not_email) for v in value)
     else:
         return properly_encode_header(value, encoder, not_email)
--- a/rhodecode/lib/rcmail/smtp_mailer.py	Sat Jan 07 22:22:16 2012 +0200
+++ b/rhodecode/lib/rcmail/smtp_mailer.py	Sat Jan 07 23:55:24 2012 +0200
@@ -59,7 +59,8 @@
 
         if isinstance(recipients, basestring):
             recipients = [recipients]
-        msg = Message(subject, recipients, body, html, self.mail_from)
+        msg = Message(subject, recipients, body, html, self.mail_from,
+                      recipients_separator=", ")
         raw_msg = msg.to_message()
 
         if self.ssl: