python自动截图发送邮件_Python 定时自动截屏,发邮件通知

前一段做试验,需要远程监控现场的电脑;

写了一个定时截屏并把图片发送至邮箱的脚本,这样微信就可以得到通知。

1. 截屏,并保存图片

from PIL import ImageGrab

import time

time_now = time.strftime('%Y%m%d-%H%M%S')

pic = ImageGrab.grab()

pic_name = time_now+'.jpg'

pic.save(pic_name)

2. 定时任务

import threading

def func():

t = time.localtime(time.time())

min, hour, wkday = t.tm_min, t.tm_hour, t.tm_wday

# --------add tasks here--------------------------

if min in [0, 15, 30, 45]:

print('tasks')

# --------tasks end----------------------------

global timer

timer = threading.Timer(60, func, [])

timer.start()

timer = threading.Timer(1, func, [])

timer.start()

3.发邮件,将图片作为附件

from email.mime.text import MIMEText

from email.header import Header

from smtplib import SMTP_SSL

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

def send_mail(mail_title='', mail_content='', pic_name=''):

sender = 'xx@qq.com'

receiver = 'xx@qq.com'

pwd = 'xxxxxxxxxxxx' # qq邮箱授权码

smtp = SMTP_SSL('smtp.qq.com') # ssl登录

smtp.login(sender, pwd)

msg = MIMEMultipart()

msg["Subject"] = Header(mail_title, 'utf-8')

msg["from"] = sender

msg["to"] = receiver

puretext = MIMEText('content: '+mail_content)

msg.attach(puretext)

jpgpart = MIMEApplication(open(pic_name, 'rb').read())

jpgpart.add_header('Content-Disposition', 'attachment', filename=pic_name)

msg.attach(jpgpart)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

完整代码

from datetime import datetime

import time

from PIL import ImageGrab

import os

import threading

def send_mail(mail_title='', mail_content='', pic_name=''):

from email.mime.text import MIMEText

from email.header import Header

from smtplib import SMTP_SSL

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

sender = 'xx@qq.com'

receiver = 'xx@qq.com'

pwd = 'xxxxxxxxxxxx' # qq邮箱授权码

smtp = SMTP_SSL('smtp.qq.com') # ssl登录

smtp.login(sender, pwd)

msg = MIMEMultipart()

msg["Subject"] = Header(mail_title, 'utf-8')

msg["from"] = sender

msg["to"] = receiver

puretext = MIMEText('content: '+mail_content)

msg.attach(puretext)

jpgpart = MIMEApplication(open(pic_name, 'rb').read())

jpgpart.add_header('Content-Disposition', 'attachment', filename=pic_name)

msg.attach(jpgpart)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

def func():

t = time.localtime(time.time())

min, hour, wkday = t.tm_min, t.tm_hour, t.tm_wday

# --------add tasks here--------------------------

if min in [0, 15, 30, 45]:

time_now = time.strftime('%Y%m%d-%H%M%S')

pic = ImageGrab.grab()

pic_name = time_now+'.jpg'

pic.save(pic_name)

title = 'mgso4 test monitor ' + time_now

send_mail(title, title, pic_name)

# os.remove(pic_name)

# --------tasks end----------------------------

global timer

timer = threading.Timer(60, func, [])

timer.start()

if __name__ == "__main__":

timer = threading.Timer(1, func, [])

timer.start()

PS1:去掉python程序的命令行窗口

把 *.py 文件后缀改成 *.pyw ,即可去掉运行时出现的cmd窗口,直接后台运行。

PS2:加入开机启动(Win10)

将 *.pyw 文件做一个快捷方式,放到开始菜单的启动文件夹C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp里,这样每次开机就能直接启动了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值