Python 定时桌面截图发送至email

python桌面截图 定时发送至email

# -*- coding: UTF-8 -*-
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from PIL import ImageGrab
from threading import Timer
import time

#线程间隔
timer_interval = 1

# 邮箱相关配置
mailto_list = ['xxxx@qq.com']  # 接收邮箱
mail_host = "smtp.163.com"  # 发送邮箱SMTP服务器
mail_user = "xxxxx@163.com"  # 发送邮箱
mail_pass = "xxxvvvyyyzzzaaaa"  # 邮箱授权密码 ,从邮箱管理获得。
#适用于登录以下服务: 例如POP3/IMAP/SMTP、Exchange/CardDAV/CalDAV服务

# 发送邮件
def send_mail(tolist, sub):
    # 图片添加函数
    def addimg(src, imgid):
        fp = open(src, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', imgid)
        return msgImage

    msg = MIMEMultipart('related')
    # 邮件内容
    msgtext = MIMEText("""<h2>桌面截图</h2>
    <table><tr><td>
    <img src="cid:img">
    </td></tr></table>""", "html")
    msg.attach(msgtext)
    msg.attach(addimg("image.png", "img"))
    msg['Subject'] = sub  # 邮件主题
    msg['From'] = mail_user  # 邮件发送者
    msg['To'] = ";".join(tolist)  # 邮件接收者
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)  # 链接邮件smtp服务器
        server.login(mail_user, mail_pass)  # 登录邮箱
        server.sendmail(mail_user, tolist, msg.as_string())  # 发送
        server.close()  # 关闭
        print("截图发送成功")
        return True

    except:
        print("截图发送失败")
        return False



def getDesktopimg():
    im = ImageGrab.grab()  # 截取桌面图片
    im.save("image.png")  # 保存为image.png
    print("截图成功")


def delayrun():
    t = Timer(timer_interval, delayrun())
    t.start()

if __name__ == '__main__':

    while True:
        
        getDesktopimg() # 桌面截图
        time.sleep(10)  # 睡眠时间
        send_mail(mailto_list, "截图")
posted @ 2020-09-15 16:14  早上好章鱼哥  阅读(647)  评论(0编辑  收藏  举报