[mail] allow to specify SMTP's MAIL FROM address to config.sendmails(). Closes #3373620
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 07 Jan 2014 15:48:31 +0100
changeset 9363 d773589b6d46
parent 9362 2f0129d17fa9
child 9364 73bd5012336f
[mail] allow to specify SMTP's MAIL FROM address to config.sendmails(). Closes #3373620 Also modify testlib.Email so we don't loose this information and may test it.
cwconfig.py
devtools/testlib.py
--- a/cwconfig.py	Wed Nov 13 15:58:42 2013 +0100
+++ b/cwconfig.py	Tue Jan 07 15:48:31 2014 +0100
@@ -1177,11 +1177,13 @@
         sourcedirs.append(self.i18n_lib_dir())
         return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs)
 
-    def sendmails(self, msgs):
+    def sendmails(self, msgs, fromaddr=None):
         """msgs: list of 2-uple (message object, recipients). Return False
         if connection to the smtp server failed, else True.
         """
         server, port = self['smtp-host'], self['smtp-port']
+        if fromaddr is None:
+            fromaddr = '%s <%s>' % (self['sender-name'], self['sender-addr'])
         SMTP_LOCK.acquire()
         try:
             try:
@@ -1190,10 +1192,9 @@
                 self.exception("can't connect to smtp server %s:%s (%s)",
                                server, port, ex)
                 return False
-            heloaddr = '%s <%s>' % (self['sender-name'], self['sender-addr'])
             for msg, recipients in msgs:
                 try:
-                    smtp.sendmail(heloaddr, recipients, msg.as_string())
+                    smtp.sendmail(fromaddr, recipients, msg.as_string())
                 except Exception as ex:
                     self.exception("error sending mail to %s (%s)",
                                    recipients, ex)
--- a/devtools/testlib.py	Wed Nov 13 15:58:42 2013 +0100
+++ b/devtools/testlib.py	Tue Jan 07 15:48:31 2014 +0100
@@ -89,7 +89,7 @@
 
 MAILBOX = []
 
-class Email:
+class Email(object):
     """you'll get instances of Email into MAILBOX during tests that trigger
     some notification.
 
@@ -98,7 +98,8 @@
     * `recipients` is a list of email address which are the recipients of this
       message
     """
-    def __init__(self, recipients, msg):
+    def __init__(self, fromaddr, recipients, msg):
+        self.fromaddr = fromaddr
         self.recipients = recipients
         self.msg = msg
 
@@ -125,8 +126,8 @@
         pass
     def close(self):
         pass
-    def sendmail(self, helo_addr, recipients, msg):
-        MAILBOX.append(Email(recipients, msg))
+    def sendmail(self, fromaddr, recipients, msg):
+        MAILBOX.append(Email(fromaddr, recipients, msg))
 
 cwconfig.SMTP = MockSMTP