基本流程
关键动作
SELECT
功能:用于选择一个邮箱,使其成为当前操作的目标。
SEARCH
功能:用于搜索满足特定条件的邮件。
FETCH
功能:用于获取指定邮件的详细信息。
重要
风险提示:下述代码在Python 3.11.9进行的测试,用于生产环境之前请务必先做好测试。
Python示例代码
场景:登录邮箱,选择收件箱,搜索未读邮件,获取邮件eml原文内容。
# -*- coding: utf-8 -*-
import imaplib
from imapclient import imap_utf7
imap_server = 'imap.qiye.aliyun.com'
username = 'test@example.com'
password = '********'
imap_port = 993
# 连接到IMAP服务器
mail = imaplib.IMAP4_SSL(imap_server, imap_port)
mail.login(username, password)
# 选择邮箱(例如 "INBOX")
folder_name = imap_utf7.encode('INBOX') # utf-7编码,中文编码
mail.select(folder_name)
# 搜索未读邮件
status, data = mail.search(None, 'UNSEEN')
if status == 'OK':
mail_ids = data[0].split()
for mail_id in mail_ids:
# 获取邮件的完整内容
status, msg_data = mail.fetch(mail_id, '(RFC822)')
if status == 'OK':
print(f"邮件ID:{mail_id.decode()}")
print("邮件内容:")
print(msg_data[0][1].decode())
# 关闭连接
mail.logout()
参数示例
SELECT注意事项
中文文件夹要utf-7编码
import imaplib
from imapclient import imap_utf7
imap_server = 'imap.qiye.aliyun.com'
username = 'test@example.com'
password = '******'
port = 993
mail = imaplib.IMAP4_SSL(imap_server, port)
# 登录
mail.login(username, password)
# 选择邮箱(例如 "收件箱")
folder_name = imap_utf7.encode('收件箱') # utf-7编码,中文编码
mail.select(folder_name)
print('已选择邮箱')
常见问题
文件夹编码错误,可以使用list命令查看文件夹名称,使用imap_utf7进行中文编码。
imaplib.IMAP4.error: SELECT command error: BAD [b'invalid command or parameters']
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
SEARCH支持的参数
import imaplib
from imapclient import imap_utf7
imap_server = 'imap.qiye.aliyun.com'
username = 'test@example.com'
password = '******'
port = 993
mail = imaplib.IMAP4_SSL(imap_server, port)
# 登录
mail.login(username, password)
# 选择邮箱(例如 "INBOX")
folder_name = imap_utf7.encode('INBOX') # utf-7编码,中文编码
mail.select(folder_name)
# search 示例
status, data = mail.search(None, 'SEEN') # 已读
# status, data = mail.search(None, 'UNSEEN') # 未读
# status, data = mail.search(None, 'ALL') # 全部
# status, data = mail.search(None, '(SEEN)') # 另一种写法
# status, data = mail.search(None, '(SEEN UNSEEN)') # 支持多条件
# status, data = mail.search(None, 'SEEN', ' UNSEEN') # 支持多条件,逗号间隔
# status, data = mail.search(None, '1') # UID
# status, data = mail.search(None, '(1 2)') # UIDs #支持多UID
# status, data = mail.search(None, 'ON 10-Jan-2024') # 搜索某一天的邮件
# status, data = mail.search(None, 'SINCE 10-Jan-2024') # 搜索大于等于某天的邮件
# status, data = mail.search(None, "(SINCE 05-Dec-2023 BEFORE 12-Jan-2024)") # SINCE代表开始,BEFORE代表结束
#
# status, data = mail.search(None, 'BEFORE', '12-Jan-2024', 'SEEN') # 搜索某个日期前的邮件,不包含该日期
# status, data = mail.search(None, 'BEFORE', '12-Jan-2024', 'SEEN', 'ON 05-Dec-2023') # 支持多条件,ON 是搜索某一天的邮件
# status, data = mail.search(None, 'BEFORE 12-Jan-2024', 'SEEN', 'ON 05-Dec-2023') # 另一种写法
# status, data = mail.search(None, 'BEFORE 12-Jan-2023 SEEN ON 05-Dec-2022')
# status, data = mail.search(None, "SENTSINCE 26-Mar-2024") # 自某个日期以来发送的邮件
print(status, data) # 返回uid信息
运行结果
SEARCH不支持的参数示例
# mail.search(None, '(SUBJECT "主题")')
# mail.search(None, 'SUBJECT "测试"'.encode('utf-8'))
# mail.search(None, 'OR FROM "a***@example.net"', 'SUBJECT "测试"'.encode('utf-8'))
# mail.search(None, '(FROM "昵称")')
# mail.search(None, 'FROM', '"昵称"')
# mail.search('GB2312', 'FROM "**example**.com"')
# mail.search(None, '(FROM "Doug" SUBJECT "Example message 2")')
# mail.search(None, "FROM", '"昵称"')
# mail.search([u'SINCE', date(2024, 1, 20)])
FETCH支持的参数
import imaplib
from imapclient import imap_utf7
imap_server = 'imap.qiye.aliyun.com'
username = 'test@example.com'
password = '******'
port = 993
# 连接到IMAP服务器
mail = imaplib.IMAP4_SSL(imap_server, port)
mail.login(username, password)
# 选择邮箱(例如 "INBOX")
folder_name = imap_utf7.encode('INBOX') # utf-7编码,中文编码
mail.select(folder_name)
# 搜索未读邮件
status, data = mail.search(None, 'UNSEEN')
if status == 'OK':
mail_ids = data[0].split()
for mail_id in mail_ids:
# 获取邮件的完整内容
f_status, msg_data = mail.fetch(mail_id, '(RFC822)')
# f_status, msg_data = mail.fetch(num, 'UID') # v_type1= OK data1= [b'1 (UID 12)']
# f_status, msg_data = mail.fetch(num, 'BODY[]') # (RFC822)等效
# f_status, msg_data = mail.fetch(num, '(UID BODY[HEADER])') # 返回完整的文件头信息。
# f_status, msg_data = mail.fetch(num, '(UID BODY[HEADER.FIELDS (Subject)])') # 在小括号里面可以指定返回的特定字段。如,Subject
# f_status, msg_data = mail.fetch(num, '(UID BODY[TEXT])') # 邮件内容,返回的数据是消息部分数据的元组
# f_status, msg_data = mail.fetch(num, 'FLAGS')[1] # [b'3 (UID 16 FLAGS (\\Seen))']#获取UID,需要处理返回数据
if f_status == 'OK':
print(f"邮件ID: {mail_id.decode()}")
print("邮件内容:")
print(msg_data[0][1].decode())
# 关闭连接
mail.logout()
运行结果
FETCH不支持的参数
# status, data = mail.fetch(num, '(UID BODY[HEADER.FIELDS.NOT (Subject)])')
# status, data = mail.fetch('2:3', '(RFC822)') #范围
常见问题
出现如下报错,通常是对应的阶段如SEARCH或FETCH中的参数错误或不支持,建议参考示例使用支持的命令参数。
imaplib.error: SEARCH command error: BAD [b'invalid command or parameters']
imaplib.error: FETCH command error: BAD [b'invalid command or parameters']
文档内容是否对您有帮助?