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."""
       6  
       7  __all__ = ['MIMEText']
       8  
       9  from email.charset import Charset
      10  from email.mime.nonmultipart import MIMENonMultipart
      11  
      12  
      13  class ESC[4;38;5;81mMIMEText(ESC[4;38;5;149mMIMENonMultipart):
      14      """Class for generating text/* type MIME documents."""
      15  
      16      def __init__(self, _text, _subtype='plain', _charset=None, *, policy=None):
      17          """Create a text/* type MIME document.
      18  
      19          _text is the string for this message object.
      20  
      21          _subtype is the MIME sub content type, defaulting to "plain".
      22  
      23          _charset is the character set parameter added to the Content-Type
      24          header.  This defaults to "us-ascii".  Note that as a side-effect, the
      25          Content-Transfer-Encoding header will also be set.
      26          """
      27  
      28          # If no _charset was specified, check to see if there are non-ascii
      29          # characters present. If not, use 'us-ascii', otherwise use utf-8.
      30          # XXX: This can be removed once #7304 is fixed.
      31          if _charset is None:
      32              try:
      33                  _text.encode('us-ascii')
      34                  _charset = 'us-ascii'
      35              except UnicodeEncodeError:
      36                  _charset = 'utf-8'
      37  
      38          MIMENonMultipart.__init__(self, 'text', _subtype, policy=policy,
      39                                    **{'charset': str(_charset)})
      40  
      41          self.set_payload(_text, _charset)