index.rst 4.2 KB

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