example.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. .. _examples-simple:
  2. Examples
  3. ========
  4. Simple Example
  5. --------------
  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. email.send(
  15. subject="An email",
  16. sender="me@example.com",
  17. receivers=['you@example.com'],
  18. test="Hi, this is an email.",
  19. html="<h1>Hi, </h1><p>this is an email.</p>"
  20. )
  21. .. _examples-mega:
  22. Super Example
  23. -------------
  24. This example covers the most interesting
  25. features of Red Mail:
  26. .. code-block:: python
  27. from pathlib import Path
  28. from redmail import EmailSender
  29. import pandas as pd
  30. from PIL import Image
  31. import matplotlib.pyplot as plt
  32. fig = plt.figure()
  33. plt.plot([1, 2, 3])
  34. df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
  35. byte_content = Path("a_file.bin").read_bytes()
  36. email = EmailSender(
  37. host='localhost',
  38. port=0,
  39. user_name='me@example.com',
  40. password='<PASSWORD>'
  41. )
  42. # Send an email
  43. email.send(
  44. subject="A lot of stuff!",
  45. sender="me@example.com",
  46. # Receivers
  47. receivers=["you@example.com"],
  48. cc=['also@example.com'],
  49. bcc=['external@example.com'],
  50. # Bodies
  51. text="""Hi {{ friend }},
  52. This email has a lot of stuff!
  53. Use HTML to view the awesome content.
  54. """,
  55. html="""<h1>Hi {{ friend }},</h1>
  56. <p>This email has a lot of stuff!</p>
  57. <p>Like this image:</p>
  58. {{ my_image }}
  59. <p>or this image:</p>
  60. {{ my_pillow }}
  61. <p>or this plot:</p>
  62. {{ my_plot }}
  63. <p>or this table:</p>
  64. {{ my_table }}
  65. <p>or this loop:</p>
  66. <ul>
  67. {% for value in container %}
  68. {% if value > 5 %}
  69. <li>{{ value }}</li>
  70. {% else %}
  71. <li style="color: red">{{ value }}</li>
  72. {% endif %}
  73. {% endfor %}
  74. </ul>
  75. """,
  76. # Embedded content
  77. body_images={
  78. "my_image": "path/to/image.png",
  79. "my_pillow": Image.new('RGB', (100, 30), color = (73, 109, 137))
  80. "my_plot": fig,
  81. },
  82. body_tables={
  83. "my_table": df,
  84. },
  85. body_params={
  86. "friend": "Jack",
  87. "container": [1, 3, 5, 7, 9],
  88. },
  89. attachments={
  90. "data.csv": df,
  91. "file.txt": "This is file content",
  92. "file.html": Path("path/to/a_file.html"),
  93. "file.bin": byte_content,
  94. }
  95. )