blob: 5563823239b8be88893d6d03a39fb1a206c65285 [file] [log] [blame]
Barry Warsaw40ef0062006-03-18 15:41:53 +00001# Copyright (C) 2001-2006 Python Software Foundation
Barry Warsawbb113862004-10-03 03:16:19 +00002# Author: Barry Warsaw
3# Contact: email-sig@python.org
Barry Warsawba925802001-09-23 03:17:28 +00004
Barry Warsawbb113862004-10-03 03:16:19 +00005"""Class representing image/* type MIME documents."""
Barry Warsawba925802001-09-23 03:17:28 +00006
Barry Warsaw40ef0062006-03-18 15:41:53 +00007__all__ = ['MIMEImage']
8
Barry Warsawba925802001-09-23 03:17:28 +00009import imghdr
10
Barry Warsaw40ef0062006-03-18 15:41:53 +000011from email import encoders
12from email.mime.nonmultipart import MIMENonMultipart
Barry Warsawba925802001-09-23 03:17:28 +000013
14
Barry Warsawe968ead2001-10-04 17:05:11 +000015
Barry Warsaw524af6f2002-06-02 19:05:08 +000016class MIMEImage(MIMENonMultipart):
Barry Warsawba925802001-09-23 03:17:28 +000017 """Class for generating image/* type MIME documents."""
18
Barry Warsaw3dd978d2001-09-26 05:34:30 +000019 def __init__(self, _imagedata, _subtype=None,
Barry Warsaw40ef0062006-03-18 15:41:53 +000020 _encoder=encoders.encode_base64, **_params):
Barry Warsawba925802001-09-23 03:17:28 +000021 """Create an image/* type MIME document.
22
23 _imagedata is a string containing the raw image data. If this data
24 can be decoded by the standard Python `imghdr' module, then the
Barry Warsaw00319822002-09-30 22:15:00 +000025 subtype will be automatically included in the Content-Type header.
Barry Warsaw3dd978d2001-09-26 05:34:30 +000026 Otherwise, you can specify the specific image subtype via the _subtype
Barry Warsawba925802001-09-23 03:17:28 +000027 parameter.
28
29 _encoder is a function which will perform the actual encoding for
30 transport of the image data. It takes one argument, which is this
31 Image instance. It should use get_payload() and set_payload() to
32 change the payload to the encoded form. It should also add any
Barry Warsaw00319822002-09-30 22:15:00 +000033 Content-Transfer-Encoding or other headers to the message as
Barry Warsawba925802001-09-23 03:17:28 +000034 necessary. The default encoding is Base64.
35
36 Any additional keyword arguments are passed to the base class
Barry Warsaw00319822002-09-30 22:15:00 +000037 constructor, which turns them into parameters on the Content-Type
Barry Warsawba925802001-09-23 03:17:28 +000038 header.
39 """
Barry Warsaw3dd978d2001-09-26 05:34:30 +000040 if _subtype is None:
41 _subtype = imghdr.what(None, _imagedata)
42 if _subtype is None:
Barry Warsawbb113862004-10-03 03:16:19 +000043 raise TypeError('Could not guess image MIME subtype')
Barry Warsaw524af6f2002-06-02 19:05:08 +000044 MIMENonMultipart.__init__(self, 'image', _subtype, **_params)
Barry Warsawba925802001-09-23 03:17:28 +000045 self.set_payload(_imagedata)
46 _encoder(self)