index.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. - :ref:`Send with cc and bcc <send-cc-bcc>`
  52. In addition, normally tables in emails look like this:
  53. .. image:: /imgs/table_without_style.png
  54. :width: 200px
  55. Red Email styles them like this:
  56. .. image:: /imgs/table_with_style.png
  57. :width: 200px
  58. More Examples
  59. -------------
  60. You could attach things to your email:
  61. .. code-block:: python
  62. from pathlib import Path
  63. import pandas as pd
  64. email.send(
  65. subject="Email subject",
  66. receivers=["me@gmail.com"],
  67. text_body="Hi, this is a simple email.",
  68. attachments={
  69. 'myfile.csv': Path("path/to/data.csv"),
  70. 'myfile.xlsx': pd.DataFrame({'A': [1, 2, 3]}),
  71. 'myfile.html': '<h1>This is content of an attachment</h1>'
  72. }
  73. )
  74. By default, HTML tables in emails look ugly. Red Mail has premade templates
  75. to turn them more visually pleasing. Just include them as Jinja parameters
  76. in body and pass them as Pandas dataframes:
  77. .. code-block:: python
  78. import pandas as pd
  79. email.send(
  80. subject="Email subject",
  81. receivers=["me@gmail.com"],
  82. html="""
  83. <h1>Hi,</h1>
  84. <p>have you seen this?</p>
  85. {{ mytable }}
  86. """,
  87. body_tables={"mytable": pd.DataFrame({'a': [1,2,3], 'b': [1,2,3]})}
  88. )
  89. You can also include images similarly:
  90. .. code-block: python
  91. from pathlib import Path
  92. import pandas as pd
  93. email.send(
  94. subject="Email subject",
  95. receivers=["me@gmail.com"],
  96. html="""
  97. <h1>Hi,</h1>
  98. <p>have you seen this?</p>
  99. {{ myimg }}
  100. """,
  101. body_images={"myimg": "path/to/my/image.png"}
  102. )
  103. Interested?
  104. -----------
  105. There is much more to offer. Install the package:
  106. .. code-block:: console
  107. pip install redmail
  108. and read further.
  109. .. toctree::
  110. :maxdepth: 2
  111. :caption: Contents:
  112. tutorials/index
  113. references
  114. Indices and tables
  115. ==================
  116. * :ref:`genindex`