mime types
[software/python-on-guile.git] / modules / language / python / module / email / mime / text.py
1 module(email,mime,text)
2 # Copyright (C) 2001-2006 Python Software Foundation
3 # Author: Barry Warsaw
4 # Contact: email-sig@python.org
5
6 """Class representing text/* type MIME documents."""
7
8 __all__ = ['MIMEText']
9
10 from email.charset import Charset
11 from email.mime.nonmultipart import MIMENonMultipart
12
13
14
15 class MIMEText(MIMENonMultipart):
16 """Class for generating text/* type MIME documents."""
17
18 def __init__(self, _text, _subtype='plain', _charset=None, *, policy=None):
19 """Create a text/* type MIME document.
20
21 _text is the string for this message object.
22
23 _subtype is the MIME sub content type, defaulting to "plain".
24
25 _charset is the character set parameter added to the Content-Type
26 header. This defaults to "us-ascii". Note that as a side-effect, the
27 Content-Transfer-Encoding header will also be set.
28 """
29
30 # If no _charset was specified, check to see if there are non-ascii
31 # characters present. If not, use 'us-ascii', otherwise use utf-8.
32 # XXX: This can be removed once #7304 is fixed.
33 if _charset is None:
34 try:
35 _text.encode('us-ascii')
36 _charset = 'us-ascii'
37 except UnicodeEncodeError:
38 _charset = 'utf-8'
39
40 MIMENonMultipart.__init__(self, 'text', _subtype, policy=policy,
41 **{'charset': str(_charset)})
42
43 self.set_payload(_text, _charset)