# Email

Email 自动化办公的实现,依赖的第三方库是:poemail

源码地址:https://github.com/CoderWanFeng/poemail

安装:pip install poemail

# 使用前准备

使用邮件功能前,你需要先获取邮箱的授权码(不是登录密码):

  1. 登录你的邮箱(以 QQ 邮箱为例)
  2. 进入「设置」→「账户」
  3. 找到「POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务」
  4. 开启服务并获取授权码

# 1、发送纯文本邮件

最基本的发邮件功能,1行代码搞定。

import poemail

key = '你的邮箱授权码'
msg_from = '你的邮箱@qq.com'      # 发件人
msg_to = '收件人@163.com'          # 收件人
msg_subject = '邮件主题'
msg_cc = '抄送人@qq.com'           # 抄送人(可选)
content = '邮件正文内容'

poemail.send.send_email(
    key=key,
    msg_from=msg_from,
    msg_to=msg_to,
    msg_subject=msg_subject,
    msg_cc=msg_cc,
    content=content
)

参数说明:

参数 类型 必填 说明
key str 邮箱授权码
msg_from str 发件人邮箱地址
msg_to str 收件人邮箱地址
msg_subject str 邮件主题
msg_cc str 抄送人邮箱,多人用 ; 分隔
content str 邮件正文内容

# 2、发送带附件的邮件

在纯文本邮件的基础上,添加 attach_files 参数即可发送附件。

import poemail

key = '你的邮箱授权码'
msg_from = '你的邮箱@qq.com'
msg_to = '收件人@163.com'
msg_subject = '带附件的邮件'
content = '请查收附件中的文件'
msg_cc = '抄送人1@qq.com;抄送人2@163.com'

# 附件列表,支持多个文件
attach_files = [
    r'D:\files\报告.doc',
    r'D:\files\数据截图.jpg'
]

poemail.send.send_email(
    key=key,
    msg_from=msg_from,
    msg_to=msg_to,
    msg_cc=msg_cc,
    msg_subject=msg_subject,
    content=content,
    attach_files=attach_files
)

参数说明:

参数 类型 必填 说明
attach_files list 附件文件路径列表,支持多个文件

其他参数同「发送纯文本邮件」。

# 3、批量发送邮件

结合 Excel 文件批量发送邮件,适用于通知、营销等场景。

import poemail

key = '你的邮箱授权码'
msg_from = '你的邮箱@qq.com'
msg_subject = '群发邮件主题'
content = '这是一封群发邮件'
attach_files = [r'D:\files\附件.pdf']

# 从 Excel 读取收件人列表,然后循环发送
email_list = ['收件人1@qq.com', '收件人2@163.com', '收件人3@gmail.com']

for msg_to in email_list:
    poemail.send.send_email(
        key=key,
        msg_from=msg_from,
        msg_to=msg_to,
        msg_subject=msg_subject,
        content=content,
        attach_files=attach_files
    )

💡 你也可以从 Excel 文件中读取收件人列表,使用 pandasopenpyxl 即可。

# 4、接收邮件并下载附件

自动登录邮箱,下载指定邮件的附件到本地。

import poemail

key = '你的邮箱授权码'
msg_from = '你的邮箱@qq.com'

poemail.receive.receive_email(
    key=key,
    msg_from=msg_from,
    output_path=r'D:\download\attachments',  # 附件保存目录
    status="UNSEEN"  # 只下载未读邮件的附件
)

参数说明:

参数 类型 必填 说明
key str 邮箱授权码
msg_from str 发件人邮箱地址
output_path str 附件保存的本地目录
status str 邮件状态过滤:"UNSEEN"(未读,默认)或 "ALL"(全部)

# 支持的邮箱

目前 poemail 支持以下邮箱服务:

  • QQ 邮箱
  • 163 邮箱
  • Gmail
  • 企业邮箱(Exchange)
  • 其他支持 SMTP/IMAP 协议的邮箱

# 相关课程

Last Updated: 4/6/2026, 10:18:15 AM