cookbook.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. .. _cookbook:
  2. Cookbook
  3. =========
  4. This section provides various examples for various
  5. needs.
  6. .. _cookbook-campaign:
  7. Email Campaign
  8. --------------
  9. In case you have a list of clients or customers you
  10. wish to send personalized emails, you may benefit from
  11. templating. It may help to make the templates to an HTML
  12. file, polish them and after then send:
  13. .. code-block:: python
  14. from redmail import EmailSender
  15. email = EmailSender(...)
  16. email.receivers = ['we@example.com']
  17. email.set_template_paths(
  18. html="path/to/campaigns"
  19. )
  20. Then make a HTML file, for example ``path/to/campaigns/summer_sale.html``:
  21. .. code-block:: html
  22. <h1>Thank you, {{ customer }}, for being awesome!</h1>
  23. <p>
  24. We are pleased to inform you that we have a lot of products
  25. in huge discounts.
  26. </p>
  27. <ul>
  28. {% for product, discount, in discounts.items() %}
  29. <li>{{ product }}: {{ '{:.0f} %'.format(discount * 100) }}</li>
  30. {% endfor %}
  31. </ul>
  32. <p>Kind regards, We Ltd.</p>
  33. Finally send the emails:
  34. .. code-block:: python
  35. discounts = {'shoes': 0.2, 'shirts': 0.4}
  36. customers = ['cust1@example.com', 'cust2@example.com', ...]
  37. for customer in customers:
  38. email.send(
  39. subject="Summer Sale!",
  40. html_template="summer_sale.html",
  41. body_params={
  42. "customer": customer,
  43. "discounts": discounts
  44. }
  45. )
  46. .. _cookbook-alerts:
  47. Error Alerts
  48. ------------
  49. If you are building long running program (ie. web app) you can make a
  50. templated error alerts that include the full traceback:
  51. .. code-block:: python
  52. from redmail import EmailSender
  53. error_email = EmailSender(...)
  54. error_email.sender = 'me@example.com'
  55. error_email.receivers = ['me@example.com']
  56. error_email.html = """
  57. <h2>An error encountered</h2>
  58. {{ error }}
  59. """
  60. try:
  61. raise RuntimeError("Oops")
  62. except:
  63. # Send an email including the traceback
  64. error_email.send(subject="Fail: doing stuff failed")
  65. .. note::
  66. The ``error`` formatting object identifies which body it is being
  67. attached to. If you wish to use text body, ``error`` will show up
  68. similarly as Python errors you see on terminals. See more from
  69. :class:`redmail.models.Error`
  70. .. _cookbook-stats:
  71. Stats Reports
  72. -------------
  73. As demonstrated :ref:`here <embedding-plt>`, embedding Matplotlib
  74. figures to the HTML bodies is trivial. Therefore you can easily
  75. create diagnostic reports or automatic analyses. Just create
  76. the plots and let Red Mail send them to you:
  77. .. code-block:: python
  78. from redmail import EmailSender
  79. stats_report = EmailSender(...)
  80. stats_report.sender = 'no-reply@example.com'
  81. stats_report.receivers = ['me@example.com']
  82. # Create a plot
  83. import matplotlib.pyplot as plt
  84. fig_performance = plt.Figure()
  85. plt.plot([1,2,3,2,3])
  86. # Create summary table
  87. import pandas as pd
  88. df = pd.DataFrame(...)
  89. df_summary = df.describe()
  90. # Send the report
  91. stats_report.send(
  92. subject="System Diagnostics",
  93. html="""
  94. <h1>System Diagnostics ({{ now }})</h1>
  95. <hr>
  96. <h2>Performance</h2>
  97. {{ perf_plot }}
  98. <h2>Summary Statistics</h2>
  99. {{ tbl_summary }}
  100. <hr>
  101. <p>System running on {{ node }}</p>
  102. """,
  103. body_images={
  104. "perf_plot": fig_performance,
  105. },
  106. body_tables={
  107. "tbl_summary": df_summary
  108. }
  109. )