index.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. .. meta::
  2. :description: Red Mail is an advanced email sender for Python. It is open source and well tested.
  3. :keywords: send, email, Python
  4. :red:`Red` Mail
  5. ===============
  6. .. image:: https://badgen.net/pypi/v/redmail
  7. :target: https://pypi.org/project/redmail/
  8. .. image:: https://badgen.net/pypi/python/redmail
  9. :target: https://pypi.org/project/redmail/
  10. Red Mail is a Python library for sending emails.
  11. It makes sending emails a breeze regardless of if
  12. you need to include HTML, embed images or tables or
  13. attach documents.
  14. Sending emails is a pretty straight forward task.
  15. However, the standard SMTP libraries don't make
  16. it particularly easy especially if you want to
  17. include more than basic text. Red Mail aims to
  18. fix this and it makes sending emails a breeze
  19. regardless of if you need to include attachments,
  20. images or prettier HTML.
  21. Visit the `source code from Github <https://github.com/Miksus/red-mail>`_
  22. or `releases in Pypi page <https://pypi.org/project/redmail/>`_.
  23. Why Red Mail?
  24. -------------
  25. Standard SMTP libraries are not very convenient to use. They let you modify any part of the
  26. message but simply sending emails **SHOULD NOT** look like this:
  27. .. code-block:: python
  28. import smtplib
  29. from email.mime.multipart import MIMEMultipart
  30. from email.mime.text import MIMEText
  31. msg = MIMEMultipart('alternative')
  32. msg['Subject'] = 'An example email'
  33. msg['From'] = 'first.last@gmail.com'
  34. msg['To'] = 'first.last@example.com'
  35. part1 = MIMEText("Hello!", 'plain')
  36. part2 = MIMEText("<h1>Hello!</h1>", 'html')
  37. msg.attach(part1)
  38. msg.attach(part2)
  39. # Send the message via our own SMTP server.
  40. s = smtplib.SMTP('localhost', port=0)
  41. s.send_message(msg)
  42. s.quit()
  43. It should look like this:
  44. .. code-block:: python
  45. from redmail import EmailSender
  46. email = EmailSender(host="localhost", port=0)
  47. email.send(
  48. subject="An example email",
  49. receivers=['first.last@example.com'],
  50. text="Hello!",
  51. html="<h1>Hello!</h1>"
  52. )
  53. Here are also other reasons to use Red Mail:
  54. - :ref:`You can put attachments to the email <attachments>`
  55. - :ref:`You can include images to the body <embedding-images>`
  56. - :ref:`You can render nicer tables to the body <embedding-tables>`
  57. - :ref:`It has Jinja support <jinja-support>`
  58. - :ref:`You can reuse your HTML templates <templating>`
  59. - :ref:`Gmail pre-configured <config-gmail>`
  60. - :ref:`Send with cc and bcc <send-cc-bcc>`
  61. In addition, normally tables in emails look like this:
  62. .. image:: /imgs/table_without_style.png
  63. :width: 200px
  64. Red Email styles them like this:
  65. .. image:: /imgs/table_with_style.png
  66. :width: 200px
  67. More Examples
  68. -------------
  69. You could attach things to your email:
  70. .. code-block:: python
  71. from pathlib import Path
  72. import pandas as pd
  73. email.send(
  74. subject="Email subject",
  75. receivers=["me@gmail.com"],
  76. text_body="Hi, this is a simple email.",
  77. attachments={
  78. 'myfile.csv': Path("path/to/data.csv"),
  79. 'myfile.xlsx': pd.DataFrame({'A': [1, 2, 3]}),
  80. 'myfile.html': '<h1>This is content of an attachment</h1>'
  81. }
  82. )
  83. By default, HTML tables in emails look ugly. Red Mail has premade templates
  84. to turn them more visually pleasing. Just include them as Jinja parameters
  85. in body and pass them as Pandas dataframes:
  86. .. code-block:: python
  87. import pandas as pd
  88. email.send(
  89. subject="Email subject",
  90. receivers=["me@gmail.com"],
  91. html="""
  92. <h1>Hi,</h1>
  93. <p>have you seen this?</p>
  94. {{ mytable }}
  95. """,
  96. body_tables={"mytable": pd.DataFrame({'a': [1,2,3], 'b': [1,2,3]})}
  97. )
  98. You can also include images similarly:
  99. .. code-block: python
  100. from pathlib import Path
  101. import pandas as pd
  102. email.send(
  103. subject="Email subject",
  104. receivers=["me@gmail.com"],
  105. html="""
  106. <h1>Hi,</h1>
  107. <p>have you seen this?</p>
  108. {{ myimg }}
  109. """,
  110. body_images={"myimg": "path/to/my/image.png"}
  111. )
  112. Interested?
  113. -----------
  114. There is much more to offer. Install the package:
  115. .. code-block:: console
  116. pip install redmail
  117. and read further. Here are some example use cases:
  118. - :ref:`cookbook-campaign`
  119. - :ref:`cookbook-alerts`
  120. - :ref:`cookbook-stats`
  121. - :ref:`examples-mega`
  122. .. toctree::
  123. :maxdepth: 2
  124. :caption: Contents:
  125. tutorials/index
  126. references
  127. versions
  128. Indices and tables
  129. ==================
  130. * :ref:`genindex`