10

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()
Imii Iik
  • 205
  • 1
  • 4
  • 14
  • What is your question? – Robᵩ Apr 23 '15 at 17:27
  • 1
    my question is: how can I edit script to send email through proxy – Imii Iik Apr 23 '15 at 17:30
  • That depends. What sort of proxy is it? What services does it provide? How is it configured? – Robᵩ Apr 23 '15 at 19:09
  • 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ᵩ Apr 23 '15 at 19:14
  • 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 Apr 24 '15 at 09:29
  • Also, to configure proxy in my browser, I use an url to a .pac file – Imii Iik Apr 24 '15 at 09:37

3 Answers3

11

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()
anarcat
  • 5,605
  • 4
  • 32
  • 38
MrAlexBailey
  • 5,219
  • 19
  • 30
  • Thank you for your help, please how can I install socks module in windows? I didn't find package – Imii Iik Apr 23 '15 at 17:31
  • @ImiiIik go to the link above, download the module, extract it to a folder and then from a command line run `python setup.py install` – MrAlexBailey Apr 23 '15 at 17:33
  • I tried your code but I got this error message ==> 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 Apr 24 '15 at 09:28
  • I changed the type of proxy to "socks.PROXY_TYPE_SOCKS5" but nothing is happening and the script remains started – Imii Iik Apr 24 '15 at 10:11
  • @ImiiIik What sort of proxy are you trying to use? Is it HTTP or SOCKS5? What port is it on? – MrAlexBailey Apr 24 '15 at 11:16
  • I Don't know exactly what sort of proxy, because I'm using a .pac file and in this file the used port for all addresses is 8080. – Imii Iik Apr 27 '15 at 09:24
  • Did you try both `PROXY_TYPE_HTTP` and `PROXY_TYPE_SOCKS5`? Are you sure you have the right proxy address? Does it require authentication? – MrAlexBailey Apr 27 '15 at 15:37
  • Yes I tried PROXY_TYPE_HTTP and the error msg is "socks.HTTPError: (502, 'Proxy Error ( The specified Secure Sockets Layer (SSL) p ort is not allowed. ISA Server.." and when I tried PROXY_TYPE_SOCKS5 or 4 nothing is happening and the script remains started – Imii Iik Apr 28 '15 at 11:08
2

To install socks module python.

sudo apt-get install python-socksipy

OR

You can also install using pip

pip install PySocks
alphaguy
  • 540
  • 3
  • 17
2

if your proxy is a http proxy you should use:

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

instead of socks.SOCKS5

eastonsuo
  • 935
  • 9
  • 11