| 1 |
.. py:currentmodule:: aiosmtplib |
| 2 |
|
| 3 |
The SMTP Client Class |
| 4 |
===================== |
| 5 |
|
| 6 |
Use the :class:`SMTP` class as a client directly when you want more control |
| 7 |
over the email sending process than the :func:`send` async function provides. |
| 8 |
|
| 9 |
|
| 10 |
Connecting to an SMTP Server |
| 11 |
---------------------------- |
| 12 |
|
| 13 |
Initialize a new :class:`SMTP` instance, then await its :meth:`SMTP.connect` |
| 14 |
coroutine. Initializing an instance does not automatically connect to the |
| 15 |
server, as that is a blocking operation. |
| 16 |
|
| 17 |
.. testcode:: |
| 18 |
|
| 19 |
import asyncio |
| 20 |
|
| 21 |
from aiosmtplib import SMTP |
| 22 |
|
| 23 |
|
| 24 |
client = SMTP() |
| 25 |
asyncio.run(client.connect(hostname="127.0.0.1", port=1025)) |
| 26 |
|
| 27 |
|
| 28 |
Connecting over TLS/SSL |
| 29 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 30 |
|
| 31 |
.. seealso:: For details on different connection types, see :ref:`connection-types`. |
| 32 |
|
| 33 |
If an SMTP server supports direct connection via TLS/SSL, pass ``use_tls=True`` |
| 34 |
when initializing the SMTP instance (or when calling :meth:`SMTP.connect`). |
| 35 |
|
| 36 |
.. code-block:: python |
| 37 |
|
| 38 |
smtp_client = aiosmtplib.SMTP(hostname="smtp.gmail.com", port=465, use_tls=True) |
| 39 |
await smtp_client.connect() |
| 40 |
|
| 41 |
|
| 42 |
STARTTLS connections |
| 43 |
~~~~~~~~~~~~~~~~~~~~ |
| 44 |
|
| 45 |
.. seealso:: For details on different connection types, see :ref:`connection-types`. |
| 46 |
|
| 47 |
By default, if the server advertises STARTTLS support, aiosmtplib will |
| 48 |
upgrade the connection automatically. Setting ``use_tls=True`` for STARTTLS |
| 49 |
servers will typically result in a connection error. |
| 50 |
|
| 51 |
To opt out of STARTTLS on connect, pass ``start_tls=False``. You may then |
| 52 |
manually call :meth:`SMTP.starttls` if needed. |
| 53 |
|
| 54 |
.. code-block:: python |
| 55 |
|
| 56 |
smtp_client = aiosmtplib.SMTP( |
| 57 |
hostname="smtp.gmail.com", |
| 58 |
port=587, |
| 59 |
start_tls=False, |
| 60 |
use_tls=False, |
| 61 |
) |
| 62 |
await smtp_client.connect() |
| 63 |
await smtp_client.starttls() |
| 64 |
|
| 65 |
|
| 66 |
Connecting via Async Context Manager |
| 67 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 68 |
|
| 69 |
Instances of the :class:`SMTP` class can also be used as an async context |
| 70 |
manager, which will automatically connect/disconnect on entry/exit. |
| 71 |
|
| 72 |
.. testcode:: |
| 73 |
|
| 74 |
import asyncio |
| 75 |
from email.message import EmailMessage |
| 76 |
|
| 77 |
from aiosmtplib import SMTP |
| 78 |
|
| 79 |
|
| 80 |
async def say_hello(): |
| 81 |
message = EmailMessage() |
| 82 |
message["From"] = "root@localhost" |
| 83 |
message["To"] = "[email protected]" |
| 84 |
message["Subject"] = "Hello World!" |
| 85 |
message.set_content("Sent via aiosmtplib") |
| 86 |
|
| 87 |
smtp_client = SMTP(hostname="127.0.0.1", port=1025) |
| 88 |
async with smtp_client: |
| 89 |
await smtp_client.send_message(message) |
| 90 |
|
| 91 |
asyncio.run(say_hello()) |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
Sending Messages |
| 96 |
---------------- |
| 97 |
|
| 98 |
:meth:`SMTP.send_message` |
| 99 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 |
|
| 101 |
Use this method to send :py:class:`email.message.EmailMessage` objects, including |
| 102 |
:py:mod:`email.mime` subclasses such as :py:class:`email.mime.text.MIMEText`. |
| 103 |
|
| 104 |
For details on creating :py:class:`email.message.EmailMessage` objects, see `the |
| 105 |
stdlib documentation examples |
| 106 |
<https://docs.python.org/3.11/library/email.examples.html>`_. |
| 107 |
|
| 108 |
.. testcode:: |
| 109 |
|
| 110 |
import asyncio |
| 111 |
from email.mime.text import MIMEText |
| 112 |
|
| 113 |
from aiosmtplib import SMTP |
| 114 |
|
| 115 |
|
| 116 |
mime_message = MIMEText("Sent via aiosmtplib") |
| 117 |
mime_message["From"] = "root@localhost" |
| 118 |
mime_message["To"] = "[email protected]" |
| 119 |
mime_message["Subject"] = "Hello World!" |
| 120 |
|
| 121 |
async def send_with_send_message(message): |
| 122 |
smtp_client = SMTP(hostname="127.0.0.1", port=1025) |
| 123 |
await smtp_client.connect() |
| 124 |
await smtp_client.send_message(message) |
| 125 |
await smtp_client.quit() |
| 126 |
|
| 127 |
asyncio.run(send_with_send_message(mime_message)) |
| 128 |
|
| 129 |
|
| 130 |
Pass :py:class:`email.mime.multipart.MIMEMultipart` objects to |
| 131 |
:meth:`SMTP.send_message` to send messages with both HTML text and plain text |
| 132 |
alternatives. |
| 133 |
|
| 134 |
.. testcode:: |
| 135 |
|
| 136 |
from email.mime.multipart import MIMEMultipart |
| 137 |
from email.mime.text import MIMEText |
| 138 |
|
| 139 |
message = MIMEMultipart("alternative") |
| 140 |
message["From"] = "root@localhost" |
| 141 |
message["To"] = "[email protected]" |
| 142 |
message["Subject"] = "Hello World!" |
| 143 |
|
| 144 |
message.attach(MIMEText("hello", "plain", "utf-8")) |
| 145 |
message.attach(MIMEText("<html><body><h1>Hello</h1></body></html>", "html", "utf-8")) |
| 146 |
|
| 147 |
async def send_multipart_message(message): |
| 148 |
smtp_client = SMTP(hostname="127.0.0.1", port=1025) |
| 149 |
await smtp_client.connect() |
| 150 |
await smtp_client.send_message(message) |
| 151 |
await smtp_client.quit() |
| 152 |
|
| 153 |
asyncio.run(send_multipart_message(message)) |
| 154 |
|
| 155 |
|
| 156 |
:meth:`SMTP.sendmail` |
| 157 |
~~~~~~~~~~~~~~~~~~~~~ |
| 158 |
|
| 159 |
Use :meth:`SMTP.sendmail` to send raw messages. Note that when using this |
| 160 |
method, you must format the message headers yourself. |
| 161 |
|
| 162 |
.. testcode:: |
| 163 |
|
| 164 |
import asyncio |
| 165 |
|
| 166 |
from aiosmtplib import SMTP |
| 167 |
|
| 168 |
|
| 169 |
sender = "root@localhost" |
| 170 |
recipients = ["[email protected]"] |
| 171 |
message = """To: [email protected] |
| 172 |
From: root@localhost |
| 173 |
Subject: Hello World! |
| 174 |
|
| 175 |
Sent via aiosmtplib |
| 176 |
""" |
| 177 |
|
| 178 |
async def send_with_sendmail(): |
| 179 |
smtp_client = SMTP(hostname="127.0.0.1", port=1025) |
| 180 |
await smtp_client.connect() |
| 181 |
await smtp_client.sendmail(sender, recipients, message) |
| 182 |
await smtp_client.quit() |
| 183 |
|
| 184 |
asyncio.run(send_with_sendmail()) |
| 185 |
|
| 186 |
|
| 187 |
Parallel Execution |
| 188 |
------------------ |
| 189 |
|
| 190 |
SMTP is a sequential protocol. Multiple commands must be sent to send an email, |
| 191 |
and they must be sent in the correct sequence. As a consequence of this, |
| 192 |
executing multiple :meth:`SMTP.send_message` tasks in parallel (i.e. with |
| 193 |
:py:func:`asyncio.gather`) is not any more efficient than executing in |
| 194 |
sequence, as the client must wait until one mail is sent before beginning the |
| 195 |
next. |
| 196 |
|
| 197 |
If you have a lot of emails to send, consider creating multiple connections |
| 198 |
(:class:`SMTP` instances) and splitting the work between them. |