body_content.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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| -- |pic2|
  20. .. |pic1| image:: /imgs/table_unrendered.png
  21. :height: 150px
  22. :align: top
  23. .. |pic2| image:: /imgs/table_rendered.png
  24. :height: 150px
  25. :align: top
  26. To embed tables, you can si mply 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. development plans to make it even more better.
  53. .. _embedding-images:
  54. Embedded Images
  55. ---------------
  56. You can also embed images straight to the HTML body
  57. of the email:
  58. .. code-block:: python
  59. email.send(
  60. subject='Some attachments',
  61. receivers=['first.last@example.com'],
  62. html="<h1>This is an image:</h1> {{ myimage }}",
  63. body_images={
  64. 'myimage': 'path/to/image.png',
  65. }
  66. )
  67. The image will be rendered as ``<img src="cid:...">``.
  68. In case you need to control the image (like the size)
  69. you can also create the ``img`` tag yourself:
  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> <img src="{{ myimage.src }}">',
  75. body_images={
  76. 'myimage': 'path/to/image.png',
  77. }
  78. )
  79. In addition to paths as strings, the following are supported:
  80. - ``pathlib.Path``
  81. - ``bytes`` (the image as raw bytes)
  82. - ``matplotlib.pyplot.Figure``
  83. - ``PIL.Image``