0

I'm writing a script to grab the link from an email that my web application sends out to then start off a set of automated tests. At the minute my code should only grab the body of the email, I haven't started to scan for the link within that yet because I get no return.

import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('user', 'passwd')
mail.list()
mail.select("inbox")

result, data = mail.search(None, "ALL")

ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]

result, data = mail.fetch(latest_email_id, "(RFC822)")

raw_email = data[0][1]
print raw_email

I don't see anything wrong in my code for getting the newest email (top of the inbox) and my IMAP is set up for the account. Can anyone enlighten me as to why I'm getting the error:

socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Thanks in advance.

Edit: Company firewall is blocking port 143 and 993 (default IMAP(S) port), I've posted my workaround as the answer. There is nothing wrong with my code!

LeonH
  • 1,039
  • 2
  • 22
  • 45
  • 1
    Works well here. Are you sure that company's firewall, IT policies or some anti-virus/firewall configuration on your machine not blocking it? – sk11 Jul 22 '14 at 09:03
  • It might well be the company firewall! – LeonH Jul 22 '14 at 09:05

1 Answers1

0

To get around the company port restrictions I used the netsh command to port forward an open port to port 993 (default for imaplib's SSL) using the following command in terminal

netsh interface portproxy add v4tov4 listenport=UNBLOCKTHISPORT listenaddress=YOURIP connectport=THISPORTISOPEN connectaddress=YOURIP
LeonH
  • 1,039
  • 2
  • 22
  • 45
  • Hi @LeonH, the same issue I'm facing and the thing you suggested not working for windows 10 machine. would you please help. – DKM Jul 02 '19 at 05:25