使用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看一下