sending.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 email 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. .. note::
  24. Some email providers (such as Gmail) do not allow specifying
  25. sender. For example, Gmail will outright ignore it and always
  26. use your own email address.
  27. Sending Email with Text Body
  28. ----------------------------
  29. To send an email with plain text message:
  30. .. code-block:: python
  31. email.send(
  32. subject='email subject',
  33. sender="me@example.com",
  34. receivers=['you@example.com'],
  35. text="Hi, this is an email."
  36. )
  37. Sending Email with HTML Body
  38. ----------------------------
  39. To send an email with html content:
  40. .. code-block:: python
  41. email.send(
  42. subject='email subject',
  43. sender="me@example.com",
  44. receivers=['you@example.com'],
  45. html="""
  46. <h1>Hi,</h1>
  47. <p>this is an email.</p>
  48. """
  49. )
  50. Sending Email with text and HTML Body
  51. -------------------------------------
  52. You can also include both to your email:
  53. .. code-block:: python
  54. email.send(
  55. subject='email subject',
  56. sender="me@example.com",
  57. receivers=['you@example.com'],
  58. text="Hi, this is an email.",
  59. html="""
  60. <h1>Hi,</h1>
  61. <p>this is an email.</p>
  62. """
  63. )
  64. .. _send-cc-bcc:
  65. Sending Email with cc and bcc
  66. -----------------------------
  67. You can also include carbon copy (cc) and blind carbon copy (bcc)
  68. to your emails:
  69. .. code-block:: python
  70. email.send(
  71. subject='email subject',
  72. sender="me@example.com",
  73. receivers=['you@example.com'],
  74. cc=['also@example.com'],
  75. bcc=['outsider@example.com']
  76. )
  77. .. _send-alias:
  78. Sending Email with Alias
  79. ------------------------
  80. You can also alias the sender and receivers:
  81. .. code-block:: python
  82. email.send(
  83. subject='email subject',
  84. sender="The Sender <me@example.com>",
  85. receivers=['The Receiver <you@example.com>']
  86. )
  87. Alias is an alternative text that is displayed instead of
  88. the actual email addresses. The receivers can still get
  89. the addresses though.
  90. .. _send-multi:
  91. Sending Multiple Emails
  92. -----------------------
  93. Normally Red Mail opens and closes the connection to the SMTP
  94. server when sending each email. If you are sending large amount
  95. of emails it may be beneficial to leave the connection open:
  96. .. code-block:: python
  97. with email:
  98. email.send(
  99. subject='email subject',
  100. sender="me@example.com",
  101. receivers=['you@example.com']
  102. )
  103. email.send(
  104. subject='email subject',
  105. sender="me@example.com",
  106. receivers=['they@example.com']
  107. )
  108. ...
  109. Alternatively, you may use the ``connect`` and ``close``
  110. methods:
  111. .. code-block:: python
  112. try:
  113. email.connect()
  114. email.send(
  115. subject='email subject',
  116. sender="me@example.com",
  117. receivers=['you@example.com']
  118. )
  119. email.send(
  120. subject='email subject',
  121. sender="me@example.com",
  122. receivers=['they@example.com']
  123. )
  124. ...
  125. finally:
  126. email.close()