微信消息提醒python实现

使用python进行微信来消息自动发邮件提醒

# python3 author jin xiang
import time
from PIL import ImageGrab
import os

basePath = "C:\\Users\\Administrator\\Desktop\\"
# 这里是为了如果找不到png这个目录的情况自己建一个png目录
absPath = os.path.abspath('.')
path = [x for x in os.listdir('.') if os.path.isdir(x)]

def sendMail2():
    import traceback
    from email.mime.text import MIMEText
    from email.mime.image import MIMEImage
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    import smtplib, ssl
    import logging
    if __name__ == '__main__':
        mail_account = "aaa@163.com"//发件邮箱
        password = "xxxx"//邮箱客户端密码
        context = ssl.create_default_context()
        mail = MIMEMultipart()
        mail_to = []  # list类型的收件人列表
        mail['From'] = mail_account  # 你的账户名
        mail['To'] = ';'.join(mail_to)
        mail['Subject'] = 'Picture Test'
        img_file = open(r'C:\\Users\\Administrator\\Desktop\\new.png', 'rb')
        img_data = img_file.read()
        img_file.close()
        img = MIMEImage(img_data)
        img.add_header('Content-ID', 'dns_config')  # 给一个content Id 供后面html内容引用
        mail.attach(img)
        mail_content = """
        <html>
          <body>
            <p>This is a picture</p>
            <img src="cid:dns_config">
          </body>
        </html>
        """  # 构建html邮件内容
        mail.attach(MIMEText(mail_content, 'html', 'utf-8'))
        # 通过邮件服务器来发送邮件
        try:
            with smtplib.SMTP_SSL("smtp.163.com", 994) as server:
                server.ehlo()
                # server.starttls(context=context)
                server.ehlo()
                server.login(mail_account, password)
                server.sendmail(mail_account, mail_to, mail.as_string())
                server.close()
                # logger.info('send finished')
        except Exception:
            traceback.print_exc()
# print(path)
def isSame():
    import cv2

    import numpy as np

    img1 = cv2.imread(basePath + 'new.png')
    img2 = cv2.imread(basePath + 'old.png')
    img1_np_arr = np.array(img1)
    img2_np_arr = np.array(img2)
    if np.all(img1_np_arr == img2_np_arr):
        return True
    else:
        return False
# 截屏
def Screenshot():
    bbox = (0, 0, 1910, 1048)
    im = ImageGrab.grab(bbox)
    # 保存(图个有png路径或者别的路径需要在这个路径下有这个目录,不然报错,所以我前面是做了规避,没路径我就自己建一个)
    if os.path.exists(basePath + 'old.png'):
        im.save(basePath + 'new.png')
        if not isSame():
            print("新消息")
            sendMail2()
            im.save(basePath + 'old.png')
    else:
        im.save(basePath + 'old.png')

while True:
    print("截图!")
    Screenshot()
    print("暂停")
    print("\n")
    time.sleep(10)  # 定时10s看一下
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值