"EOF error" when fetching emails in bulk using imap_tools python - python

I've build a very simple script that fetches emails from an Outlook inbox. It looks like this:
from imap_tools import MailBox
inbox = MailBox("outlook.office365.com").login("email", "password")
for email in inbox.fetch(): # bulk=True
print(email)
It's working when I use it like this. Though, when passing the arg bulk=True on the fetch() function it will raise the following error:
Traceback (most recent call last):
File "/Users/julius/Library/Application Support/JetBrains/IntelliJIdea2022.3/scratches/scratch_1.py", line 5, in <module>
for email in inbox.fetch(bulk=True):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 171, in fetch
for fetch_item in (self._fetch_in_bulk if bulk else self._fetch_by_one)(nums, message_parts, reverse): # noqa
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 144, in _fetch_in_bulk
fetch_result = self.client.fetch(','.join(message_nums), message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 548, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1008, in _command
raise self.abort('socket error: %s' % val)
imaplib.IMAP4.abort: socket error: EOF occurred in violation of protocol (_ssl.c:2396)
Has anyone got a clue on how to fix this?

Right answer: combine bulk and limit args.
Text from docs:
For actions with a large number of messages imap command may be too large and will cause exception at server side, use 'limit' argument for fetch in this case.

Related

Reading emails using poplib throws an error - poplib.error_proto: -ERR EOF

I am trying to read emails using the poplib library but getting an error - poplib.error_proto: -ERR EOF. The initial connection seems to be successful and I am able to get the response for the getwelcome() function but after that, the server seems to be closing the connection. Adding the code below, nothing special just the dummy code -
import poplib
client = poplib.POP3_SSL(server_name, port)
client.user(username)
client.pass_(password)
welcome = client.getwelcome()
print(welcome)
messages = len(client.list()[1]) # throws error - poplib.error_proto: -ERR EOF
Traceback (most recent call last):
File "email-pop.py", line 68, in <module>
fetch_all_mails()
File "email-pop.py", line 36, in fetch_all_mails
numMessages = len(pop_conn.list()[1])
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/poplib.py", line 243, in list
return self._longcmd('LIST')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/poplib.py", line 186, in _longcmd
return self._getlongresp()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/poplib.py", line 162, in _getlongresp
resp = self._getresp()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/poplib.py", line 152, in _getresp
resp, o = self._getline()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/poplib.py", line 136, in _getline
if not line: raise error_proto('-ERR EOF')
poplib.error_proto: -ERR EOF
Can't seem to find what could be the issue, any help would be appreciated.
Thanks

Trying to mass delete emails using Python 3.7.2 & IMAPClient - imaplib.IMAP4.error: UID command error: BAD [b'Command line too large']

I'm very new to programming so I'm looking for some help here. I have an email folder that has over 200k emails. I am trying to delete them all because they're slowing me down. I wrote a script in Python 3.7.2 using IMAPClient to try and delete them en masse but I keep getting an error.
I have tried to delete all emails in the folder, and I have tried to delete a single days worth. An average single day has about 2k emails. Using both 'ALL' and 'ON DD-MMM-YYYY"
import imapclient
imapObj = imapclient.IMAPClient('imap.server.com', ssl=True)
imapObj.login('email', 'password')
import pprint
pprint.pprint(imapObj.list_folders())
[((b'\\NoInferiors',), b'/', 'INBOX'),
((b'\\HasNoChildren',), b'/', 'Folder I want to delete'),
--all folders--
imapObj.select_folder('Folder I want to delete', readonly=False)
{b'PERMANENTFLAGS': (b'\\Answered', b'\\Flagged', b'\\Draft',
b'\\Deleted', b'\\Seen', b'$Forwarded', b'$NotJunk', b'NotJunk',
b'$MailFlagBit1', b'$MailFlagBit0', b'$MailFlagBit2', b'\\*'),
b'FLAGS': (b'\\Answered', b'\\Flagged', b'\\Draft', b'\\Deleted',
b'\\Seen', b'$Forwarded', b'$NotJunk', b'NotJunk', b'$MailFlagBit1',
b'$MailFlagBit0', b'$MailFlagBit2'), b'EXISTS': 3792507, b'RECENT':
0, b'UNSEEN': [b'3792504'], b'UIDVALIDITY': 1398179813, b'UIDNEXT':
7577201, b'READ-WRITE': True}
UIDs = imapObj.search('ALL')
imapObj.delete_messages(UIDs)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imapObj.delete_messages(UIDs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1171, in delete_messages
return self.add_flags(messages, DELETED, silent=silent)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1081, in add_flags
return self._store(b'+FLAGS', messages, flags, b'FLAGS',
silent=silent)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1567, in _store
uid=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1535, in
_command_and_check
typ, data = self._imap.uid(command, *args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib
.py", line 879, in uid
typ, dat = self._simple_command(name, command, *args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib
.py", line 1196, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib
.py", line 1027, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: UID command error: BAD [b'Command line too large']
I expect the emails to be delete but the command just errors out to the above.
The list of UIds is to long to make one request , we need to split every deletion.
UIDs = imapObj.search('ALL')
for UID in UIDs
imapObj.delete_messages([UID])
print( 'Deleted--->'+str(UID))

How to sort email by time in imaplib using python

I would like to sort the emails by arrival time.
Here is the code:
data = get_credential('info.txt')
imap_conn = create_connection(data, 'outlook.office365.com')
imap_conn.select('INBOX', readonly=False)
result, messages = imap_conn.sort('ARRIVAL', 'UTF-8', 'FROM peter#gmail.com SINCE "'+(datetime.date.today()-datetime.timedelta(4)).strftime('%d-%b-%Y')+'"
It returns the following error:
File "C:\Program Files\Python37\lib\imaplib.py", line 794, in sort
typ, dat = self._simple_command(name, sort_criteria, charset, *search_criteria)
File "C:\Program Files\Python37\lib\imaplib.py", line 1196, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Program Files\Python37\lib\imaplib.py", line 1027, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: SORT command error: BAD [b'Command Error. 12']
How to solve the problem?
Is the input charset UTF-8 correct? How to get it from messages?
Office365 doesn't support the SORT extension.
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+
You should check CAPABILITY before using any extensions.

Having trouble logging in to IMAP account in Python3.61

I am developing a Python script in version 3.61 to connect to an Imap server and select all email messages older than a certain date. I imported the imapclient module and then provided the following in the IDLE shell:
HOST = 'imap.comcast.net'
USERNAME = 'username'
PASSWORD = 'topsecret'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
I received the following errors after using the login option:
Traceback (most recent call last):
File "C:\Python\Python36-32\lib\imaplib.py", line 1011, in _command_complete
typ, data = self._get_tagged_response(tag)
File "C:\Python\Python36-32\lib\imaplib.py", line 1123, in
_get_tagged_response
self._check_bye()
File "C:\Python36-32\lib\imaplib.py", line 926, in _check_bye
raise self.abort(bye[-1].decode(self._encoding, 'replace'))
imaplib.IMAP4.abort: Zimbra IMAP server terminating connection
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
server.login(USERNAME, PASSWORD)
File "C:\Python\Python36-32\lib\site-packages\imapclient\imapclient.py",
line
215, in login
unpack=True,
File "C:\Python\Python36-32\lib\site-packages\imapclient\imapclient.py",
line
1180, in _command_and_check
typ, data = meth(*args)
File "C:\Python36-32\lib\imaplib.py", line 588, in login
typ, dat = self._simple_command('LOGIN', user, self._quote(password))
File "C:\Python\Python36-32\lib\imaplib.py", line 1188, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Python\Python36-32\lib\imaplib.py", line 1013, in _command_complete
raise self.abort('command: %s => %s' % (name, val))
imaplib.IMAP4.abort: command: LOGIN => Zimbra IMAP server terminating
connection
The docs indicate to me that this should be sufficient to make the connection but it is not clear why the connection is terminated after attempting the login.
Any help provided would be most welcome.

Using imaplib to log into Gmail using credentials from a file

I apologise in advance, I know this will be a simple answer but I'm not having any luck and it's starting to bug me.
I just want to log into my Gmail account using imaplib and credentials pulled from a file.
I can log into Gmail no worries using:
srv = imaplib.IMAP_SSL("imap.gmail.com")
srv.login("bob#gmail.com", "mypassword")
But instead I'm trying to use a text file (credentials.dat). Inside the file is 2 lines:
bob#gmail.com
mypassword
My code is as follows:
import imaplib
CREDS = open("credentials.dat", "r")
account = CREDS.readline()
password = CREDS.readline()
srv = imaplib.IMAP4_SSL("imap.gmail.com")
srv.login(account, password)
When I run it I get the followign error:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib64/python2.4/imaplib.py", line 480, in login
typ, dat = self._simple_command('LOGIN', user, self._quote(password))
File "/usr/lib64/python2.4/imaplib.py", line 1028, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/lib64/python2.4/imaplib.py", line 865, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: LOGIN command error: BAD ['Failed to parse your command r13if7033890ebd.47']
I assume it doesn't like the data type being entered into srv.login, but I have no idea how else to do it.
There are so many more things I want to do once I get into Gmail (read emails, post, etc), but it's looking like an uphill battle if I can't even log in :(
Thanks for your time.
readline() includes the newline character at the end of the line, so you're really saying: login with 'bob#gmail.com\n'
Try readline().strip() to strip whitespace characters from both sides of the line.

Categories