example.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. .. _examples:
  2. Examples
  3. ========
  4. This is a collection of various examples of
  5. sending emails. Remember to initiate the
  6. sender object as:
  7. .. code-block:: python
  8. from redmail import EmailSender
  9. email = EmailSender(
  10. host='localhost',
  11. port=0,
  12. user_name='me@example.com',
  13. password='<PASSWORD>'
  14. )
  15. .. _examples-simple:
  16. Simple Example
  17. --------------
  18. .. code-block:: python
  19. email.send(
  20. subject="An email",
  21. sender="me@example.com",
  22. receivers=['you@example.com'],
  23. test="Hi, this is an email.",
  24. html="<h1>Hi, </h1><p>this is an email.</p>"
  25. )
  26. .. _examples-attachments:
  27. Attachments
  28. -----------
  29. .. code-block:: python
  30. from pathlib import Path
  31. import pandas as pd
  32. email.send(
  33. subject="Email subject",
  34. sender="me@example.com",
  35. receivers=["you@example.com"],
  36. text="Hi, this is a simple email.",
  37. attachments={
  38. 'myfile.csv': Path("path/to/data.csv"),
  39. 'myfile.xlsx': pd.DataFrame({'A': [1, 2, 3]}),
  40. 'myfile.html': '<h1>This is content of an attachment</h1>'
  41. }
  42. )
  43. .. _examples-embed-image:
  44. Embedded Images
  45. ---------------
  46. .. code-block:: python
  47. import pandas as pd
  48. email.send(
  49. subject="Email subject",
  50. sender="me@example.com",
  51. receivers=["you@example.com"],
  52. html="""
  53. <h1>Hi,</h1>
  54. <p>have you seen this?</p>
  55. {{ myimg }}
  56. """,
  57. body_images={"myimg": "path/to/my/image.png"}
  58. )
  59. .. _examples-embed-plot:
  60. Embedded Plots
  61. --------------
  62. .. code-block:: python
  63. import matplotlib.pyplot as plt
  64. fig = plt.figure()
  65. plt.plot([1,2,3,2,3])
  66. email.send(
  67. subject="Email subject",
  68. sender="me@example.com",
  69. receivers=["you@example.com"],
  70. html="""
  71. <h1>Hi,</h1>
  72. <p>have you seen this?</p>
  73. {{ myplot }}
  74. """,
  75. body_images={"myplot": fig}
  76. )
  77. .. _examples-embed-table:
  78. Embedded Tables
  79. ---------------
  80. .. code-block:: python
  81. import pandas as pd
  82. email.send(
  83. subject="Email subject",
  84. sender="me@example.com",
  85. receivers=["you@example.com"],
  86. html="""
  87. <h1>Hi,</h1>
  88. <p>have you seen this?</p>
  89. {{ mytable }}
  90. """,
  91. body_tables={"mytable": pd.DataFrame({'a': [1,2,3], 'b': [1,2,3]})}
  92. )
  93. .. _examples-parametrized:
  94. Parametrization
  95. ---------------
  96. .. code-block:: python
  97. email.send(
  98. subject="Email subject",
  99. sender="me@example.com",
  100. receivers=["you@example.com"],
  101. text="Hi {{ friend }}, nice to meet you.",
  102. html="<h1>Hi {{ friend }}, nice to meet you</h1>",
  103. body_params={
  104. "friend": "Jack"
  105. }
  106. )
  107. .. _examples-mega:
  108. Super Example
  109. -------------
  110. This example covers the most interesting
  111. features of Red Mail:
  112. .. code-block:: python
  113. from pathlib import Path
  114. from redmail import EmailSender
  115. import pandas as pd
  116. from PIL import Image
  117. import matplotlib.pyplot as plt
  118. fig = plt.figure()
  119. plt.plot([1, 2, 3])
  120. df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
  121. byte_content = Path("a_file.bin").read_bytes()
  122. email.send(
  123. subject="A lot of stuff!",
  124. sender="me@example.com",
  125. # Receivers
  126. receivers=["you@example.com"],
  127. cc=['also@example.com'],
  128. bcc=['external@example.com'],
  129. # Bodies
  130. text="""Hi {{ friend }},
  131. This email has a lot of stuff!
  132. Use HTML to view the awesome content.
  133. """,
  134. html="""<h1>Hi {{ friend }},</h1>
  135. <p>This email has a lot of stuff!</p>
  136. <p>Like this image:</p>
  137. {{ my_image }}
  138. <p>or this image:</p>
  139. {{ my_pillow }}
  140. <p>or this plot:</p>
  141. {{ my_plot }}
  142. <p>or this table:</p>
  143. {{ my_table }}
  144. <p>or this loop:</p>
  145. <ul>
  146. {% for value in container %}
  147. {% if value > 5 %}
  148. <li>{{ value }}</li>
  149. {% else %}
  150. <li style="color: red">{{ value }}</li>
  151. {% endif %}
  152. {% endfor %}
  153. </ul>
  154. """,
  155. # Embedded content
  156. body_images={
  157. "my_image": "path/to/image.png",
  158. "my_pillow": Image.new('RGB', (100, 30), color = (73, 109, 137))
  159. "my_plot": fig,
  160. },
  161. body_tables={
  162. "my_table": df,
  163. },
  164. body_params={
  165. "friend": "Jack",
  166. "container": [1, 3, 5, 7, 9],
  167. },
  168. attachments={
  169. "data.csv": df,
  170. "file.txt": "This is file content",
  171. "file.html": Path("path/to/a_file.html"),
  172. "file.bin": byte_content,
  173. }
  174. )