imaplib — IMAP4 协议的客户端库
优质
小牛编辑
135浏览
2023-12-01
连接到服务器
# imaplib_connect.py
import imaplib
import configparser
import os
def open_connection(verbose=False):
# Read the config file
config = configparser.ConfigParser()
config.read([os.path.expanduser('~/.pymotw')])
# Connect to the server
hostname = config.get('server', 'hostname')
if verbose:
print('Connecting to', hostname)
connection = imaplib.IMAP4_SSL(hostname)
# Login to our account
username = config.get('account', 'username')
password = config.get('account', 'password')
if verbose:
print('Logging in as', username)
connection.login(username, password)
return connection
if __name__ == '__main__':
with open_connection(verbose=True) as c:
print(c)
认证失败
# imaplib_connect_fail.py
import imaplib
import configparser
import os
# Read the config file
config = configparser.ConfigParser()
config.read([os.path.expanduser('~/.pymotw')])
# Connect to the server
hostname = config.get('server', 'hostname')
print('Connecting to', hostname)
connection = imaplib.IMAP4_SSL(hostname)
# Login to our account
username = config.get('account', 'username')
password = 'this_is_the_wrong_password'
print('Logging in as', username)
try:
connection.login(username, password)
except Exception as err:
print('ERROR:', err)
示例配置
列出邮箱
# imaplib_list.py
import imaplib
from pprint import pprint
from imaplib_connect import open_connection
with open_connection() as c:
typ, data = c.list()
print('Response code:', typ)
print('Response:')
pprint(data)
# imaplib_list_parse.py
import imaplib
import re
from imaplib_connect import open_connection
list_response_pattern = re.compile(
r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)'
)
def parse_list_response(line):
match = list_response_pattern.match(line.decode('utf-8'))
flags, delimiter, mailbox_name = match.groups()
mailbox_name = mailbox_name.strip('"')
return (flags, delimiter, mailbox_name)
with open_connection() as c:
typ, data = c.list()
print('Response code:', typ)
for line in data:
print('Server response:', line)
flags, delimiter, mailbox_name = parse_list_response(line)
print('Parsed response:', (flags, delimiter, mailbox_name))
# imaplib_list_subfolders.py
import imaplib
from imaplib_connect import open_connection
with open_connection() as c:
typ, data = c.list(directory='Example')
print('Response code:', typ)
for line in data:
print('Server response:', line)
# imaplib_list_pattern.py
import imaplib
from imaplib_connect import open_connection
with open_connection() as c:
typ, data = c.list(pattern='*Example*')
print('Response code:', typ)
for line in data:
print('Server response:', line)
Mailbox状态
# imaplib_status.py
import imaplib
import re
from imaplib_connect import open_connection
from imaplib_list_parse import parse_list_response
with open_connection() as c:
typ, data = c.list()
for line in data:
flags, delimiter, mailbox = parse_list_response(line)
print('Mailbox:', mailbox)
status = c.status(
'"{}"'.format(mailbox),
'(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)',
)
print(status)
选择邮箱
# imaplib_select.py
import imaplib
import imaplib_connect
with imaplib_connect.open_connection() as c:
typ, data = c.select('INBOX')
print(typ, data)
num_msgs = int(data[0])
print('There are {} messages in INBOX'.format(num_msgs))
# imaplib_select_invalid.py
import imaplib
import imaplib_connect
with imaplib_connect.open_connection() as c:
typ, data = c.select('Does-Not-Exist')
print(typ, data)
查询信息
# imaplib_search_all.py
import imaplib
import imaplib_connect
from imaplib_list_parse import parse_list_response
with imaplib_connect.open_connection() as c:
typ, mbox_data = c.list()
for line in mbox_data:
flags, delimiter, mbox_name = parse_list_response(line)
c.select('"{}"'.format(mbox_name), readonly=True)
typ, msg_ids = c.search(None, 'ALL')
print(mbox_name, typ, msg_ids)
Search Criteria
# imaplib_search_subject.py
import imaplib
import imaplib_connect
from imaplib_list_parse import parse_list_response
with imaplib_connect.open_connection() as c:
typ, mbox_data = c.list()
for line in mbox_data:
flags, delimiter, mbox_name = parse_list_response(line)
c.select('"{}"'.format(mbox_name), readonly=True)
typ, msg_ids = c.search(
None,
'(SUBJECT "Example message 2")',
)
print(mbox_name, typ, msg_ids)
# imaplib_search_from.py
import imaplib
import imaplib_connect
from imaplib_list_parse import parse_list_response
with imaplib_connect.open_connection() as c:
typ, mbox_data = c.list()
for line in mbox_data:
flags, delimiter, mbox_name = parse_list_response(line)
c.select('"{}"'.format(mbox_name), readonly=True)
typ, msg_ids = c.search(
None,
'(FROM "Doug" SUBJECT "Example message 2")',
)
print(mbox_name, typ, msg_ids)
获取消息
# imaplib_fetch_raw.py
import imaplib
import pprint
import imaplib_connect
imaplib.Debug = 4
with imaplib_connect.open_connection() as c:
c.select('INBOX', readonly=True)
typ, msg_data = c.fetch('1', '(BODY.PEEK[HEADER] FLAGS)')
pprint.pprint(msg_data)
# imaplib_fetch_separately.py
import imaplib
import pprint
import imaplib_connect
with imaplib_connect.open_connection() as c:
c.select('INBOX', readonly=True)
print('HEADER:')
typ, msg_data = c.fetch('1', '(BODY.PEEK[HEADER])')
for response_part in msg_data:
if isinstance(response_part, tuple):
print(response_part[1])
print('\nBODY TEXT:')
typ, msg_data = c.fetch('1', '(BODY.PEEK[TEXT])')
for response_part in msg_data:
if isinstance(response_part, tuple):
print(response_part[1])
print('\nFLAGS:')
typ, msg_data = c.fetch('1', '(FLAGS)')
for response_part in msg_data:
print(response_part)
print(imaplib.ParseFlags(response_part))
Whole Messages
# imaplib_fetch_rfc822.py
import imaplib
import email
import email.parser
import imaplib_connect
with imaplib_connect.open_connection() as c:
c.select('INBOX', readonly=True)
typ, msg_data = c.fetch('1', '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
email_parser = email.parser.BytesFeedParser()
email_parser.feed(response_part[1])
msg = email_parser.close()
for header in ['subject', 'to', 'from']:
print('{:^8}: {}'.format(
header.upper(), msg[header]))
上传信息
# imaplib_append.py
import imaplib
import time
import email.message
import imaplib_connect
new_message = email.message.Message()
new_message.set_unixfrom('pymotw')
new_message['Subject'] = 'subject goes here'
new_message['From'] = 'pymotw@example.com'
new_message['To'] = 'example@example.com'
new_message.set_payload('This is the body of the message.\n')
print(new_message)
with imaplib_connect.open_connection() as c:
c.append('INBOX', '',
imaplib.Time2Internaldate(time.time()),
str(new_message).encode('utf-8'))
# Show the headers for all messages in the mailbox
c.select('INBOX')
typ, [msg_ids] = c.search(None, 'ALL')
for num in msg_ids.split():
typ, msg_data = c.fetch(num, '(BODY.PEEK[HEADER])')
for response_part in msg_data:
if isinstance(response_part, tuple):
print('\n{}:'.format(num))
print(response_part[1])
移动和复制消息
# imaplib_archive_read.py
import imaplib
import imaplib_connect
with imaplib_connect.open_connection() as c:
# Find the "SEEN" messages in INBOX
c.select('INBOX')
typ, [response] = c.search(None, 'SEEN')
if typ != 'OK':
raise RuntimeError(response)
msg_ids = ','.join(response.decode('utf-8').split(' '))
# Create a new mailbox, "Example.Today"
typ, create_response = c.create('Example.Today')
print('CREATED Example.Today:', create_response)
# Copy the messages
print('COPYING:', msg_ids)
c.copy(msg_ids, 'Example.Today')
# Look at the results
c.select('Example.Today')
typ, [response] = c.search(None, 'ALL')
print('COPIED:', response)
删除的消息
# imaplib_delete_messages.py
import imaplib
import imaplib_connect
from imaplib_list_parse import parse_list_response
with imaplib_connect.open_connection() as c:
c.select('Example.Today')
# What ids are in the mailbox?
typ, [msg_ids] = c.search(None, 'ALL')
print('Starting messages:', msg_ids)
# Find the message(s)
typ, [msg_ids] = c.search(
None,
'(SUBJECT "subject goes here")',
)
msg_ids = ','.join(msg_ids.decode('utf-8').split(' '))
print('Matching messages:', msg_ids)
# What are the current flags?
typ, response = c.fetch(msg_ids, '(FLAGS)')
print('Flags before:', response)
# Change the Deleted flag
typ, response = c.store(msg_ids, '+FLAGS', r'(\Deleted)')
# What are the flags now?
typ, response = c.fetch(msg_ids, '(FLAGS)')
print('Flags after:', response)
# Really delete the message.
typ, response = c.expunge()
print('Expunged:', response)
# What ids are left in the mailbox?
typ, [msg_ids] = c.search(None, 'ALL')
print('Remaining messages:', msg_ids)