This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: smtplib does not support proxy
Type: behavior Stage: resolved
Components: Library (Lib) Versions:
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, rares, ryanhiebert
Priority: normal Keywords:

Created on 2017-03-01 17:29 by rares, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg288765 - (view) Author: Rares Vernica (rares) Date: 2017-03-01 17:29
smtplib does not support connections through a proxy. The accepted workaround is something like:

import smtplib
import socks

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, proxy_host, proxy_port)

smtp = smtplib.SMTP()

The side-effects of `socks.wrapmodule` impact other libraries which don't need to use the proxy, like `requests`. See here for a disucssion
msg388947 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-03-17 17:24
The Python standard library has no builtin support for socks proxy. I suggest that you report issues with socks library to the author of the package.

By the way the smptlib makes it really easy to override the socket object with a custom implementation:

class SocksSMTP(smtplib.SMTP):
    def _get_socket(self, host, port, timeout):
        return some_socket_like_object(...)
msg388958 - (view) Author: Ryan Hiebert (ryanhiebert) * Date: 2021-03-17 18:51
Thank you, Christian. It sounds like you believe that we should view the `_get_socket` method as a public interface? That at least makes it possible to use a proxy socket through an appropriate mechanism, which solves my use-case.
msg388960 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-03-17 19:01
It's not a public API but it's a stable API. It hasn't changed since Python 2.6 and commit 366d6262f81 from 2007. It's unlikely to change in the near future.
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73873
2021-03-17 19:01:48christian.heimessetmessages: + msg388960
2021-03-17 18:51:03ryanhiebertsetmessages: + msg388958
2021-03-17 17:24:26christian.heimessetstatus: open -> closed

type: behavior

nosy: + christian.heimes
messages: + msg388947
resolution: third party
stage: resolved
2021-03-17 14:59:47ryanhiebertsetnosy: + ryanhiebert
2017-03-01 17:29:32rarescreate