惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
P
Privacy International News Feed
Apple Machine Learning Research
Apple Machine Learning Research
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
The Hacker News
The Hacker News
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
About on SuperTechFans
S
Secure Thoughts
G
Google Developers Blog
博客园 - 司徒正美
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
Latest news
Latest news
V2EX - 技术
V2EX - 技术
I
InfoQ
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Security Latest
Security Latest
AI
AI
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cloudbric
Cloudbric
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Schneier on Security
博客园 - 【当耐特】
NISL@THU
NISL@THU
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
Help Net Security
Help Net Security
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 百衲本

Centos7 副本集模式部署 MongoDB centos7部署Nacos集群模式 centos7 yum快速安装clickhouse单机版 k8s高可用部署Seata kubernetes中pod磁盘占用排查 jumpserver V3 终端常用操作 Python 列表生成式、字典生成式与生成器表达式 Python 的可迭代对象、迭代器对象与生成器 python email模块自动化操作邮件 python自动化操作PDF 国内企业邓白氏编码免费申请流程 Python psutil模块 免费公共API调用清单 Python pathlib 模块 容器网络故障排查:从 ping 到 tcpdump 的全链路思路 systemd详解 APP性能指标 Linux 上禁用 USB 存储设备 线上故障的排查清单,运维小哥拿走不谢!
python yagmail 模块自动化操作邮件
百衲本 · 2025-09-25 · via 博客园 - 百衲本

一、安装需要的模块

pip install yagmail  #专门为gmail设计的邮件发送模块,可以极大简化邮件发送流程,其他邮箱也可以使用
pip install keyring  #安全密码管理,避免代码中硬编码密码等敏感信息
pip install schedule #任务调度
pip install mbox     #邮件处理

二、常用示例

1.存储和读取密码

import keyring
import yagmail

yagmail.register('960193682@qq.com', 'password')  # 只要注册一遍,后面使用就不需要密码了,如果密码改了再次使用新密码注册即可
password = keyring.get_password("yagmail", "96019xxx@qq.com") #获取密码
print(password)

2.发送邮件

import yagmail

yag = yagmail.SMTP(user='9601xxxx@qq.com', host='smtp.qq.com', port=465)  # 如果用户名已经注册过,密码就不需要填了,否则需要填入密码
# 除了文本,HTML也可以直接写,添加附件直接指定文件路径,当前目录可以不写路径前缀,如 test.png,如果附件需要展示在正文,可以使用inline参数指定,如下发示例
contents = ['文本内容', '<a href="https://www.baidu.com">百度搜索</a>', 'test.png', yagmail.inline('test.png')] 
yag.send('xxxx@cheryholxxx.com', '测试邮件', contents) # 如果是群发,收件人使用列表格式

3.定时发送

#官方示例
import schedule
import time

def job():  # 把job换成我们发送邮件的函数即可
    print("I'm working...")

schedule.every(10).minutes.do(job) # 每10分钟执行一次
schedule.every(5).to(10).minutes.do(job) # 每5~10分钟执行一次
schedule.every().hour.do(job)  #每小时执行一次
schedule.every().day.at("10:30").do(job) #每天 10:30 执行
schedule.every().monday.do(job) #每周一执行
schedule.every().wednesday.at("13:15").do(job) #每周三执行13:15
schedule.every().day.at("12:42", "Europe/Amsterdam").do(job)  #Europe/Amsterdam时区的每天12:42执行
schedule.every().minute.at(":17").do(job) #每分钟的第17秒执行

while True:
    schedule.run_pending()
    time.sleep(1)


参考文档:https://schedule.readthedocs.io/en/stable/

4.接收与查找邮件  

import keyring
from imbox import Imbox

password = keyring.get_password("yagmail", "9601xxxx@qq.com")
print(password)

with Imbox('imap.qq.com', '9601xxxx@qq.com', password, port=993,ssl=True) as imbox:
    all_inbox_messages = imbox.messages()  # 读取所有邮件
    for uid, message in all_inbox_messages:  # uid可以查找到邮件,后续可以做标记或者删除
        print(uid)
        print(message.subject) # 常用读取参数见下方
        print(message.body['plain'])


#imbox.messages() 从服务端获取邮件时可以的参数
unread:  未读邮件
flagged: 红旗/标星邮件
unflagged:  非红旗/标星邮件
sent_from': 某发件人邮件
sent_to: 某收件人邮件
date__gt: 某时间之前的邮件  值格式为:datetime.data(2025,09,09)  年月日
date__lt: 某时间之后的邮件
date__on: 某天的邮件
subject: 邮件主体
uid__range': 邮件编号
text': 邮件内容



#message.xxx  单个邮件读取时可用参数
发件人:message.sent_from
收件人:message.sent_to
主题:message.subject
时间:message.date
文本格式内容:message.body['plain']
HTML格式内容:message.body['html']
附件:message.attachments