You don't need to dirtily hack imaplib. You could try using the SocksiPy package, which supports socks4, socks5 and http proxy (connect):
Something like this, obviously you'd want to handle the setproxy options better, via extra arguments to a custom __init__ method, etc. You could probably do similar with the IMAP4_SSL.
from imaplib import IMAP4, IMAP4_PORT
from socks import sockssocket, PROXY_TYPE_SOCKS4, PROXY_TYPE_SOCKS5, PROXY_TYPE_HTTP
self.host = host
self.port = port
self.sock = sockssocket()
self.file = self.sock.makefile('rb')
Nice find MattH, that's exactly the kind of thing I was looking for (didn't know SocksiPy). I'll give it a go. Thanks.