| 1 |
.. py:currentmodule:: aiosmtplib |
| 2 |
|
| 3 |
Proxy Support |
| 4 |
============= |
| 5 |
|
| 6 |
|
| 7 |
SOCKS Proxies |
| 8 |
~~~~~~~~~~~~~ |
| 9 |
|
| 10 |
You can use the `python-socks`_ library to connect to a SOCKS proxy. |
| 11 |
|
| 12 |
Create a socket using the ``proxy.connect`` method, and pass it as the ``sock`` |
| 13 |
argument to the :func:`send` coroutine or :py:class:`SMTP` class. |
| 14 |
|
| 15 |
.. code-block:: python |
| 16 |
|
| 17 |
import ssl |
| 18 |
import asyncio |
| 19 |
import aiosmtplib |
| 20 |
from python_socks.async_.asyncio import Proxy |
| 21 |
|
| 22 |
hello_message = """To: [email protected] |
| 23 |
From: root@localhost |
| 24 |
Subject: Hello World! |
| 25 |
|
| 26 |
Sent via aiosmtplib |
| 27 |
""" |
| 28 |
|
| 29 |
async def send_via_proxy(message): |
| 30 |
proxy = Proxy.from_url('socks5://user:[email protected]:1080') |
| 31 |
|
| 32 |
# `proxy.connect` returns a socket in non-blocking mode |
| 33 |
sock = await proxy.connect(dest_host='example.com', dest_port=443) |
| 34 |
|
| 35 |
|
| 36 |
# Use the socket with aiosmtplib |
| 37 |
await aiosmtplib.send( |
| 38 |
message, |
| 39 |
sender="root@localhost", |
| 40 |
recipients=["[email protected]"], |
| 41 |
sock=sock, |
| 42 |
) |
| 43 |
|
| 44 |
asyncio.run(send_via_proxy(hello_message)) |
| 45 |
|
| 46 |
|
| 47 |
.. _python-socks: https://pypi.org/project/python-socks/ |