How to access IMAP server through a proxy server - python

i am trying to get all inbox emails from an emails in yahoo, cause of many emails in a day ,yahoo refuse my connections for a while , i want to connect via proxy when yahoo refuses my actual ip and gives that erro :
Cannot connect to : Can not authenticate to IMAP server: [CLOSED] IMAP connection broken (authenticate).
is there a way to connect to yahoo IMAP over proxy ?
i tried this way but no luck :
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,proxy_ip,port,True)
socket.socket = socks.socksocket
M = imaplib.IMAP4_SSL("imap.mail.yahoo.com")
a = M.login(user, passwd)

Related

Getting errors connecting to mail servers using SMTP in python

I want to send a mail using smtplib in python. I tried to connect to the gmail server and protonmail server.But getting these errors,
When trying to connect to Gmail server,
import smtplib
server=smtplib.SMTP('smtp.gmail.com',465)
Error: "A connection attempt failed because the connected party did not properly respond after a period of time"
When trying to connect to protonmail server,
import smtplib
Server=smtplib.SMTP('127.0.0.1',1025)
Error: "No connection could be made because the target machine actively refused it."
Please let me know how can I resolve it.
Try this for email,
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("yourmail#gmail.com", "yourpassword")

FTP connection issues

I have been tasked to try and build an interface to download and Upload files to an external FTP site which we use to communicate with a stock exchange.
For this we are communicating through the Filezilla FTP package.
I have spent the last few days researching and reading various pieces but cannot get the connection to work.
Now if I do a basic connection (as below) to the Filezilla website I get a connection.
from ftplib import FTP
ftp = FTP('filezilla-project.org')
Welcome = ftp.getwelcome()
print(Welcome)
# 220 FZ router and firewall tester ready
Basic I am trying to use the following (example not real credentials)
Protocol: FTP - File Transfer Protocol
Host: 212.31.105.26
Port:
Encryption: Require implicit FTP over TLS
Logon Type: Normal
User: L12
Password: GetConnected
I have created the below using various snippets etc. I do not get a fail message or timeout but I also do not get a response.
from ftplib import FTP_TLS
def connectftp():
ftp=ftplib.FTP('212.31.105.26', timeout=0.001)
print(ftp.getwelcome())
#ftp.login(username, password)
ftp.login(user='L12', passwd ='GetConnected')
#login_response = ftp.login(username, password)
login_response = ftp.login(user='L12', passwd ='GetConnected')
print(login_response)

Connection refused when sending email using Scrapy mail module

I am trying to send email after scraping using Scrapy, but I get this error:
2017-02-25 12:44:44 [scrapy.mail] ERROR: Unable to send mail: To=['<my_email>#gmail.com'] Cc=['<my_email>#gmail.com'] Subject="Test" Attachs=0- Connection was refused by other side: 10061: No connection could be made because the target machine actively refused it..
This is the code:
mailer = scrapy.mail.MailSender.from_settings(scrapy.conf.settings)
mailer.send(to=["<my_email>#gmail.com"], subject="Test",
body="Test", cc=["<my_email>#gmail.com"])
How can I send email successfully using Scrapy MailSender()
To send email you need an SMTP server that allows you to send email, and to configure the connection to that SMTP server through mail settings like MAIL_HOST and MAIL_PORT.
If you search the internet for the name of your email provider (e.g. Kolab Now, Google Mail) and "SMTP", you should be able to find the settings you need to use.

Sending SSL data over a TCP proxy connection in Python

