python注册全局热键_截图_发邮件带附件
最近考试,视频考试,为了那啥,做了个脚本,废话不多说,直接上代码:
# -*- coding: utf-8 -*----import ctypes
import ctypes.wintypes
import win32con
from threading import Thread
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from PIL import ImageGrab
class HotKey(Thread):
def run(self) -> None:
# 注册全局热键
keyAlt = 0x0001
keyCtrl = 0x0002
keyF3 = 0x72
ret = ctypes.WinDLL("user32.dll").RegisterHotKey(None, 4001, keyCtrl | keyAlt, keyF3)
if ret == 0:
print("热键注册失败!")
else:
print("热键注册成功!")
msg = ctypes.wintypes.MSG()
while True:
if ctypes.windll.user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY and msg.wParam == 4001:
# 第三方 SMTP 服务
mail_host = "smtp.qq.com"# 设置服务器
# 用户名
mail_user = "1363534266"
# 口令(注意这个口令不是QQ邮箱密码,登录QQ邮箱,设置-帐户-生成授权码)
mail_pass = "zjsuwdsaqwswqsaxghej"
sender = "1363534266@qq.com"
# 接收邮件,可设置为你的QQ邮箱或者其他邮箱
receivers = ["1304168777@qq.com"]
# 截屏保存图片
pic = ImageGrab.grab()
pic_name = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) + ".jpg"
pic.save(pic_name)
# 创建一个带附件的实例
message = MIMEMultipart()
message["From"] = Header("LDC", "utf-8")
message["To"] = Header("李康", "utf-8")
# 邮件标题
subject = pic_name
message["Subject"] = Header(subject, "utf-8")
# 邮件正文内容
message.attach(MIMEText("Hello World!", "plain", "utf-8"))
# 构造附件1 传送当前目录下的 1.jpg 文件
att1 = MIMEText(open(pic_name, "rb").read(), "base64", "utf-8")
att1["Content-Type"] = "application/octet-stream"
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = "attachment; filename="1.jpg""
message.attach(att1)
try:
server = smtplib.SMTP()
# 25 为 SMTP 端口号
server.connect(mail_host, 25)
server.login(mail_user, mail_pass)
server.sendmail(sender, receivers, message.as_string())
print("LDC:邮件发送成功!")
except smtplib.SMTPException:
print("Error: 无法发送邮件!")
ctypes.windll.user32.TranslateMessage(ctypes.byref(msg))
ctypes.windll.user32.DispatchMessageA(ctypes.byref(msg))
if__name__ == "__main__":
hot_key = HotKey()
hot_key.start()
以上是 python注册全局热键_截图_发邮件带附件 的全部内容, 来源链接: utcz.com/z/538076.html