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 image/* type MIME documents.""" |
---|
6 | n/a | |
---|
7 | n/a | __all__ = ['MIMEImage'] |
---|
8 | n/a | |
---|
9 | n/a | import imghdr |
---|
10 | n/a | |
---|
11 | n/a | from email import encoders |
---|
12 | n/a | from email.mime.nonmultipart import MIMENonMultipart |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | class MIMEImage(MIMENonMultipart): |
---|
17 | n/a | """Class for generating image/* type MIME documents.""" |
---|
18 | n/a | |
---|
19 | n/a | def __init__(self, _imagedata, _subtype=None, |
---|
20 | n/a | _encoder=encoders.encode_base64, *, policy=None, **_params): |
---|
21 | n/a | """Create an image/* type MIME document. |
---|
22 | n/a | |
---|
23 | n/a | _imagedata is a string containing the raw image data. If this data |
---|
24 | n/a | can be decoded by the standard Python `imghdr' module, then the |
---|
25 | n/a | subtype will be automatically included in the Content-Type header. |
---|
26 | n/a | Otherwise, you can specify the specific image subtype via the _subtype |
---|
27 | n/a | parameter. |
---|
28 | n/a | |
---|
29 | n/a | _encoder is a function which will perform the actual encoding for |
---|
30 | n/a | transport of the image data. It takes one argument, which is this |
---|
31 | n/a | Image instance. It should use get_payload() and set_payload() to |
---|
32 | n/a | change the payload to the encoded form. It should also add any |
---|
33 | n/a | Content-Transfer-Encoding or other headers to the message as |
---|
34 | n/a | necessary. The default encoding is Base64. |
---|
35 | n/a | |
---|
36 | n/a | Any additional keyword arguments are passed to the base class |
---|
37 | n/a | constructor, which turns them into parameters on the Content-Type |
---|
38 | n/a | header. |
---|
39 | n/a | """ |
---|
40 | n/a | if _subtype is None: |
---|
41 | n/a | _subtype = imghdr.what(None, _imagedata) |
---|
42 | n/a | if _subtype is None: |
---|
43 | n/a | raise TypeError('Could not guess image MIME subtype') |
---|
44 | n/a | MIMENonMultipart.__init__(self, 'image', _subtype, policy=policy, |
---|
45 | n/a | **_params) |
---|
46 | n/a | self.set_payload(_imagedata) |
---|
47 | n/a | _encoder(self) |
---|