index.rst 3.8 KB

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