以下是一个使用Python和IMAP库来按主题查找不完全匹配的电子邮件的示例代码:
import imaplib
import email
from email.header import decode_header
# 邮箱登录凭据
username = "your_email@example.com"
password = "your_password"
# 连接到IMAP服务器
imap_server = imaplib.IMAP4_SSL("imap.example.com")
imap_server.login(username, password)
# 选择邮箱文件夹
imap_server.select("INBOX")
# 定义要查找的主题
subject_to_search = "example subject"
# 构建搜索字符串
search_string = f'(SUBJECT "{subject_to_search}")'
# 在邮箱中搜索邮件
status, response = imap_server.search(None, search_string)
email_ids = response[0].split()
# 获取每封邮件的主题和发件人
for email_id in email_ids:
# 获取邮件内容
status, response = imap_server.fetch(email_id, "(RFC822)")
raw_email = response[0][1]
email_message = email.message_from_bytes(raw_email)
# 解码主题和发件人
subject = decode_header(email_message["Subject"])[0][0]
from_name = decode_header(email_message["From"])[0][0]
# 打印主题和发件人
print(f"Subject: {subject}")
print(f"From: {from_name}")
print("----------")
# 关闭IMAP连接
imap_server.logout()
请注意,你需要替换your_email@example.com
和your_password
为实际的邮箱登录凭据,以及imap.example.com
为实际的IMAP服务器地址。
此代码将连接到指定的IMAP服务器,登录到指定的邮箱,然后在收件箱中按指定的主题搜索邮件。它将打印出每封找到的邮件的主题和发件人。你可以根据需要进一步扩展代码。
上一篇:按住鼠标左键时进行能量充能射击
下一篇:按主题的一部分对电子邮件进行分类