Topic: how to access message flags?
==== REQUIRED BASIC INFO OF YOUR IREDMAIL SERVER ====
- iRedMail version (check /etc/iredmail-release):
- Deployed with iRedMail Easy or the downloadable installer?
- Linux/BSD distribution name and version:
- Store mail accounts in which backend (LDAP/MySQL/PGSQL):
- Web server (Apache or Nginx):
- Manage mail accounts with iRedAdmin-Pro?
- [IMPORTANT] Related original log or error message is required if you're experiencing an issue.
====
```
def read_mail_list():
# Connect to the IMAP server
...
# Search for all emails in the mailbox. use UNSEEN search unread mail
try:
status, email_ids = mail.search(None, "All")
except:
print("Failed to retrieve email list.")
mail.logout()
exit(1)
# Get a list of email IDs
email_id_list = email_ids[0].split()
# Loop through the email IDs and fetch the email details
print(f"search email count: {len(email_id_list)} \n")
for email_id in email_id_list:
# Fetch the email content by ID
status, msg_data = mail.fetch(email_id, "(BODY.PEEK[])")
if status == "OK":
email_content = msg_data[0][1]
# Parse the email message
msg = email.message_from_bytes(email_content)
print(msg)
# Get the "FLAGS" field from the email's response
# Check for common flags
is_seen = 'SEEN' in msg.get("FLAGS", "")
is_answered = 'ANSWERED' in msg.get("FLAGS", "")
is_flagged = 'FLAGGED' in msg.get("FLAGS", "")
is_draft = 'DRAFT' in msg.get("FLAGS", "")
is_recent = 'RECENT' in msg.get("FLAGS", "")
# Print the email's flags
print(f"Email ID: {email_id.decode()}")
print(f"Is Seen: {is_seen}")
print(f"Is Answered: {is_answered}")
print(f"Is Flagged: {is_flagged}")
print(f"Is Draft: {is_draft}")
print(f"Is Recent: {is_recent}")
# Check the "UNSEEN" state to determine the email status
is_unread = email_id in email_id_list[0].split()
# Print the email details
print(f"Unread: {'Yes' if is_unread else 'No'} \n")
print("From : {}".format(msg.get("From")))
print("To : {}".format(msg.get("To")))
print("Bcc : {}".format(msg.get("Bcc")))
print("Date : {}".format(msg.get("Date")))
print("Subject : {}".format(msg.get("Subject")))
# Get the email content
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True).decode()
print("\nMessage:")
print(body)
else:
body = msg.get_payload(decode=True).decode()
print("\nMessage:")
print(body)
# Close the mailbox and log out
mail.close()
mail.logout()
```
i want to get mail status from flags, but i get nothing, how to access message flags?
----
Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.