python发送多个图片
# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import smtplib
import datetime
import os
mail_to = "123456@qq.com"
mail_host = "mail.qq.com:465"
mail_user = "yw@qq.com"
mail_pass = "123456"
def send_mail(title,images=[]):
me = "yw@qq.com"
msg = MIMEMultipart()
msg['Subject'] = title
msg['From'] = mail_user
msg['To'] = mail_to
mail_msg = '<h2 align="center">各rds实例数据库占用空间大小(GB)</h2><br>'
for i in range(len(images)):
mail_msg += '<p align="center"><img src="cid:image%d" height="900" width="900"></p>' % (i+1)
msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))
for i, img_name in enumerate(images):
with open("image/" + img_name, 'rb') as fp:
img_data = fp.read()
msg_image = MIMEImage(img_data)
msg_image.add_header('Content-ID', '<image%d>' % (i+1))
msg.attach(msg_image)
try:
s = smtplib.SMTP_SSL(mail_host)
s.login(mail_user, mail_pass)
s.sendmail(me, mail_to.split(','), msg.as_string())
s.quit()
return True
except Exception as e:
print(e)
return False
def get_imagelist(path):
# 获取当前目录下的所有文件
files = [os.path.join(path, file) for file in os.listdir(path)]
sorted_files = sorted(files)
# 遍历文件列表,输出文件名
image_list=[]
for file in sorted_files:
image_list.append(os.path.basename(file))
return image_list
if __name__ == '__main__':
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-1)
today = datetime.datetime.now().weekday() ##0 表示周一
title = "【数据库监控】各实例数据库占用空间" + '('+str(now_time.strftime('%Y%m%d'))+')'
content = "rds各实例数据库占用空间大小(GB)"
print(today)
path = "C:/Users/huangxueliang/PycharmProjects/rdscmdb/os/image"
if today == 2:
image_list = get_imagelist(path)
if len(image_list) > 0:
send_mail(title,image_list)