From e324e3d87cae19a0be8d5a03aef79bbca073fe82 Mon Sep 17 00:00:00 2001
From: Eygene Ryabinkin <rea@codelabs.ru>
Date: Mon, 15 Sep 2008 21:44:38 +0400
Subject: [PATCH] mail/sendmail.py: new option 'expand_to' for sendmail() routine

This option, when set to True, instructs sendmail() to put the
actual recipient list to the To: field of the sent mail messages.
Default behaviour was to put sender's e-mail and it is preserved
for the compatibility.

The need for this modification is simple: modern spam filters,
mailing lists and mail clients are very unhappy when message has
implicit destination.  For example, Mailman software holds such
messages for moderator approval even if the envelope sender is
whitelisted.

Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
---
 MoinMoin/config/multiconfig.py |    1 +
 MoinMoin/events/emailnotify.py |   13 ++++++++-----
 MoinMoin/mail/sendmail.py      |    9 +++++++--
 MoinMoin/user.py               |    3 ++-
 wiki/config/wikiconfig.py      |    5 +++++
 5 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/MoinMoin/config/multiconfig.py b/MoinMoin/config/multiconfig.py
index 6e8c6fd..80e27c1 100644
--- a/MoinMoin/config/multiconfig.py
+++ b/MoinMoin/config/multiconfig.py
@@ -374,6 +374,7 @@ Lists: * bullets; 1., a. numbered items.
     log_timing = False # log infos about timing of actions, good to analyze load conditions
 
     mail_from = None # u'Juergen Wiki <noreply@jhwiki.org>'
+    mail_expand_to = False # set to True if you want all recipients to be listed in To: field
     mail_login = None # "user pwd" if you need to use SMTP AUTH when using your mail server
     mail_smarthost = None # your SMTP mail server
     mail_sendmail = None # "/usr/sbin/sendmail -t -i" to not use SMTP, but sendmail
