From 22238844d1c33b0d43872458f030147bba072f57 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 14 Apr 2013 07:55:05 -0400 Subject: [PATCH] email: Factor out text/* part creation into get_mimetext(). 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 --- rss2email/email.py | 56 +++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/rss2email/email.py b/rss2email/email.py index ea00646..46da48c 100644 --- a/rss2email/email.py +++ b/rss2email/email.py @@ -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 ', recipient='Ζεύς ', - ... 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 - To: =?utf-8?b?zpbOtc+Nz4I=?= - Subject: Testing - Approved: joe@bob.org Hello, world! @@ -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 ', recipient='Ζεύς ', + ... 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 + To: =?utf-8?b?zpbOtc+Nz4I=?= + Subject: Testing + Approved: joe@bob.org + + Hello, world! + + """ + 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) -- 2.26.2