1
0

sending.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. .. meta::
  2. :description: Send email in Python.
  3. :keywords: send, email, Python
  4. .. _sending-emails:
  5. Sending Emails
  6. ==============
  7. This section covers the basics of sending emails.
  8. See :ref:`configure` to revise how ``EmailSender``
  9. is configured. At minimum, sending an email requires:
  10. .. code-block:: python
  11. from redmail import EmailSender
  12. email = EmailSender(host='localhost', port=0)
  13. email.send(
  14. subject='email subject',
  15. sender="me@example.com",
  16. receivers=['you@example.com']
  17. )
  18. .. note::
  19. If you don't spesify the ``sender``, the sender is considered to
  20. be ``email.sender``. If ``email.sender`` is also missing, the sender
  21. is then set to be ``email.username``. Ensure that any of these is a
  22. valid email address.
  23. Similar flow of logic applies to most attributes. You can set defaults on the
  24. ``email`` instance which are used in case they are not passed via the
  25. method call ``email.send(...)``.
  26. Here is an example:
  27. .. code-block:: python
  28. email = EmailSender(host='localhost', port=0)
  29. email.subject = "email subject"
  30. email.receivers = ["you@example.com"]
  31. email.send(
  32. sender="me@example.com",
  33. subject="important email"
  34. )
  35. The above sends an email that has ``"important email"`` as the email subject
  36. and the email is sent to address ``you@example.com``.
  37. .. note::
  38. Some email providers (such as Gmail) do not allow specifying
  39. sender. For example, Gmail will outright ignore it and always
  40. use your own email address.
  41. Sending Email with Text Body
  42. ----------------------------
  43. To send an email with plain text message:
  44. .. code-block:: python
  45. email.send(
  46. subject='email subject',
  47. sender="me@example.com",
  48. receivers=['you@example.com'],
  49. text="Hi, this is an email."
  50. )
  51. Sending Email with HTML Body
  52. ----------------------------
  53. To send an email with html content:
  54. .. code-block:: python
  55. email.send(
  56. subject='email subject',
  57. sender="me@example.com",
  58. receivers=['you@example.com'],
  59. html="""
  60. <h1>Hi,</h1>
  61. <p>this is an email.</p>
  62. """
  63. )
  64. Sending Email with text and HTML Body
  65. -------------------------------------
  66. You can also include both to your email:
  67. .. code-block:: python
  68. email.send(
  69. subject='email subject',
  70. sender="me@example.com",
  71. receivers=['you@example.com'],
  72. text="Hi, this is an email.",
  73. html="""
  74. <h1>Hi,</h1>
  75. <p>this is an email.</p>
  76. """
  77. )
  78. .. _send-cc-bcc:
  79. Sending Email with cc and bcc
  80. -----------------------------
  81. You can also include carbon copy (cc) and blind carbon copy (bcc)
  82. to your emails:
  83. .. code-block:: python
  84. email.send(
  85. subject='email subject',
  86. sender="me@example.com",
  87. receivers=['you@example.com'],
  88. cc=['also@example.com'],
  89. bcc=['outsider@example.com']
  90. )
  91. .. _send-alias:
  92. Sending Email with Alias
  93. ------------------------
  94. You can also alias the sender and receivers:
  95. .. code-block:: python
  96. email.send(
  97. subject='email subject',
  98. sender="The Sender <me@example.com>",
  99. receivers=['The Receiver <you@example.com>']
  100. )
  101. Alias is an alternative text that is displayed instead of
  102. the actual email addresses. The receivers can still get
  103. the addresses though.
  104. .. _send-headers:
  105. Sending with Custom Headers
  106. ---------------------------
  107. Sometimes you might want to override or add custom
  108. email headers to your email. For example, you
  109. might want to set a custom date for the email and
  110. set it as important:
  111. .. code-block:: python
  112. import datetime
  113. email.send(
  114. subject='email subject',
  115. sender="The Sender <me@example.com>",
  116. receivers=['you@example.com'],
  117. headers={
  118. "Importance": "high",
  119. "Date": datetime.datetime(2021, 1, 31, 6, 56, 46)
  120. }
  121. )
  122. Read more about email headers from `IANA's website <https://www.iana.org/assignments/message-headers/message-headers.xhtml>`_.
  123. .. note::
  124. Headers passed this way can override the other headers such
  125. as ``From``, ``To``, ``Cc``, ``Bcc``, ``Date`` and ``Message-ID``.
  126. .. _send-multi:
  127. Sending Multiple Emails
  128. -----------------------
  129. Normally Red Mail opens and closes the connection to the SMTP
  130. server when sending each email. If you are sending large amount
  131. of emails it may be beneficial to leave the connection open:
  132. .. code-block:: python
  133. with email:
  134. email.send(
  135. subject='email subject',
  136. sender="me@example.com",
  137. receivers=['you@example.com']
  138. )
  139. email.send(
  140. subject='email subject',
  141. sender="me@example.com",
  142. receivers=['they@example.com']
  143. )
  144. ...
  145. Alternatively, you may use the ``connect`` and ``close``
  146. methods:
  147. .. code-block:: python
  148. try:
  149. email.connect()
  150. email.send(
  151. subject='email subject',
  152. sender="me@example.com",
  153. receivers=['you@example.com']
  154. )
  155. email.send(
  156. subject='email subject',
  157. sender="me@example.com",
  158. receivers=['they@example.com']
  159. )
  160. ...
  161. finally:
  162. email.close()