blob: d91b93df3d1814ff6d3ffadf3a63c7ddfc9ab883 [file] [log] [blame]
Barry Warsaw409a4c02002-04-10 21:01:31 +00001# Copyright (C) 2001,2002 Python Software Foundation
Barry Warsawba925802001-09-23 03:17:28 +00002# Author: barry@zope.com (Barry Warsaw)
3
4"""Class representing text/* type MIME documents.
5"""
6
Barry Warsaw409a4c02002-04-10 21:01:31 +00007import warnings
Barry Warsaw524af6f2002-06-02 19:05:08 +00008from email.MIMENonMultipart import MIMENonMultipart
9from email.Encoders import encode_7or8bit
Barry Warsawba925802001-09-23 03:17:28 +000010
11
Barry Warsawe968ead2001-10-04 17:05:11 +000012
Barry Warsaw524af6f2002-06-02 19:05:08 +000013class MIMEText(MIMENonMultipart):
Barry Warsawba925802001-09-23 03:17:28 +000014 """Class for generating text/* type MIME documents."""
15
Barry Warsaw3dd978d2001-09-26 05:34:30 +000016 def __init__(self, _text, _subtype='plain', _charset='us-ascii',
Barry Warsaw409a4c02002-04-10 21:01:31 +000017 _encoder=None):
Barry Warsawba925802001-09-23 03:17:28 +000018 """Create a text/* type MIME document.
19
20 _text is the string for this message object. If the text does not end
21 in a newline, one is added.
22
Barry Warsaw3dd978d2001-09-26 05:34:30 +000023 _subtype is the MIME sub content type, defaulting to "plain".
Barry Warsawba925802001-09-23 03:17:28 +000024
Barry Warsaw2d7fab12002-10-01 00:52:27 +000025 _charset is the character set parameter added to the Content-Type
Barry Warsaw409a4c02002-04-10 21:01:31 +000026 header. This defaults to "us-ascii". Note that as a side-effect, the
Barry Warsaw2d7fab12002-10-01 00:52:27 +000027 Content-Transfer-Encoding header will also be set.
Barry Warsawba925802001-09-23 03:17:28 +000028
Barry Warsaw409a4c02002-04-10 21:01:31 +000029 The use of the _encoder is deprecated. The encoding of the payload,
30 and the setting of the character set parameter now happens implicitly
31 based on the _charset argument. If _encoder is supplied, then a
32 DeprecationWarning is used, and the _encoder functionality may
33 override any header settings indicated by _charset. This is probably
34 not what you want.
Barry Warsawba925802001-09-23 03:17:28 +000035 """
Barry Warsaw524af6f2002-06-02 19:05:08 +000036 MIMENonMultipart.__init__(self, 'text', _subtype,
37 **{'charset': _charset})
Barry Warsawbba6b022002-09-28 20:27:28 +000038 if _text and not _text.endswith('\n'):
Barry Warsawba925802001-09-23 03:17:28 +000039 _text += '\n'
Barry Warsaw409a4c02002-04-10 21:01:31 +000040 self.set_payload(_text, _charset)
41 if _encoder is not None:
42 warnings.warn('_encoder argument is obsolete.',
43 DeprecationWarning, 2)
44 # Because set_payload() with a _charset will set its own
Barry Warsaw2d7fab12002-10-01 00:52:27 +000045 # Content-Transfer-Encoding header, we need to delete the
Barry Warsaw409a4c02002-04-10 21:01:31 +000046 # existing one or will end up with two of them. :(
47 del self['content-transfer-encoding']
48 _encoder(self)