example.rst 4.7 KB

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