| 1 |
.. py:currentmodule:: aiosmtplib |
| 2 |
|
| 3 |
The send Coroutine |
| 4 |
================== |
| 5 |
|
| 6 |
The :func:`send` coroutine is the main entry point for sending email, and is |
| 7 |
recommended for most use cases. If you need more direct control over connection, |
| 8 |
disconnection, etc, consider instantiating an :class:`SMTP` object directly. |
| 9 |
|
| 10 |
Sending Messages |
| 11 |
---------------- |
| 12 |
|
| 13 |
To send a message, create an :py:class:`email.message.EmailMessage` object, set |
| 14 |
appropriate headers (``From``, and one of ``To``, ``Cc`` or ``Bcc``, at minimum), |
| 15 |
then pass it to :func:`send` with the hostname and port of an SMTP server. |
| 16 |
|
| 17 |
For details on creating :py:class:`email.message.EmailMessage` objects, see |
| 18 |
`the stdlib documentation examples |
| 19 |
<https://docs.python.org/3.12/library/email.examples.html>`_. |
| 20 |
|
| 21 |
.. caution:: Confusingly, :py:class:`email.message.Message` objects are part of the |
| 22 |
legacy email API (prior to Python 3.3), while :py:class:`email.message.EmailMessage` |
| 23 |
objects support email policies other than the older :py:class:`email.policy.Compat32`. |
| 24 |
|
| 25 |
Use :py:class:`email.message.EmailMessage` where possible; it makes headers easier to |
| 26 |
work with. |
| 27 |
|
| 28 |
.. testcode:: |
| 29 |
|
| 30 |
import asyncio |
| 31 |
from email.message import EmailMessage |
| 32 |
|
| 33 |
import aiosmtplib |
| 34 |
|
| 35 |
async def send_hello_world(): |
| 36 |
message = EmailMessage() |
| 37 |
message["From"] = "root@localhost" |
| 38 |
message["To"] = "[email protected]" |
| 39 |
message["Subject"] = "Hello World!" |
| 40 |
message.set_content("Sent via aiosmtplib") |
| 41 |
|
| 42 |
await aiosmtplib.send(message, hostname="127.0.0.1", port=1025) |
| 43 |
|
| 44 |
asyncio.run(send_hello_world()) |
| 45 |
|
| 46 |
|
| 47 |
Multipart Messages |
| 48 |
------------------ |
| 49 |
|
| 50 |
Pass :py:class:`email.mime.multipart.MIMEMultipart` objects to :func:`send` to |
| 51 |
send messages with both HTML text and plain text alternatives. |
| 52 |
|
| 53 |
.. testcode:: |
| 54 |
|
| 55 |
from email.mime.multipart import MIMEMultipart |
| 56 |
from email.mime.text import MIMEText |
| 57 |
|
| 58 |
|
| 59 |
message = MIMEMultipart("alternative") |
| 60 |
message["From"] = "root@localhost" |
| 61 |
message["To"] = "[email protected]" |
| 62 |
message["Subject"] = "Hello World!" |
| 63 |
|
| 64 |
plain_text_message = MIMEText("Sent via aiosmtplib", "plain", "utf-8") |
| 65 |
html_message = MIMEText( |
| 66 |
"<html><body><h1>Sent via aiosmtplib</h1></body></html>", "html", "utf-8" |
| 67 |
) |
| 68 |
message.attach(plain_text_message) |
| 69 |
message.attach(html_message) |
| 70 |
|
| 71 |
|
| 72 |
Sending Raw Messages |
| 73 |
-------------------- |
| 74 |
|
| 75 |
You can also send a ``str`` or ``bytes`` message, by providing the ``sender`` |
| 76 |
and ``recipients`` keyword arguments. Note that you must provide any headers |
| 77 |
as part of the message text. |
| 78 |
|
| 79 |
.. testcode:: |
| 80 |
|
| 81 |
import asyncio |
| 82 |
|
| 83 |
import aiosmtplib |
| 84 |
|
| 85 |
async def send_hello_world(): |
| 86 |
message = """To: [email protected] |
| 87 |
From: root@localhost |
| 88 |
Subject: Hello World! |
| 89 |
|
| 90 |
Sent via aiosmtplib |
| 91 |
""" |
| 92 |
|
| 93 |
await aiosmtplib.send( |
| 94 |
message, |
| 95 |
sender="root@localhost", |
| 96 |
recipients=["[email protected]"], |
| 97 |
hostname="127.0.0.1", |
| 98 |
port=1025 |
| 99 |
) |
| 100 |
|
| 101 |
asyncio.run(send_hello_world()) |
| 102 |
|
| 103 |
|
| 104 |
Connecting Over TLS/SSL |
| 105 |
----------------------- |
| 106 |
|
| 107 |
.. seealso:: For details on different connection types, see :ref:`connection-types`. |
| 108 |
|
| 109 |
If an SMTP server supports direct connection via TLS/SSL, pass |
| 110 |
``use_tls=True``. |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
await send(message, hostname="smtp.gmail.com", port=465, use_tls=True) |
| 115 |
|
| 116 |
|
| 117 |
STARTTLS connections |
| 118 |
~~~~~~~~~~~~~~~~~~~~ |
| 119 |
|
| 120 |
By default, if the server advertises STARTTLS support, aiosmtplib will |
| 121 |
upgrade the connection automatically. Setting ``use_tls=True`` for STARTTLS |
| 122 |
servers will typically result in a connection error. |
| 123 |
|
| 124 |
To opt out of STARTTLS on connect, pass ``start_tls=False``. |
| 125 |
|
| 126 |
.. code-block:: python |
| 127 |
|
| 128 |
await send(message, hostname="smtp.gmail.com", port=587, start_tls=False) |
| 129 |
|
| 130 |
|
| 131 |
Authentication |
| 132 |
-------------- |
| 133 |
|
| 134 |
To authenticate with a username and password, pass the ``username`` and |
| 135 |
``password`` keyword arguments to :func:`send`. |
| 136 |
|
| 137 |
.. code-block:: python |
| 138 |
|
| 139 |
await send( |
| 140 |
message, |
| 141 |
hostname="smtp.gmail.com", |
| 142 |
port=587, |
| 143 |
username="[email protected]", |
| 144 |
password="test" |
| 145 |
) |
| 146 |
|
| 147 |
.. seealso:: For OAuth2 authentication (required by Gmail, Outlook, and other |
| 148 |
providers), see :ref:`oauth`. |