6
votes

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 = '[email protected]'  
smtppass = 'mypassword'  

RECIPIENTS = '[email protected]'
SENDER = '[email protected]'
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()
3
What is your question? - Robᵩ
my question is: how can I edit script to send email through proxy - Imii Iik
That depends. What sort of proxy is it? What services does it provide? How is it configured? - Robᵩ
Also, what do you mean "he doesn't work"? Does it hang? Does it send the wrong email? Does it produce an error message? If so, what is the error message? - Robᵩ
Itried the given code in Answer 1 but I got this error msg ===>> socks.HTTPError: (502, 'Proxy Error ( The specified Secure Sockets Layer (SSL) p ort is not allowed. ISA Server is not configured to allow SSL requests from this port. Most Web browsers use port 443 for SSL requests. )') - Imii Iik

3 Answers

9
votes

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 = '[email protected]'  
smtppass = 'mypassword'  

RECIPIENTS = '[email protected]'
SENDER = '[email protected]'
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()
0
votes

To install socks module python.

sudo apt-get install python-socksipy

OR

You can also install using pip

pip install PySocks
0
votes

if your proxy is a http proxy you should use:

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

instead of socks.SOCKS5