I want to parse some emails from a user ‘s inbox but when I do:
typ, msg_data = imap_conn.fetch(uid, '(RFC822)')
It marks the email as SEEN or read. This is not the desired functionality. Do you know how can I keep the email at its previous stare either SEEN or NOT SEEN?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You might also set read_only
to true when selecting the folder:
imap_conn.select('Inbox', readonly=True)
Method 2
The following should work:
typ, msg_data = imap_conn.fetch(uid, '(BODY.PEEK[HEADER])')
or BODY.PEEK[TEXT]
, etc.
Method 3
You can use (RFC822.PEEK)
as the “message-parts” argument, according to RFC 1730 (I have not verified which servers actually implement that correctly, but it doesn’t seem hard for them to).
Method 4
You may use imap_tools package:
https://pypi.org/project/imap-tools/
from imap_tools import MailBox, Q # get list of email subjects from INBOX folder with MailBox('imap.mail.com').login('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3145544245715c50585d1f525e5c">[email protected]</a>', 'password') as mailbox: # mark_seen=False - not mark emails as seen on fetch subjects = [msg.subject for msg in mailbox.fetch(mark_seen=False)]
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0