Julian Brost avatar Julian Brost committed 699bc60

Updated sendmail function to allow adding recipients to To: and CC: headers.

Comments (0)

Files changed (2)

MoinMoin/mail/sendmail.py

         return str(address)
 
 
-def sendmail(to, subject, text, mail_from=None):
+def sendmail(subject, text, to=None, cc=None, bcc=None, mail_from=None):
     """ Create and send a text/plain message
 
     Return a tuple of success or error indicator and message.
 
-    :param to: recipients (list)
-    :param subject: subject of email (unicode)
-    :param text: email body text (unicode)
+    :param subject: subject of email
+    :type subject: unicode
+    :param text: email body text
+    :type text: unicode
+    :param to: recipients
+    :type to: list
+    :param cc: recipients (CC)
+    :type cc: list
+    :param bcc: recipients (BCC)
+    :type bcc: list
     :param mail_from: override default mail_from
     :type mail_from: unicode
+
     :rtype: tuple
     :returns: (is_ok, Description of error or OK message)
     """
     logging.debug("send mail, from: {0!r}, subj: {1!r}".format(mail_from, subject))
     logging.debug("send mail, to: {0!r}".format(to))
 
-    if not to:
+    if not to and not cc and not bcc:
         return (1, _("No recipients, nothing to do"))
 
     subject = subject.encode(config.charset)
 
     msg.set_payload(text)
 
-    # Create message headers
-    # Don't expose emails addreses of the other subscribers, instead we
-    # 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 to:
+        msg['To'] = ','.join(to)
+    if cc:
+        msg['CC'] = ','.join(cc)
     msg['Date'] = formatdate()
     msg['Message-ID'] = make_msgid()
     msg['Subject'] = Header(subject, charset)
     msg['Auto-Submitted'] = 'auto-generated'
 
     if cfg.mail_sendmail:
-        # Set the BCC.  This will be stripped later by sendmail.
-        msg['BCC'] = ','.join(to)
+        if bcc:
+            # Set the BCC.  This will be stripped later by sendmail.
+            msg['BCC'] = ','.join(bcc)
         # Set Return-Path so that it isn't set (generally incorrectly) for us.
         msg['Return-Path'] = address
 
                         logging.debug("could not establish a tls connection to smtp server, continuing without tls")
                     logging.debug("trying to log in to smtp server using account '{0}'".format(cfg.mail_username))
                     server.login(cfg.mail_username, cfg.mail_password)
-                server.sendmail(mail_from, to, msg.as_string())
+                server.sendmail(mail_from, (to or []) + (cc or []) + (bcc or []), msg.as_string())
             finally:
                 try:
                     server.quit()
 
         subject = _('[%(sitename)s] Your wiki password recovery link',
                     sitename=self._cfg.sitename or "Wiki")
-        mailok, msg = sendmail.sendmail([self.email], subject, text, mail_from=self._cfg.mail_from)
+        mailok, msg = sendmail.sendmail(subject, text, to=[self.email], mail_from=self._cfg.mail_from)
         return mailok, msg
 
     def mailVerificationLink(self):
 """, link=url_for('frontend.verifyemail',
                         username=self.name, token=token, _external=True))
 
-        subject = _('[%(sitename)s] Verify your email address',
+        subject = _('[%(sitename)s] Please verify your email address',
                     sitename=self._cfg.sitename or "Wiki")
-        mailok, msg = sendmail.sendmail([self.email], subject, text, mail_from=self._cfg.mail_from)
+        mailok, msg = sendmail.sendmail(subject, text, to=[self.email], mail_from=self._cfg.mail_from)
         return mailok, msg
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.