74 lines
2.4 KiB
Python
Executable File
74 lines
2.4 KiB
Python
Executable File
import imaplib
|
||
import smtplib
|
||
import email
|
||
from email.header import decode_header
|
||
from email.mime.text import MIMEText
|
||
from imapclient import IMAPClient
|
||
from email.parser import Parser
|
||
import base64
|
||
def send_email(subject, body, to_email):
|
||
# 这个函数名为send_email,它接受三个参数:
|
||
# subject(邮件主题)、body(邮件内容)和to_email(收件人的电子邮件地址)。
|
||
from_email = 'yizeguo1@126.com'
|
||
|
||
mail_pass = 'CHRIZKWQSRWYLBOL'# 126授权码
|
||
# mail_pass = 'pwvzuqbiysqshgha'# qq授权码
|
||
|
||
msg = MIMEText(body)
|
||
msg['Subject'] = subject
|
||
msg['From'] = from_email
|
||
msg['To'] = to_email
|
||
|
||
server = smtplib.SMTP('smtp.126.com', 25)
|
||
# server = smtplib.SMTP('smtp.qq.com', 465)
|
||
server.starttls()
|
||
server.login(from_email, mail_pass)
|
||
server.sendmail(from_email, to_email, msg.as_string())
|
||
server.quit()
|
||
|
||
|
||
def get_latest_email_body(to_email):
|
||
# 连接到126邮箱的IMAP服务器
|
||
mail = IMAPClient("imap.126.com")
|
||
|
||
from_email = 'yizeguo1@126.com'
|
||
mail_pass = 'CHRIZKWQSRWYLBOL'# 126授权码
|
||
mail.login(from_email, mail_pass)
|
||
mail.id_({"name": "IMAPClient", "version": "2.1.0"})
|
||
|
||
# mail.list_folders()
|
||
print(mail.list_folders())
|
||
# 选择收件箱
|
||
# messages = mail.select_folder(folder="inbox", readonly=True)
|
||
mail.select_folder('INBOX')
|
||
# 搜索发件人为指定邮箱的所有邮件
|
||
encoded_email = to_email.encode('utf-8')
|
||
|
||
# 构建搜索条件:HEADER FROM "<发件人邮箱>"
|
||
search_criteria = f'FROM "{encoded_email.decode("utf-8")}"'
|
||
|
||
# 执行搜索操作
|
||
messages = mail.search(search_criteria)
|
||
# print(f"Search Status: {status}")
|
||
print(f"Matching Emails: {messages}")
|
||
|
||
for msgid, data in mail.fetch(messages, ['ENVELOPE']).items():
|
||
envelope = data[b'ENVELOPE']
|
||
print('ID #%d: "%s" received %s' % (msgid, envelope.subject.decode(), envelope.date))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
|
||
# send_email("测试", 'test', "guoyize2209@163.com")
|
||
# id_info = {
|
||
# 'name': 'myname',
|
||
# 'version': '1.0.0',
|
||
# 'vendor': 'myclient',
|
||
# 'support-email': 'yizeguo1@126.com'
|
||
# }
|
||
get_latest_email_body("guoyize2209@163.com")
|
||
# if body:
|
||
# print("Latest email body:", body)
|
||
# else:
|
||
# print("No plain text email found.")
|