简体   繁体   中英

Fetch emails through IMAP with proxy of form user:password:host:port

I have code to login to my email account to fetch recent emails:

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("Inbox")
                :

I'm trying to tweak it to go through a proxy.

ref: How can I fetch emails via POP or IMAP through a proxy?

ref: https://gist.github.com/sstevan/efccf3d5d3e73039c21aa848353ff52f

In each of the above resources, the proxy is of clean form IP:PORT.

However my proxy is of the form USER:PASS:HOST:PORT.

The proxy works:

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
)

The following code looks like it should work, but errors:

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)

在此处输入图像描述

I needed to add a timeout arg/param in 2 places to get the code to run:

    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)

Rather confusing that nobody else seems to have flagged that in the gist.

But it still won't work.

If I use any number other than 443 for IMAP_PORT I get this error:

GeneralProxyError: Socket error: 403: Forbidden [*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)

And if I use 443, while I now get no error, mailbox = SocksIMAP4SSL( never completes.

So I am still far from a working solution.

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?

Maybe you can try monkeypatching socket.socket with PySocket .

import socket
import socks

socks.set_default_proxy(socks.SOCKS5, HOST, PORT, True, USER, PASS)
socket.socket = socks.socksocket

Then check if your IMAP traffic is going through a given proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM