bh: utils/testing send_filtertest_emails.py,1.1,1.2

cvs at intevation.de cvs at intevation.de
Wed Dec 22 16:29:39 CET 2004


Author: bh

Update of /kolabrepository/utils/testing
In directory doto:/tmp/cvs-serv886

Modified Files:
	send_filtertest_emails.py 
Log Message:
Added command line parsing.  There are no hardcoded values anymore for
username and maildomain.  They have to be given on the command line.
The smtp server and port can also be given on the command line.


Index: send_filtertest_emails.py
===================================================================
RCS file: /kolabrepository/utils/testing/send_filtertest_emails.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- send_filtertest_emails.py	17 Dec 2004 18:54:31 -0000	1.1
+++ send_filtertest_emails.py	22 Dec 2004 15:29:37 -0000	1.2
@@ -11,6 +11,7 @@
 Bernhard Reiter <bernhard at intevation.de>
 """
 #20041217 Bernhard initial
+#20041222 Bernhard Herzog.
 __version__="$Revision$"[10:-1]
 
 import sys
@@ -18,9 +19,10 @@
 
 import smtplib
 import string
+import getopt
 
-def send_mail(envelope_from, envelope_to, header_from, header_to, 
-    test_number=0):
+def send_mail(envelope_from, envelope_to, header_from, header_to,
+              test_number=0, smtp_host="localhost", smtp_port = None):
 
     basemsg=("From: %s\r\nTo: %s\r\n%%s\r\n" % 
                 (header_from, string.join(header_to, ", "))
@@ -36,7 +38,7 @@
     head=basemsg % (headers + subject)
     msg=head+"Hello!\n"
 
-    server=smtplib.SMTP('localhost')
+    server=smtplib.SMTP(smtp_host, smtp_port)
     #server.set_debuglevel(1)
 
     server.sendmail(envelope_from,envelope_to,msg)
@@ -47,21 +49,85 @@
     #sys.stdout.write("\n")
 
 
+def assemble_email_address(username, domain, realname = None):
+    """Assemble an email address from a realname, a username and a domain.
+
+    If realname is given and not None, the return value is a string with
+    realname followed by a space and the emailaddress username at domain in
+    angle brackets.  If realname is omitted or None, the return value is
+    just the email address username at domain.
+    """
+    if realname is not None:
+        return "%(realname)s <%(username)s@%(domain)s>" % locals()
+    else:
+        return "%(username)s@%(domain)s" % locals()
+
+
 def main(argv):
+    # default values for the parameters
 
-    maildomain="kolab.mail.domain"
+    # Domain used in the email addresses
+    maildomain = None
 
-    envelope_from="Proko2Test <proko2test@%s>" % maildomain
-    header_from="proko2test@%s" % maildomain
+    # username.  username at maildomain will be the email address used
+    username = None
 
-    envelope_to="Proko2Test <proko2test@%s>" % maildomain
-    header_to= ["proko2test@%s" % maildomain]
+    # The real name part of an email address,
+    # i.e. "realname <username at maildomain>"
+    realname = None
 
-    send_mail(envelope_from, envelope_to, header_from, header_to, 0)
+    # SMTP host and port to use when sending the test mails
+    smtp_host = "localhost"
+    smtp_port = 25
+
+    # parse the command line
+    opts, args = getopt.getopt(argv[1:], 'h:p:',
+                               ["smtp-host=", "smtp-port=",
+                                "maildomain=", "username=", "realname=",
+                                'version'])
+    for optchar, value in opts:
+        if optchar in ("-h", "--smtp-host"):
+            smtp_host = value
+        elif optchar in ("-p", "--smtp-port"):
+            smtp_port = int(value)
+        elif optchar == "--maildomain":
+            maildomain = value
+        elif optchar == "--username":
+            username = value
+        elif optchar == "--realname":
+            realname = value
+        elif optchar == "--version":
+            print __version__
+            return
+        else:
+            print >>sys.stderr, "unknown option %r" % optchar
+
+
+    error = False
+    if maildomain is None:
+        print >>sys.stderr, "--maildomain missing"
+        error = True
+    if username is None:
+        print >>sys.stderr, "--username missing"
+        error = True
+
+    if error:
+        sys.exit(1)
+
+    envelope_to = assemble_email_address(username, maildomain, realname)
+    header_to= [assemble_email_address(username, maildomain)]
+
+    envelope_from = envelope_to
+    header_from = assemble_email_address(username, maildomain)
+
+    send_mail(envelope_from, envelope_to, header_from, header_to, 0,
+              smtp_host = smtp_host, smtp_port = smtp_port)
+
+    # First test: different letter cases in the header_from, should go
+    # through
+    send_mail(envelope_from, envelope_to, header_from.swapcase(), header_to, 1,
+              smtp_host = smtp_host, smtp_port = smtp_port)
 
-    # First test: different letter cases, should go through
-    header_from="proKo2tEst@%s" % maildomain
-    send_mail(envelope_from, envelope_to, header_from, header_to, 1)
 
 if __name__=="__main__":
     main(sys.argv)





More information about the commits mailing list