1
0

body_content.rst 4.0 KB

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