diff --git a/MoinMoin/events/emailnotify.py b/MoinMoin/events/emailnotify.py
index e757300..46925e4 100644
--- a/MoinMoin/events/emailnotify.py
+++ b/MoinMoin/events/emailnotify.py
@@ -54,7 +54,7 @@ def prep_page_changed_mail(request, page, comment, email_lang, revisions, trivia
     return {'subject': subject, 'text': change['text'] + pagelink + change['diff']}
 
 
-def send_notification(request, from_address, emails, data):
+def send_notification(request, from_address, emails, data, expand_to):
     """ Send notification email
 
     @param emails: list of email addresses
@@ -62,7 +62,7 @@ def send_notification(request, from_address, emails, data):
     @rtype int
 
     """
-    return sendmail.sendmail(request, emails, data['subject'], data['text'], mail_from=from_address)
+    return sendmail.sendmail(request, emails, data['subject'], data['text'], mail_from=from_address, expand_to=expand_to)
 
 
 def handle_page_change(event):
@@ -79,6 +79,7 @@ def handle_page_change(event):
     trivial = isinstance(event, ev.TrivialPageChangedEvent)
     subscribers = page.getSubscribers(request, return_users=1)
     mail_from = page.cfg.mail_from
+    expand_to = page.cfg.mail_expand_to
 
     if subscribers:
         recipients = set()
@@ -94,7 +95,7 @@ def handle_page_change(event):
             names = [u.name for u in users]
             data = prep_page_changed_mail(request, page, comment, lang, revisions, trivial)
 
-            if send_notification(request, mail_from, emails, data):
+            if send_notification(request, mail_from, emails, data, expand_to):
                 recipients.update(names)
 
         if recipients:
@@ -107,6 +108,7 @@ def handle_user_created(event):
     request = event.request
     sitename = request.cfg.sitename
     from_address = request.cfg.mail_from
+    expand_to = request.cfg.mail_expand_to
     event_name = event.name
     email = event.user.email or u"NOT SET"
     username = event.user.name
@@ -118,7 +120,7 @@ def handle_user_created(event):
         if usr.isSuperUser() and event_name in usr.email_subscribed_events:
             _ = lambda text: request.getText(text, lang=usr.language or 'en')
             data = notification.user_created_message(request, _, sitename, username, email)
-            send_notification(request, from_address, [usr.email], data)
+            send_notification(request, from_address, [usr.email], dat, expand_toa)
 
 
 def handle_file_attached(event):
@@ -126,6 +128,7 @@ def handle_file_attached(event):
 
     names = set()
     from_address = event.request.cfg.mail_from
+    expand_to = event.request.cfg.mail_expand_to
     request = event.request
     page = Page(request, event.pagename)
 
@@ -151,7 +154,7 @@ def handle_file_attached(event):
 
         emails = [usr.email for usr in subscribers[lang]]
 
-        if send_notification(request, from_address, emails, data):
+        if send_notification(request, from_address, emails, data, expand_to):
             names.update(recipients)
 
     return notification.Success(names)
diff --git a/MoinMoin/mail/sendmail.py b/MoinMoin/mail/sendmail.py
index a3f7a0a..db07541 100644
--- a/MoinMoin/mail/sendmail.py
+++ b/MoinMoin/mail/sendmail.py
@@ -43,7 +43,7 @@ def encodeAddress(address, charset):
         return address.encode(config.charset)
 
 
-def sendmail(request, to, subject, text, mail_from=None):
+def sendmail(request, to, subject, text, mail_from=None, expand_to=False):
     """ Create and send a text/plain message
 
     Return a tuple of success or error indicator and message.
@@ -54,6 +54,8 @@ def sendmail(request, to, subject, text, mail_from=None):
     @param text: email body text (unicode)
     @param mail_from: override default mail_from
     @type mail_from: unicode
+    @keyword expand_to: specify all recipients in the 'To' field
+    @type expand_to: bool
     @rtype: tuple
     @return: (is_ok, Description of error or OK message)
     """
@@ -92,7 +94,10 @@ def sendmail(request, to, subject, text, mail_from=None):
     # use the same mail_from, e.g. u"Jürgen Wiki <noreply@mywiki.org>"
     address = encodeAddress(mail_from, charset)
     msg['From'] = address
-    msg['To'] = address
+    if (expand_to == False):
+        msg['To'] = address
+    else:
+        msg['To'] = ", ".join(encodeAddress(i, charset) for i in to)
     msg['Date'] = formatdate()
     msg['Message-ID'] = make_msgid()
     msg['Subject'] = Header(subject, charset)
diff --git a/MoinMoin/user.py b/MoinMoin/user.py
index 8d9918b..7ea37ee 100644
--- a/MoinMoin/user.py
+++ b/MoinMoin/user.py
@@ -1089,6 +1089,7 @@ recovery token.
         subject = _('[%(sitename)s] Your wiki account data',
                 ) % {'sitename': self._cfg.sitename or "Wiki"}
         mailok, msg = sendmail.sendmail(self._request, [self.email], subject,
-                                    text, mail_from=self._cfg.mail_from)
+                                    text, mail_from=self._cfg.mail_from,
+       	                            expand_to=self._cfg.mail_expand_to)
         return mailok, msg
 
diff --git a/wiki/config/wikiconfig.py b/wiki/config/wikiconfig.py
index 4befe7f..20b90f4 100644
--- a/wiki/config/wikiconfig.py
+++ b/wiki/config/wikiconfig.py
@@ -119,6 +119,11 @@ class Config(DefaultConfig):
     # "user pwd" if you need to use SMTP AUTH
     #mail_login = ""
 
+    # Specify all message recipients in the To: field?  It is useful
+    # if destination mail reader wants explicit message recipient(s).
+    # Default is to use 'mail_from' as a To: field contents.
+    #mail_expand_to = False
+
 
     # User interface ----------------------------------------------------
 
-- 
1.5.6.4

