sending.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. .. _sending-emails:
  2. Sending Emails
  3. ==============
  4. This section covers the basics of sending emails.
  5. See :ref:`configure` to revise how ``EmailSender``
  6. is configured. At minimum, sending an email requires:
  7. .. code-block:: python
  8. from email import EmailSender
  9. email = EmailSender(host='localhost', port=0)
  10. email.send(
  11. subject='email subject',
  12. sender="me@example.com",
  13. receivers=['you@example.com']
  14. )
  15. .. note::
  16. If you don't spesify the ``sender``, the sender is considered to
  17. be ``email.sender``. If ``email.sender`` is also missing, the sender
  18. is then set to be ``email.user_name``. Ensure that any of these is a
  19. valid email address.
  20. .. note::
  21. Some email providers (such as Gmail) do not allow specifying
  22. sender. For example, Gmail will outright ignore it and always
  23. use your own email address.
  24. Sending Email with Text Body
  25. ----------------------------
  26. To send an email with plain text message:
  27. .. code-block:: python
  28. email.send(
  29. subject='email subject',
  30. sender="me@example.com",
  31. receivers=['you@example.com'],
  32. text="Hi, this is an email."
  33. )
  34. Sending Email with HTML Body
  35. ----------------------------
  36. To send an email with html content:
  37. .. code-block:: python
  38. email.send(
  39. subject='email subject',
  40. sender="me@example.com",
  41. receivers=['you@example.com'],
  42. html="""
  43. <h1>Hi,</h1>
  44. <p>this is an email.</p>
  45. """
  46. )
  47. Sending Email with text and HTML Body
  48. -------------------------------------
  49. You can also include both to your email:
  50. .. code-block:: python
  51. email.send(
  52. subject='email subject',
  53. sender="me@example.com",
  54. receivers=['you@example.com'],
  55. text="Hi, this is an email.",
  56. html="""
  57. <h1>Hi,</h1>
  58. <p>this is an email.</p>
  59. """
  60. )
  61. .. _send-cc-bcc:
  62. Sending Email with cc and bcc
  63. -----------------------------
  64. You can also include carbon copy (cc) and blind carbon copy (bcc)
  65. to your emails:
  66. .. code-block:: python
  67. email.send(
  68. subject='email subject',
  69. sender="me@example.com",
  70. receivers=['you@example.com'],
  71. cc=['also@example.com'],
  72. bcc=['outsider@example.com']
  73. )
  74. .. _send-alias:
  75. Sending Email with Alias
  76. ------------------------
  77. You can also alias the sender and receivers:
  78. .. code-block:: python
  79. email.send(
  80. subject='email subject',
  81. sender="The Sender <me@example.com>",
  82. receivers=['The Receiver <you@example.com>']
  83. )
  84. Alias is an alternative text that is displayed instead of
  85. the actual email addresses. The receivers can still get
  86. the addresses though.