Код: Выделить всё
import imaplib
import email
# Dummy data for testing purposes
sent_emails = {
'recipient@example.com': ''
}
# Connect to the mail server (use your actual mail server settings)
mail = imaplib.IMAP4_SSL('imap.yourmailserver.com')
mail.login('your_email@example.com', 'your_password')
# Prepare the results list
results = []
for to_email, message_id in sent_emails.items():
# Clean up the Message-ID
clean_message_id = message_id.strip(' ').replace(' ', '\ ')
try:
# Use search command with `HEADER` for "In-Reply-To"
status, messages = mail.search(None, f'HEADER "In-Reply-To" "{clean_message_id}"')
if status == "OK" and messages[0]:
reply_ids = messages[0].split()
if reply_ids:
reply_id = reply_ids[0]
status, reply_data = mail.fetch(reply_id, "(RFC822)")
for part in reply_data:
if isinstance(part, tuple):
reply_msg = email.message_from_bytes(part[1])
reply_content = ""
if reply_msg.is_multipart():
for part in reply_msg.walk():
if part.get_content_type() == "text/plain":
reply_content = part.get_payload(decode=True).decode()
break
else:
reply_content = reply_msg.get_payload(decode=True).decode()
results.append([to_email, "True", reply_content])
else:
results.append([to_email, "False", "No reply found"])
else:
results.append([to_email, "False", "No reply found"])
except Exception as e:
print(f"Error processing email for {to_email}: {e}")
results.append([to_email, "Error", f"Search error: {str(e)}"])
# Logout after fetching
mail.logout()
Не могли бы вы мне сказать? что я могу исправить, чтобы получить правильные данные, сейчас мой CSV-файл выглядит так:
Код: Выделить всё
email replaid reply content
email1 Error Search error: SEARCH command error: BAD \[b'Could not parse command'\]
email2 Error Search error: SEARCH command error: BAD \[b'Could not parse command'\]
email3 Error Search error: SEARCH command error: BAD \[b'Could not parse command'\]
Код: Выделить всё
Added email for tracking: ... with Message-ID:
Added email for tracking: ... with Message-ID:
Added email for tracking: ... with Message-ID:
Error processing email for ...: SEARCH command error: BAD [b'Could not parse command']
Error processing email for ...: SEARCH command error: BAD [b'Could not parse command']
Error processing email for ...: SEARCH command error: BAD [b'Could not parse command']
Results saved to file email_replies_hello.csv
Подробнее здесь: https://stackoverflow.com/questions/791 ... -with-imap