github star gitee star atomgit star PyPI Downloads AI编程 AI交流群

大家好,这里是程序员晚枫,正在all in AI编程实战

第24讲:AI邮件处理——自动收发邮件

AI邮件能做什么?

  • 自动分类邮件(重要/普通/垃圾)
  • 智能回复建议
  • 自动生成邮件内容
  • 定时发送

1、安装邮件库

1
pip install python-office openai

2、读取邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import office

# 用python-office读取邮件
emails = office.email.receive_all(
imap_server="imap.qq.com",
email="你的邮箱@qq.com",
password="授权码" # 不是登录密码,是授权码
)

for email in emails[:5]:
print(f"主题:{email['subject']}")
print(f"发件人:{email['from']}")
print(f"时间:{email['date']}")
print(f"内容:{email['body'][:100]}")
print("---")

3、AI分类邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from openai import OpenAI

client = OpenAI(api_key="你的Key", base_url="https://api.deepseek.com")

def classify_email(email_body):
"""AI分类邮件"""
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{
"role": "user",
"content": f"""对以下邮件进行分类,只能返回:重要/普通/垃圾
邮件内容:{email_body[:500]}"""
}],
temperature=0
)
return response.choices[0].message.content.strip()

def classify_all_emails():
"""批量分类所有邮件"""
emails = office.email.receive_all(
imap_server="imap.qq.com",
email="你的邮箱@qq.com",
password="授权码"
)

important = []
normal = []
spam = []

for email in emails:
category = classify_email(email["body"])
if category == "重要":
important.append(email)
elif category == "普通":
normal.append(email)
else:
spam.append(email)

print(f"重要邮件:{len(important)}封")
print(f"普通邮件:{len(normal)}封")
print(f"垃圾邮件:{len(spam)}封")

return important, normal, spam

classify_all_emails()

4、AI生成回复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def generate_reply(email_body, sender):
"""AI生成邮件回复"""
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{
"role": "user",
"content": f"""你是邮件助手,根据以下邮件内容,生成一封得体的回复邮件。
要求:
1. 语言简洁专业
2. 直接回复,不要多余废话
3. 中文回复

发件人:{sender}
邮件内容:
{email_body}"""
}]
)
return response.choices[0].message.content

5、自动发送邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def send_ai_reply():
"""自动回复重要邮件"""
emails = office.email.receive_all(
imap_server="imap.qq.com",
email="你的邮箱@qq.com",
password="授权码"
)

for email in emails:
if classify_email(email["body"]) == "重要":
# AI生成回复
reply_content = generate_reply(email["body"], email["from"])

# 发送回复
office.email.send_email(
smtp_server="smtp.qq.com",
from_email="你的邮箱@qq.com",
password="授权码",
to_email=email["from"],
subject=f"Re: {email['subject']}",
content=reply_content
)
print(f"已回复:{email['from']}")

send_ai_reply()

6、定时发送日报

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import schedule
from datetime import datetime

def send_daily_report():
"""每日自动发送报告"""
# AI生成日报内容
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{
"role": "user",
"content": f"生成{datetime.now().strftime('%Y年%m月%d日')}的工作日报"
}]
)
report = response.choices[0].message.content

# 发送邮件
office.email.send_email(
smtp_server="smtp.qq.com",
from_email="你的邮箱@qq.com",
password="授权码",
to_email="领导@公司.com",
subject=f"【日报】{datetime.now().strftime('%Y-%m-%d')}",
content=report
)
print("日报已发送")

# 每天下午6点发送
schedule.every().day.at("18:00").do(send_daily_report)

下讲预告

学会了AI邮件处理,下一讲我们学 AI微信机器人——自动回复微信消息。

敬请期待!


程序员晚枫专注AI编程培训,小白看完他和图灵社区合作的教程《30讲 · AI编程训练营》就能上手做AI项目。

前3讲可以试听,试听链接:https://www.bilibili.com/cheese/play/ss982042944