body_content.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. .. _embedded:
  2. Embedding Content
  3. =================
  4. Red Mail allows to embed images and tables to the
  5. HTML bodies of emails. By default, tables often
  6. look outdated and ugly in emails but Red Mail
  7. has pre-made table templates that render nicer
  8. looking tables from Pandas dataframes.
  9. .. _embedding-images:
  10. Embedded Images
  11. ---------------
  12. You can also embed images straight to the HTML body
  13. of the email:
  14. .. code-block:: python
  15. email.send(
  16. subject='An image',
  17. receivers=['first.last@example.com'],
  18. html="""<h1>This is an image:</h1>
  19. {{ my_image }}
  20. """,
  21. body_images={
  22. 'my_image': 'path/to/image.png',
  23. }
  24. )
  25. The outcome looks like this:
  26. .. image:: /imgs/email_emb_img.png
  27. :align: center
  28. The image will be rendered as ``<img src="cid:...">``.
  29. In case you need to control the image (like the size)
  30. you can also create the ``img`` tag yourself:
  31. .. code-block:: python
  32. email.send(
  33. subject='An image',
  34. receivers=['first.last@example.com'],
  35. html='<h1>This is an image:</h1> <img src="{{ my_image.src }}" width=500 height=350>',
  36. body_images={
  37. 'my_image': 'path/to/image.png',
  38. }
  39. )
  40. In addition to paths as strings, the following are supported:
  41. - ``pathlib.Path``
  42. - ``bytes`` (the image as raw bytes)
  43. - ``matplotlib.pyplot.Figure``
  44. - ``PIL.Image``
  45. .. _embedding-plt:
  46. Embedding Figure
  47. ^^^^^^^^^^^^^^^^
  48. As mentioned, you may also include Matplotlib figures directly to the email.
  49. This is especially handy if you are creating automatic statistics.
  50. A simple example to include a figure:
  51. .. code-block:: python
  52. # Create a simple plot
  53. import matplotlib.pyplot as plt
  54. fig = plt.figure()
  55. plt.plot([1,2,3,2,3])
  56. # Send the plot
  57. email.send(
  58. subject='A plot',
  59. receivers=['first.last@example.com'],
  60. html="""<h1>This is a plot:</h1>
  61. {{ my_plot }}
  62. """,
  63. body_images={
  64. 'my_plot': fig,
  65. }
  66. )
  67. The outcome looks like this:
  68. .. image:: /imgs/email_emb_plt.png
  69. :align: center
  70. .. _embedding-tables:
  71. Embedded Tables
  72. ---------------
  73. You may include tables simply by turning them
  74. to raw HTML for example using ``df.to_html()``
  75. in Pandas. However, this often lead to very
  76. ugly tables as SMTP is poor at handling CSS
  77. or styling in general. Here is a comparison
  78. of using ``df.to_html()`` directly vs embedding
  79. via Red Mail:
  80. |pic1| vs |pic2|
  81. .. |pic1| image:: /imgs/table_without_style.png
  82. :height: 150px
  83. :align: top
  84. .. |pic2| image:: /imgs/table_with_style.png
  85. :height: 150px
  86. :align: top
  87. To embed tables, you can simply pass them
  88. to the send function as Pandas dataframes:
  89. .. code-block:: python
  90. # Creating a simple dataframe
  91. import pandas as pd
  92. df = pd.DataFrame({
  93. 'nums': [1,2,3],
  94. 'strings': ['yes', 'no', 'yes'],
  95. })
  96. # Let Red Mail to render the dataframe for you:
  97. email.send(
  98. subject='A prettified table',
  99. receivers=['first.last@example.com'],
  100. html="<h1>This is a table:</h1> {{ mytable }}",
  101. body_tables={
  102. 'mytable': df,
  103. }
  104. )
  105. Red Mail uses Jinja and inline HTML styling to make the
  106. tables look nice. Email servers typically don't handle
  107. well CSS.
  108. .. warning::
  109. Red Email Pandas templating should work on various
  110. dataframe strucutres (empty, multi-indexed etc.) but
  111. sometimes the rendering may be off if the dataframe
  112. is especially complex in structural sense. There are
  113. plans to make it even more better.
  114. You may also override the template paths (see
  115. :ref:`templating`) to create custom templates
  116. if you wish to make your own table prettifying:
  117. .. code-block:: python
  118. email.set_template_paths(
  119. html_table="path/to/templates",
  120. text_template="path/to/templates"
  121. )
  122. email.default_html_theme = "my_table_template.html"
  123. email.default_text_theme = "my_table_template.txt"
  124. The templates get parameter ``df`` which is the dataframe
  125. to be prettified.