#8989: add 'domain' keyword to make_msgid.
authorR. David Murray <rdmurray@bitdance.com>
Thu, 2 Dec 2010 21:47:19 +0000 (21:47 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Thu, 2 Dec 2010 21:47:19 +0000 (21:47 +0000)
Patch by Adrian von Bidder.

Doc/library/email.util.rst
Lib/email/test/test_email.py
Lib/email/utils.py
Misc/ACKS
Misc/NEWS

index a1ce3017f427f787712d80b7e7a10c0dcdf93ecd..f7b777a01d696eea848e50de788990ebcaf531e6 100644 (file)
@@ -105,11 +105,17 @@ There are several useful utilities provided in the :mod:`email.utils` module:
    ``False``.  The default is ``False``.
 
 
-.. function:: make_msgid(idstring=None)
+.. function:: make_msgid(idstring=None, domain=None)
 
    Returns a string suitable for an :rfc:`2822`\ -compliant
    :mailheader:`Message-ID` header.  Optional *idstring* if given, is a string
-   used to strengthen the uniqueness of the message id.
+   used to strengthen the uniqueness of the message id.  Optional *domain* if
+   given provides the portion of the msgid after the '@'.  The default is the
+   local hostname.  It is not normally necessary to override this default, but
+   may be useful certain cases, such as a constructing distributed system that
+   uses a consistent domain name across multiple hosts.
+
+   .. versionchanged:: 3.2 domain keyword added
 
 
 .. function:: decode_rfc2231(s)
index 5545abe4a64fcaae791337bfe337581d9c5467b7..79934869e6d5009a9e4411221cd71abb82f1153a 100644 (file)
@@ -2457,6 +2457,10 @@ multipart/report
     text/rfc822-headers
 """)
 
+    def test_make_msgid_domain(self):
+        self.assertEqual(
+            email.utils.make_msgid(domain='testdomain-string')[-19:],
+            '@testdomain-string>')
 
 
 # Test the iterator/generators
index 5f40bac174bb94fce26025afcc5377b67ba9c8be..ac4da3705f31781ff038af9b1b3bf351b208e3c4 100644 (file)
@@ -148,13 +148,15 @@ def formatdate(timeval=None, localtime=False, usegmt=False):
 
 
 
-def make_msgid(idstring=None):
+def make_msgid(idstring=None, domain=None):
     """Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
 
     <20020201195627.33539.96671@nightshade.la.mastaler.com>
 
     Optional idstring if given is a string used to strengthen the
-    uniqueness of the message id.
+    uniqueness of the message id.  Optional domain if given provides the
+    portion of the message id after the '@'.  It defaults to the locally
+    defined hostname.
     """
     timeval = time.time()
     utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval))
@@ -164,8 +166,9 @@ def make_msgid(idstring=None):
         idstring = ''
     else:
         idstring = '.' + idstring
-    idhost = socket.getfqdn()
-    msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, idhost)
+    if domain is None:
+        domain = socket.getfqdn()
+    msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, domain)
     return msgid
 
 
index d3ec04729b7f92c1b5a1e4da64ea9a879b087e9f..6f1f89a448b6c95af9f903d18ff54e74685d2973 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -77,6 +77,7 @@ Eric Beser
 Steven Bethard
 Stephen Bevan
 Ron Bickers
+Adrian von Bidder
 David Binger
 Dominic Binks
 Philippe Biondi
index aeb1f143cc85347f7493b0ae1d12542e5f2c6389..820534370a068ed916ab9b66877512c7a8a3f4c1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -53,6 +53,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #8989: email.utils.make_msgid now has a domain parameter that can
+  override the domain name used in the generated msgid.
+
 - Issue #9299: Add exist_ok parameter to os.makedirs to suppress the
   'File exists' exception when a target directory already exists with the
   specified mode. Patch by Ray Allen.