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>
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)