email: Factor out text/* part creation into get_mimetext().
authorW. Trevor King <wking@tremily.us>
Sun, 14 Apr 2013 11:55:05 +0000 (07:55 -0400)
committerW. Trevor King <wking@tremily.us>
Sun, 14 Apr 2013 12:00:03 +0000 (08:00 -0400)
Another utility function masking some of the stdlib's email oddities.
Besides use in get_message(), this function can be used to create
text/* parts for multipart messages included references (e.g. cid:
images).

Signed-off-by: W. Trevor King <wking@tremily.us>
rss2email/email.py

index ea00646b53337e2706acd6230eb11cdc76b12cce..46da48cf03fe2b2ea3b347de5b54361f3ff668f3 100644 (file)
@@ -130,34 +130,25 @@ def set_headers(message, sender, recipient, subject, extra_headers=None,
             encoding = guess_encoding(value, encodings)
             message[key] = _Header(value, encoding)
 
-def get_message(sender, recipient, subject, body, content_type,
-                extra_headers=None, config=None, section='DEFAULT'):
-    """Generate a `Message` instance.
+def get_mimetext(body, content_type='plain', config=None, section='DEFAULT'):
+    """Generate a text/* `Message` instance.
 
-    All arguments should be Unicode strings (plain ASCII works as well).
-
-    Only the real name part of sender and recipient addresses may contain
-    non-ASCII characters.
+    All arguments should be Unicode strings (plain ASCII works as
+    well).
 
     The email will be properly MIME encoded.
 
-    The charset of the email will be the first one out of the list
-    that can represent all the characters occurring in the email.
+    The message charset will be the first one out of the configured
+    list of encodings that can represent all the characters occurring
+    in the body.
 
-    >>> message = get_message(
-    ...     sender='John <jdoe@a.com>', recipient='Ζεύς <z@olympus.org>',
-    ...     subject='Testing',
+    >>> message = get_mimetext(
     ...     body='Hello, world!\\n',
-    ...     content_type='plain',
-    ...     extra_headers={'Approved': 'joe@bob.org'})
+    ...     content_type='plain')
     >>> print(message.as_string())  # doctest: +REPORT_UDIFF
     MIME-Version: 1.0
     Content-Type: text/plain; charset="us-ascii"
     Content-Transfer-Encoding: 7bit
-    From: John <jdoe@a.com>
-    To: =?utf-8?b?zpbOtc+Nz4I=?= <z@olympus.org>
-    Subject: Testing
-    Approved: joe@bob.org
     <BLANKLINE>
     Hello, world!
     <BLANKLINE>
@@ -174,6 +165,35 @@ def get_message(sender, recipient, subject, body, content_type,
         charset = _Charset(body_encoding)
         charset.body_encoding = _email_encoders.encode_7or8bit
         message.set_payload(body, charset=charset)
+    return message
+
+def get_message(sender, recipient, subject, body, content_type,
+                extra_headers=None, config=None, section='DEFAULT'):
+    """Generate a `Message` instance.
+
+    This is a convenient wrapper around `get_mimetext()` and
+    `set_headers()`.
+
+    >>> message = get_message(
+    ...     sender='John <jdoe@a.com>', recipient='Ζεύς <z@olympus.org>',
+    ...     subject='Testing',
+    ...     body='Hello, world!\\n',
+    ...     content_type='plain',
+    ...     extra_headers={'Approved': 'joe@bob.org'})
+    >>> print(message.as_string())  # doctest: +REPORT_UDIFF
+    MIME-Version: 1.0
+    Content-Type: text/plain; charset="us-ascii"
+    Content-Transfer-Encoding: 7bit
+    From: John <jdoe@a.com>
+    To: =?utf-8?b?zpbOtc+Nz4I=?= <z@olympus.org>
+    Subject: Testing
+    Approved: joe@bob.org
+    <BLANKLINE>
+    Hello, world!
+    <BLANKLINE>
+    """
+    message = get_mimetext(
+        body=body, content_type=content_type, config=config, section=section)
     set_headers(
         message=message, sender=sender, recipient=recipient, subject=subject,
         extra_headers=extra_headers, config=config, section=section)