3

I have the code below that I'm running in a jupyter notebook. I'm trying to login to a yahoo email account and download all the emails from a folder. I'm getting the error below when I try to run the code. When I just go to yahoo.com and login with the same email address and password, it works fine. Does anyone see what the issue might be? (The email below is a fake example.)

The code comes from the github repo: https://gist.github.com/robulouski/7442321

code:

import imaplib, email, getpass
import sys
from email.utils import getaddresses


IMAP_SERVER = 'imap.mail.yahoo.com'
EMAIL_ACCOUNT = 'MadeUp@yahoo.com'
EMAIL_FOLDER = 'TheFolder'
OUTPUT_DIRECTORY = '/Users/name/Desktop/Stuff/NewFolder'


PASSWORD = getpass.getpass()


def process_mailbox(M):
    """
    Dump all emails in the folder to files in output directory.
    """

    rv, data = M.search(None, "ALL")
    if rv != 'OK':
        print("No messages found!")
        return

    for num in data[0].split():
        rv, data = M.fetch(num, '(RFC822)')
        if rv != 'OK':
            print("ERROR getting message", num)
            return
        print("Writing message ", num)
        f = open('%s/%s.eml' %(OUTPUT_DIRECTORY, num), 'wb')
        f.write(data[0][1])
        f.close()

def main():
    M = imaplib.IMAP4_SSL(IMAP_SERVER)
    M.login(EMAIL_ACCOUNT, PASSWORD)
    rv, data = M.select(EMAIL_FOLDER)
    if rv == 'OK':
        print("Processing mailbox: ", EMAIL_FOLDER)
        process_mailbox(M)
        M.close()
    else:
        print("ERROR: Unable to open mailbox ", rv)
    M.logout()


if __name__ == "__main__":
    main()

Error:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-8-972361fa1b80> in <module>()
      1 if __name__ == "__main__":
----> 2     main()

<ipython-input-5-28d5fe9c1500> in main()
     21 def main():
     22     M = imaplib.IMAP4_SSL(IMAP_SERVER)
---> 23     M.login(EMAIL_ACCOUNT, PASSWORD)
     24     rv, data = M.select(EMAIL_FOLDER)
     25     if rv == 'OK':

~/anaconda/envs/py36/lib/python3.6/imaplib.py in login(self, user, password)
    591         typ, dat = self._simple_command('LOGIN', user, self._quote(password))
    592         if typ != 'OK':
--> 593             raise self.error(dat[-1])
    594         self.state = 'AUTH'
    595         return typ, dat

error: b'[AUTHENTICATIONFAILED] LOGIN Invalid credentials'

Update:

I went in to my yahoo account security settings and updated "Allow apps that use less secure sign in" to allow. Now I'm getting the error message below when I run the code in jupyter notebook.

error:

---------------------------------------------------------------------------
abort                                     Traceback (most recent call last)
~/anaconda/envs/py36/lib/python3.6/imaplib.py in _command_complete(self, name, tag)
   1013         try:
-> 1014             typ, data = self._get_tagged_response(tag)
   1015         except self.abort as val:

~/anaconda/envs/py36/lib/python3.6/imaplib.py in _get_tagged_response(self, tag)
   1133             try:
-> 1134                 self._get_response()
   1135             except self.abort as val:

~/anaconda/envs/py36/lib/python3.6/imaplib.py in _get_response(self)
   1041 
-> 1042         resp = self._get_line()
   1043 

~/anaconda/envs/py36/lib/python3.6/imaplib.py in _get_line(self)
   1145         if not line:
-> 1146             raise self.abort('socket error: EOF')
   1147 

abort: socket error: EOF

During handling of the above exception, another exception occurred:

abort                                     Traceback (most recent call last)
<ipython-input-14-3de4ef460c62> in <module>()
     39 
     40 if __name__ == "__main__":
---> 41     main()
     42 

<ipython-input-14-3de4ef460c62> in main()
     29     M = imaplib.IMAP4_SSL(IMAP_SERVER)
     30     M.login(EMAIL_ACCOUNT, PASSWORD)
---> 31     rv, data = M.select(EMAIL_FOLDER)
     32     if rv == 'OK':
     33         print("Processing mailbox: ", EMAIL_FOLDER)

~/anaconda/envs/py36/lib/python3.6/imaplib.py in select(self, mailbox, readonly)
    738         else:
    739             name = 'SELECT'
--> 740         typ, dat = self._simple_command(name, mailbox)
    741         if typ != 'OK':
    742             self.state = 'AUTH'     # Might have been 'SELECTED'

~/anaconda/envs/py36/lib/python3.6/imaplib.py in _simple_command(self, name, *args)
   1189     def _simple_command(self, name, *args):
   1190 
-> 1191         return self._command_complete(name, self._command(name, *args))
   1192 
   1193 

~/anaconda/envs/py36/lib/python3.6/imaplib.py in _command_complete(self, name, tag)
   1014             typ, data = self._get_tagged_response(tag)
   1015         except self.abort as val:
-> 1016             raise self.abort('command: %s => %s' % (name, val))
   1017         except self.error as val:
   1018             raise self.error('command: %s => %s' % (name, val))

abort: command: SELECT => socket error: EOF
Max
  • 10,701
  • 2
  • 24
  • 48
modLmakur
  • 531
  • 2
  • 8
  • 24
  • The server closed the connection on you – Max Sep 24 '18 at 01:19
  • Your code is also full of "Smart quotes". Is this in your actual code? If not, can you fix your question? – Max Sep 24 '18 at 01:19
  • @Max Thanks for getting back to me. There were some double quotes in there that I changed to single quotes. I think the formatting got messed up when I posted it, but double quotes shouldn't make a difference to python. Any idea why the server would be closing the connection on me? I'm working on a mac, do I need to open ports or something? – modLmakur Sep 24 '18 at 22:33
  • Turn on imaplib debugging, and see if the trace shows you anything. Maybe login failed, but didn't throw an exception? – Max Sep 25 '18 at 02:27

0 Answers0