Python smtplib 代理支持
我想通过代理发送电子邮件。
我当前的实现如下:
我通过身份验证连接到 smtp 服务器。成功登录后,我会发送一封电子邮件。它工作正常,但当我查看电子邮件标题时,我可以看到我的主机名。我想通过代理来传输它。
任何帮助将不胜感激。
I would like to send email through a proxy.
My current implementation is as follows:
I connect to the smtp server with authentication. After I've successfully logged in, I send an email. It works fine but when I look at the email header I can see my host name. I would like to tunnel it through a proxy instead.
Any help will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用SocksiPy:
Use SocksiPy:
我昨天也遇到了类似的问题,这是我为解决问题而编写的代码。它无形地允许您通过代理使用所有 smtp 方法。
I had a similar problem yesterday, this is the code I wrote to solve the problem. It invisibly allows you to use all of the smtp methods via proxy.
一种更简单的方法,仅修补
smtplib
:A much simpler approach which patches
smtplib
only:正如 mkerrig 和 Denis Cornehl 在另一个答案的评论中指出的那样,使用 smtplib 中修改后的 SMTP 类的 PySocks create_connection 可以工作,而无需为所有内容进行 Monkeypatch 套接字。
我仍然讨厌这个实现(谁知道其他版本的 python 或 smtplib 会破坏什么),但现在可以使用(3.8.1)。由于我无法在互联网上的其他地方找到任何其他有效的解决方案,因此我尝试:
my_proxy_smtplib.py:
并使用:
As mkerrig and Denis Cornehl noted in a comment on another answer PySocks create_connection with a modified SMTP class from smtplib works without having to monkeypatch sockets for everything.
I still hate this implementation (who know what will break with other version of python or smtplib), but this works for now (3.8.1). Since I was unable to find any other solutions elsewhere on the internet that worked, here is my attempt:
my_proxy_smtplib.py:
And to use:
这段代码是我赚来的。
1. 文件名不能是email.py 重命名文件名,例如emailSend.py
2.需要允许Google发送来自不可靠来源的消息。
This code has earned from me.
1. The file name must not be email.py Rename file name for example emailSend.py
2. It is necessary to allow Google to send messages from unreliable sources.
对于那些仍然需要它的人:)我已经使用 Python3 和 PySocks 制作了一个工作解决方案:
For those who still need it :) I've made a working solution with Python3 and PySocks:
我不久前遇到了这个问题,这是我解决的方法。此示例使用 Tor 作为ocks5 代理。
I had this issue a while ago, here is how I solved it. This example uses Tor as socks5 proxy.
smtplib
模块不包含通过 HTTP 代理连接到 SMTP 服务器的功能。 ryoh 发布的自定义类 对我不起作用,显然是因为我的 HTTP 代理仅接收编码消息。我根据ryos的代码编写了以下自定义类,并且运行良好。 (但是,您的情况可能会有所不同。)要连接到 SMTP 服务器,只需使用类
ProxySMTP
而不是smtplib.SMTP
。The
smtplib
module doesn't include the functionality to connect to a SMTP server through a HTTP proxy. The custom class posted by ryoh didn't work for me, apparently because my HTTP proxy receives encoded messages only. I wrote the following custom class based on ryos's code, and it worked fine. (Your mileage, however, may vary.)To connect to the SMTP server, just use the class
ProxySMTP
instead ofsmtplib.SMTP
.我尝试了很多方法,但我发现Nginx SMTP代理是更好的一种,不需要打猴子补丁,你只需要在与你的私有网络连接的互联网机器上安装Nginx,Nginx配置就是这样。
参考: https://docs.nginx.com/nginx /admin-guide/mail-proxy/mail-proxy/
I have tried many methods, but I found Nginx SMTP proxy is a better one, it is not necessary do the monkey patch, you just need to install Nginx on an internet machine connect with your private network, the Nginx Configuration is just like this.
ref: https://docs.nginx.com/nginx/admin-guide/mail-proxy/mail-proxy/