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