1
0

image.rst 4.8 KB

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