index.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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'] = 'me@example.com'
  34. msg['To'] = 'you@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. sender="me@example.com",
  50. receivers=['you@example.com'],
  51. text="Hello!",
  52. html="<h1>Hello!</h1>"
  53. )
  54. Here are also other reasons to use Red Mail:
  55. - :ref:`You can put attachments to the email <attachments>`
  56. - :ref:`You can include images to the body <embedding-images>`
  57. - :ref:`You can render nicer tables to the body <embedding-tables>`
  58. - :ref:`It has Jinja support <jinja-support>`
  59. - :ref:`You can reuse your HTML templates <templating>`
  60. - :ref:`Gmail pre-configured <config-gmail>`
  61. - :ref:`Send with cc and bcc <send-cc-bcc>`
  62. In addition, normally tables in emails look like this:
  63. .. image:: /imgs/table_without_style.png
  64. :width: 200px
  65. Red Email styles them like this:
  66. .. image:: /imgs/table_with_style.png
  67. :width: 200px
  68. More Examples
  69. -------------
  70. You can include attachments with your email:
  71. .. code-block:: python
  72. from pathlib import Path
  73. import pandas as pd
  74. email.send(
  75. subject="Email subject",
  76. sender="me@example.com",
  77. receivers=["you@example.com"],
  78. text="Hi, this is a simple email.",
  79. attachments={
  80. 'myfile.csv': Path("path/to/data.csv"),
  81. 'myfile.xlsx': pd.DataFrame({'A': [1, 2, 3]}),
  82. 'myfile.html': '<h1>This is content of an attachment</h1>'
  83. }
  84. )
  85. You can also embed images to the HTML body:
  86. .. code-block:: python
  87. import pandas as pd
  88. email.send(
  89. subject="Email subject",
  90. sender="me@example.com",
  91. receivers=["you@example.com"],
  92. html="""
  93. <h1>Hi,</h1>
  94. <p>have you seen this?</p>
  95. {{ myimg }}
  96. """,
  97. body_images={"myimg": "path/to/my/image.png"}
  98. )
  99. You can also embed prettified tables to the body (using Pandas):
  100. .. code-block:: python
  101. import pandas as pd
  102. email.send(
  103. subject="Email subject",
  104. sender="me@example.com",
  105. receivers=["you@example.com"],
  106. html="""
  107. <h1>Hi,</h1>
  108. <p>have you seen this?</p>
  109. {{ mytable }}
  110. """,
  111. body_tables={"mytable": pd.DataFrame({'a': [1,2,3], 'b': [1,2,3]})}
  112. )
  113. You can also parametrize and template the bodies with Jinja:
  114. .. code-block:: python
  115. import pandas as pd
  116. email.send(
  117. subject="Email subject",
  118. sender="me@example.com",
  119. receivers=["you@example.com"],
  120. text="Hi {{ friend }}, nice to meet you.",
  121. html="<h1>Hi {{ friend }}, nice to meet you</h1>",
  122. body_params={
  123. "friend": "Jack"
  124. }
  125. )
  126. Interested?
  127. -----------
  128. There is much more to offer. Install the package:
  129. .. code-block:: console
  130. pip install redmail
  131. and read further. Here are some example use cases:
  132. - :ref:`cookbook-campaign`
  133. - :ref:`cookbook-alerts`
  134. - :ref:`cookbook-stats`
  135. - :ref:`examples-mega`
  136. .. toctree::
  137. :maxdepth: 2
  138. :caption: Contents:
  139. tutorials/index
  140. references
  141. versions
  142. Indices and tables
  143. ==================
  144. * :ref:`genindex`