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