I am facing the following scenario:
I am forced to use an HTTP proxy to connect to an HTTPS server. For several reasons I need access to the raw data (before encryption) so I am using the socket library instead of one of the HTTP specific libraries.
I thus first connect a TCP socket to the HTTP proxy and issue the connect command.
At this point, the HTTP proxy accepts the connection and seemingly forwards all further data to the target server.
However, if I now try to switch to SSL, I receive
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
indicating that the socket attempted the handshake with the HTTP proxy and not with the HTTPS target.
Here's the code I have so far:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('proxy',9502))
s.send("""CONNECT en.wikipedia.org:443 HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20100101 Firefox/15.0.1
Proxy-Connection: keep-alive
Host: en.wikipedia.org
""")
print s.recv(1000)
ssl = socket.ssl(s, None, None)
ssl.connect(("en.wikipedia.org",443))
What would be the correct way to open an SSL socket to the target server after connecting to the HTTP proxy?
(Note that in generally, it would be easier to use an existing HTTPS library such as PyCurl, instead of implementing it all by yourself.)
Firstly, don't call your variable ssl. This name is already used by the ssl module, so you don't want to hide it.
Secondly, don't use connect a second time. You're already connected, what you need is to wrap the socket. Since Python doesn't do any certificate verification by default, you'll need to verify the remote certificate and verify the host name too.
Here are the steps involved:
Establish your plain-text connection and use CONNECT like you're doing in the first few lines.
Read the HTTP response you get, and make sure you get a 200 status code. (You'll need to read the header line by line).
Use ssl_s = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_TLS1, ca_certs='/path/to/cabundle.pem') to wrap the socket. Then, verify the host name. It's worth reading this answer: the connect method and what it does after wrapping the socket.
Then, use ssl_s as if it was your normal socket. Don't call connect again.
works with python 3
< proxy > is an ip or domain name
< port > 443 or 80 or whatever your proxy is listening to
< endpoint > your final server you want to connect to via the proxy
< cn > is an optional sni field your final server could be expecting
import socket,ssl
def getcert_sni_proxy(cn,endpoint,PROXY_ADDR=("<proxy>", <port>)):
#prepare the connect phrase
CONNECT = "CONNECT %s:%s HTTP/1.0\r\nConnection: close\r\n\r\n" % (endpoint, 443)
#connect to the actual proxy
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect(PROXY_ADDR)
conn.send(str.encode(CONNECT))
conn.recv(4096)
#set the cipher for the ssl layer
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
#connect to the final endpoint via the proxy, sending an optional servername information [cn here]
sock = context.wrap_socket(conn, server_hostname=cn)
#retreive certificate from the server
certificate = ssl.DER_cert_to_PEM_cert(sock.getpeercert(True))
return certificate

SMTP directly to a host's MX record

I've tried playing around in python to learn more about the smtp protocol. More precisely I'm been trying to send a mail straight to a host's smtp server, but with little success.
For example, say I want to send a mail to a gmail.com address, I lookup the mx record for gmail.com:
>> nslookup -type=MX gmail.com
gmail.com MX preference = 40, mail exchanger = alt4.gmail-smtp-in.l.google.com
gmail.com MX preference = 5, mail exchanger = gmail-smtp-in.l.google.com
gmail.com MX preference = 10, mail exchanger = alt1.gmail-smtp-in.l.google.com
Then I do the following in python:
import smtplib
# Tried both port 465 and 587 (can't test port 25 since it's blocked by my ISP)
s = smtplib.SMTP("alt1.gmail-smtp-in.l.google.com", 587)
I tried several, and for everyone but one I always got a:
"[Errno 10051] A socket operation was attempted to an unreachable network" or
"[Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because conneted host has failed to respond" exception.
I read somewhere that some mail servers do a reverse lookup on your IP, and rejecting the connection if it hasn't got a domain. How do they do that?
I also read somewhere that many mail servers reject incoming mails from dynamic IP addresses (which I obviously have as a private customer to my ISP). How can they check if an IP address is dynamic or static?
Are these the reasons most servers seem to reject my connection? Or is there something more to it?
Um, your problem is exactly this:
# Tried both port 465 and 587 (can't test port 25 since it's blocked by my ISP)
Google's MX server is listening on port 25. If your ISP does not allow outgoing connections on this port, then you will not be able to send SMTP messages the way you are trying to do. You should get this sorted out with your ISP.
Regarding the rejection of messages, sending e-mail directly like this does increase the likelihood that it will be rejected or flagged as spam. Particularly if you set the "from" address to something that does not match the domain associated with your IP address, or if your SMTP client sends a mismatched domain in its EHLO message, or if the content of your message looks "spammy". The actual behavior will vary according to how each individual MX server has been configured.
Direct to MX email like you describe above will be blocked by Gmail's SMTP servers, with an error message "421-4.7.0", however many other SMTP severs, like MailEnable will allow Direct To MX.
The following website has source code for .NET and PHP for Direct to MX code, http://www.directtomx.com - you may be able to consume the webservice in python using SUDS.

Categories

Resources