jpayne@68: # Copyright (C) 2001-2006 Python Software Foundation jpayne@68: # Author: Barry Warsaw jpayne@68: # Contact: email-sig@python.org jpayne@68: jpayne@68: """Class representing text/* type MIME documents.""" jpayne@68: jpayne@68: __all__ = ['MIMEText'] jpayne@68: jpayne@68: from email.charset import Charset jpayne@68: from email.mime.nonmultipart import MIMENonMultipart jpayne@68: jpayne@68: jpayne@68: jpayne@68: class MIMEText(MIMENonMultipart): jpayne@68: """Class for generating text/* type MIME documents.""" jpayne@68: jpayne@68: def __init__(self, _text, _subtype='plain', _charset=None, *, policy=None): jpayne@68: """Create a text/* type MIME document. jpayne@68: jpayne@68: _text is the string for this message object. jpayne@68: jpayne@68: _subtype is the MIME sub content type, defaulting to "plain". jpayne@68: jpayne@68: _charset is the character set parameter added to the Content-Type jpayne@68: header. This defaults to "us-ascii". Note that as a side-effect, the jpayne@68: Content-Transfer-Encoding header will also be set. jpayne@68: """ jpayne@68: jpayne@68: # If no _charset was specified, check to see if there are non-ascii jpayne@68: # characters present. If not, use 'us-ascii', otherwise use utf-8. jpayne@68: # XXX: This can be removed once #7304 is fixed. jpayne@68: if _charset is None: jpayne@68: try: jpayne@68: _text.encode('us-ascii') jpayne@68: _charset = 'us-ascii' jpayne@68: except UnicodeEncodeError: jpayne@68: _charset = 'utf-8' jpayne@68: jpayne@68: MIMENonMultipart.__init__(self, 'text', _subtype, policy=policy, jpayne@68: **{'charset': str(_charset)}) jpayne@68: jpayne@68: self.set_payload(_text, _charset)