jpayne@68: # Copyright (C) 2001-2006 Python Software Foundation jpayne@68: # Author: Barry Warsaw jpayne@68: # Contact: email-sig@python.org jpayne@68: jpayne@68: """Class representing image/* type MIME documents.""" jpayne@68: jpayne@68: __all__ = ['MIMEImage'] jpayne@68: jpayne@68: import imghdr jpayne@68: jpayne@68: from email import encoders jpayne@68: from email.mime.nonmultipart import MIMENonMultipart jpayne@68: jpayne@68: jpayne@68: jpayne@68: class MIMEImage(MIMENonMultipart): jpayne@68: """Class for generating image/* type MIME documents.""" jpayne@68: jpayne@68: def __init__(self, _imagedata, _subtype=None, jpayne@68: _encoder=encoders.encode_base64, *, policy=None, **_params): jpayne@68: """Create an image/* type MIME document. jpayne@68: jpayne@68: _imagedata is a string containing the raw image data. If this data jpayne@68: can be decoded by the standard Python `imghdr' module, then the jpayne@68: subtype will be automatically included in the Content-Type header. jpayne@68: Otherwise, you can specify the specific image subtype via the _subtype jpayne@68: parameter. jpayne@68: jpayne@68: _encoder is a function which will perform the actual encoding for jpayne@68: transport of the image data. It takes one argument, which is this jpayne@68: Image instance. It should use get_payload() and set_payload() to jpayne@68: change the payload to the encoded form. It should also add any jpayne@68: Content-Transfer-Encoding or other headers to the message as jpayne@68: necessary. The default encoding is Base64. jpayne@68: jpayne@68: Any additional keyword arguments are passed to the base class jpayne@68: constructor, which turns them into parameters on the Content-Type jpayne@68: header. jpayne@68: """ jpayne@68: if _subtype is None: jpayne@68: _subtype = imghdr.what(None, _imagedata) jpayne@68: if _subtype is None: jpayne@68: raise TypeError('Could not guess image MIME subtype') jpayne@68: MIMENonMultipart.__init__(self, 'image', _subtype, policy=policy, jpayne@68: **_params) jpayne@68: self.set_payload(_imagedata) jpayne@68: _encoder(self)