Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python send email behind a proxy server

Tags:

python

proxy

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()
like image 557
Imii Iik Avatar asked Apr 23 '15 17:04

Imii Iik


Video Answer


1 Answers

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()
like image 61
MrAlexBailey Avatar answered Sep 21 '22 00:09

MrAlexBailey