I use this code to get unseen and new messages from my yahoo mail and find the messages with a specific URL.
from imap_tools import MailBox, AND
import re
from config import email, password
from scrap import scrap
yahooSmtpServer = "imap.mail.yahoo.com"
client = MailBox(yahooSmtpServer).login(email, password, 'INBOX')
while True:
msgs = client.fetch(AND(seen=False))
for msg in msgs:
mail = msg.html
if 'pick' in mail and not 'Combo-pick' in mail:
for i in re.findall(r'(https?://[^\s]+)', mail):
if 'pick' in i:
link = i.replace('"', "")
print(link)
try:
scrap(link)
except:
pass
client.seen(msg.uid, True)
client.logout()
client = MailBox(yahooSmtpServer).login(email, password, 'INBOX')
most of the time it works for some time. around 15 minutes. then it returns an error.
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\imaplib.py", line 1047, in _command_complete
typ, data = self._get_tagged_response(tag, expect_bye=logout)
File "C:\Program Files\Python39\lib\imaplib.py", line 1165, in _get_tagged_response
self._check_bye()
File "C:\Program Files\Python39\lib\imaplib.py", line 961, in _check_bye
raise self.abort(bye[-1].decode(self._encoding, 'replace'))
imaplib.abort: IMAP4rev1 Server logging out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\yahooMail (2)\yahooMail (1)\main.py", line 24, in <module>
client = MailBox(yahooSmtpServer).login(email, password, 'INBOX')
File "C:\Program Files\Python39\lib\site-packages\imap_tools\mailbox.py", line 44, in login
self.folder.set(initial_folder)
File "C:\Program Files\Python39\lib\site-packages\imap_tools\folder.py", line 37, in set
result = self.mailbox.box.select(encode_folder(folder))
File "C:\Program Files\Python39\lib\imaplib.py", line 756, in select
typ, dat = self._simple_command(name, mailbox)
File "C:\Program Files\Python39\lib\imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Program Files\Python39\lib\imaplib.py", line 1049, in _command_complete
raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => IMAP4rev1 Server logging out
Is there any way I can fix it? Or can I fetch the new/unseen Emails without login in every time? (When I tried to do it without login in a loop it didn't get the new messages). Thank you.
You should consider IMAP IDLE protocol extension described in RFC2177
https://datatracker.ietf.org/doc/html/rfc2177
imap_tools cannot do it, but you can implement it for lib.
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.