title: Fetch emails through IMAP with proxy of form user:password:host:port
body: <p>I have code to login to my email account to fetch recent emails:</p>
  <pre class="lang-py prettyprint-override"><code>def fetchRecentEmail(emailAddr, emailPassword, timeout=120):
      host = fetch_imap_server(emailAddr)  # e.g. 'outlook.office365.com'
      with IMAP4_SSL(host) as session:
          status, _ = session.login(emailAddr, emailPassword)
          if status == 'OK':
                  # fetch most recent message
                  status, messageData = session.select(&quot;Inbox&quot;)
                  :
  </code></pre>
  <p>I'm trying to tweak it to go through a proxy.</p>
  <p>ref: <a href="https://stackoverflow.com/questions/3386724/how-can-i-fetch-emails-via-pop-or-imap-through-a-proxy">How can I fetch emails via POP or IMAP through a proxy?</a></p>
  <p>ref: <a href="https://gist.github.com/sstevan/efccf3d5d3e73039c21aa848353ff52f" rel="nofollow noreferrer">https://gist.github.com/sstevan/efccf3d5d3e73039c21aa848353ff52f</a></p>
  <p>In each of the above resources, the proxy is of clean form IP:PORT.</p>
  <p>However my proxy is of the form USER:PASS:HOST:PORT.</p>
  <p>The proxy works:</p>
  <pre class="lang-py prettyprint-override"><code>USER = 'Pp7fwti5n-res-any-sid-' + random8Digits()
  PASS = 'abEDxts7v'
  HOST = 'gw.proxy.rainproxy.io'
  PORT = 5959
  
  proxy = f'{USER}:{PASS}@{HOST}:{PORT}'
  
  proxies = {
      'http': 'http://' + proxy,
      'https': 'http://' + proxy
  }
  
  response = requests.get(
      'https://ip.nf/me.json',
      proxies=proxies, timeout=15
  )
  </code></pre>
  <p>The following code looks like it should work, but errors:</p>
  <pre class="lang-py prettyprint-override"><code>HOST = 'outlook.office365.com'
  IMAP_PORT = 963
  PROXY_TYPE = 'http'  # rainproxies are HTTP
  
  mailbox = SocksIMAP4SSL(
      host=HOST,
      port=IMAP_PORT,
      proxy_type=PROXY_TYPE,
  
      proxy_addr=URL,
      proxy_port=PORT,
      username=USER,
      password=PASS
  )
  
  emailAddress, emailPassword = EMAIL.split(',')
  
  mailbox.login(emailAddress, emailPassword)
  
  typ, data = mailbox.list()
  print(typ)
  print(data)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/UMNEj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UMNEj.png" alt="enter image description here" /></a></p>
  <p>I needed to add a <code>timeout</code> arg/param in 2 places to get the code to run:</p>
  <pre class="lang-py prettyprint-override"><code>    def _create_socket(self, timeout=None):
          sock = SocksIMAP4._create_socket(self, timeout)
          server_hostname = self.host if ssl.HAS_SNI else None
          return self.ssl_context.wrap_socket(
              sock, server_hostname=server_hostname
          )
  
      def open(self, host='', port=IMAP4_PORT, timeout=None):
          SocksIMAP4.open(self, host, port, timeout)
  </code></pre>
  <p>Rather confusing that nobody else seems to have flagged that in the gist.</p>
  <p>But it still won't work.</p>
  <p>If I use any number other than 443 for IMAP_PORT I get this error:</p>
  <blockquote>
  <p>GeneralProxyError: Socket error: 403: Forbidden
  [*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)</p>
  </blockquote>
  <p>And if I use 443, while I now get no error, <code>mailbox = SocksIMAP4SSL(</code> never completes.</p>
  <p>So I am still far from a working solution.</p>
  <p>I am hoping to run this code simultaneously on 2 CPU cores, so I don't understand the implications of using port 443. Is that going to mean that no other process on my system can use that port? And if this code is using this port simultaneously in two processes, does this mean that there will be a conflict?</p>