blob: 5a5e39bb3891f3823f4501c6ddbfb7a334bae34f [file] [log] [blame]
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001# 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
9from email.encoders import encode_7or8bit
10from email.mime.nonmultipart import MIMENonMultipart
11
12
13
14class MIMEText(MIMENonMultipart):
15 """Class for generating text/* type MIME documents."""
16
17 def __init__(self, _text, _subtype='plain', _charset='us-ascii'):
18 """Create a text/* type MIME document.
19
20 _text is the string for this message object.
21
22 _subtype is the MIME sub content type, defaulting to "plain".
23
24 _charset is the character set parameter added to the Content-Type
25 header. This defaults to "us-ascii". Note that as a side-effect, the
26 Content-Transfer-Encoding header will also be set.
27 """
28 MIMENonMultipart.__init__(self, 'text', _subtype,
29 **{'charset': _charset})
R David Murray8680bcc2012-03-22 22:17:51 -040030
31 # If _charset was defualted, check to see see if there are non-ascii
32 # characters present. Default to utf-8 if there are.
33 # XXX: This can be removed once #7304 is fixed.
34 if _charset =='us-ascii':
35 try:
36 _text.encode(_charset)
37 except UnicodeEncodeError:
38 _charset = 'utf-8'
39
Guido van Rossum8b3febe2007-08-30 01:15:14 +000040 self.set_payload(_text, _charset)