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