Python send email behind a proxy server

2020-02-12 08:50发布

I want to send an email using python script via hotmail smtp but I'm connected to a proxy server.

There is my code , it works when it's connected directely to internet but wen it's connected to a proxy server he doesn't work.

import smtplib

smtpserver = 'smtp.live.com'
AUTHREQUIRED = 1 
smtpuser = 'example@hotmail.fr'  
smtppass = 'mypassword'  

RECIPIENTS = 'mailto@gmail.com'
SENDER = 'example@hotmail.fr'
mssg = "test message"
s = mssg   

server = smtplib.SMTP(smtpserver,587)
server.ehlo()
server.starttls() 
server.ehlo()
server.login(smtpuser,smtppass)
server.set_debuglevel(1)
server.sendmail(SENDER, [RECIPIENTS], s)
server.quit()

标签: python proxy
3条回答
仙女界的扛把子
2楼-- · 2020-02-12 09:16

if your proxy is a http proxy you should use:

socks.setdefaultproxy(socks.HTTP, 'proxy.proxy.com', 8080)

instead of socks.SOCKS5

查看更多
Fickle 薄情
3楼-- · 2020-02-12 09:33

You can accomplish this with a module called SocksiPy or PySocks, the currently maintained fork:

import smtplib
import socks

#socks.setdefaultproxy(TYPE, ADDR, PORT)
socks.setdefaultproxy(socks.SOCKS5, 'proxy.proxy.com', 8080)
socks.wrapmodule(smtplib)

smtpserver = 'smtp.live.com'
AUTHREQUIRED = 1 
smtpuser = 'example@hotmail.fr'  
smtppass = 'mypassword'  

RECIPIENTS = 'mailto@gmail.com'
SENDER = 'example@hotmail.fr'
mssg = "test message"
s = mssg   

server = smtplib.SMTP(smtpserver,587)
server.ehlo()
server.starttls() 
server.ehlo()
server.login(smtpuser,smtppass)
server.set_debuglevel(1)
server.sendmail(SENDER, [RECIPIENTS], s)
server.quit()
查看更多
Rolldiameter
4楼-- · 2020-02-12 09:40

To install socks module python.

sudo apt-get install python-socksipy

OR

You can also install using pip

pip install PySocks
查看更多
登录 后发表回答