image.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. .. meta::
  2. :description: Send email with image in the body in Python.
  3. :keywords: send, email, Python, image, content
  4. .. _embedding-images:
  5. Sending Email with Image in Body
  6. ================================
  7. This section goes through how to send an email with
  8. Embedding Image from path
  9. ^^^^^^^^^^^^^^^^^^^^^^^^^
  10. You can also embed images straight to the HTML body
  11. of the email:
  12. .. code-block:: python
  13. email.send(
  14. subject='An image',
  15. receivers=['first.last@example.com'],
  16. html="""
  17. <h1>This is an image:</h1>
  18. {{ my_image }}
  19. """,
  20. body_images={
  21. 'my_image': 'path/to/image.png',
  22. }
  23. )
  24. The outcome looks like this:
  25. .. image:: /imgs/email_emb_img.png
  26. :align: center
  27. The image will be rendered as ``<img src="cid:...">``.
  28. In case you need to control the image (like the size)
  29. you can also create the ``img`` tag yourself:
  30. .. code-block:: python
  31. email.send(
  32. subject='An image',
  33. receivers=['first.last@example.com'],
  34. html="""
  35. <h1>This is an image:</h1>
  36. <img src="{{ my_image.src }}" width=500 height=350>
  37. """,
  38. body_images={
  39. 'my_image': 'path/to/image.png',
  40. }
  41. )
  42. In addition to paths as strings, the following are supported:
  43. - pathlib.Path
  44. - :ref:`bytes (the image as raw bytes) <embedding-images-bytes>`
  45. - :ref:`matplotlib.pyplot.Figure <embedding-images-plt>`
  46. - :ref:`PIL.Image (Pillow image) <embedding-images-pil>`
  47. - :ref:`dict (content as bytes and specify the type) <embedding-images-dict>`
  48. .. _embedding-images-bytes:
  49. Embedding Image from bytes
  50. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  51. You may also pass the image as bytes:
  52. .. code-block:: python
  53. import base64
  54. # data of a simple PNG image
  55. data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
  56. data_as_bytes = base64.b64decode(data_as_base64)
  57. gmail.send(
  58. subject='An image',
  59. receivers=['first.last@example.com'],
  60. html="""
  61. <h1>This is an image:</h1>
  62. {{ myimage }}
  63. """,
  64. body_images={
  65. 'myimage': data_as_bytes
  66. },
  67. )
  68. .. note::
  69. The bytes are expected to represent a PNG image. In case your image is in
  70. other format (ie. JPEG), you should specify the image using the
  71. :ref:`dict format <embedding-images-dict>`
  72. .. _embedding-images-dict:
  73. Embedding Image with dict format
  74. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  75. You may also include images using the dict format:
  76. .. code-block:: python
  77. import base64
  78. # data of a simple PNG image
  79. data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
  80. data_as_bytes = base64.b64decode(data_as_base64)
  81. gmail.send(
  82. subject='An image',
  83. receivers=['first.last@example.com'],
  84. html="""
  85. <h1>This is an image:</h1>
  86. {{ myimage }}
  87. """,
  88. body_images={
  89. 'myimage': {
  90. 'myimage': data_as_bytes,
  91. 'subtype': 'png'
  92. }
  93. }
  94. )
  95. This enables more control than including bytes as you may specify the ``subtype`` of the image.
  96. .. _embedding-images-plt:
  97. Embedding Figure
  98. ^^^^^^^^^^^^^^^^
  99. As mentioned, you may also include Matplotlib figures directly to the email.
  100. This is especially handy if you are creating automatic statistics.
  101. A simple example to include a figure:
  102. .. code-block:: python
  103. # Create a simple plot
  104. import matplotlib.pyplot as plt
  105. fig = plt.figure()
  106. plt.plot([1,2,3,2,3])
  107. # Send the plot
  108. email.send(
  109. subject='A plot',
  110. receivers=['first.last@example.com'],
  111. html="""
  112. <h1>This is a plot:</h1>
  113. {{ my_plot }}
  114. """,
  115. body_images={
  116. 'my_plot': fig,
  117. }
  118. )
  119. The outcome looks like this:
  120. .. image:: /imgs/email_emb_plt.png
  121. :align: center
  122. .. _embedding-images-pil:
  123. Embedding Pillow Image
  124. ^^^^^^^^^^^^^^^^^^^^^^
  125. You may also include Pillow image:
  126. .. code-block:: python
  127. # Create a simple image
  128. from PIL.Image import Image
  129. img = Image.new('RGB', (100, 30), color = (73, 109, 137))
  130. # Send the plot
  131. email.send(
  132. subject='A PIL image',
  133. receivers=['first.last@example.com'],
  134. html="""
  135. <h1>This is a Pillow image:</h1>
  136. {{ my_image }}
  137. """,
  138. body_images={
  139. 'my_image': img,
  140. }
  141. )