python截图html并发送带图片的html邮件
截图html
通过seleniun的webdriver打开html文件并截图
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import time
from src.read_conf import c_conf
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# https://develop.porsche-preview.cn:28444/smoke/summary/connect_summary.html
html_path = os.path.join(c_conf('host'),'smoke','summary','connect_summary.html')
file_path = os.path.join(c_conf('file'),'connect_summary.png')
def get_shot():
options = webdriver.ChromeOptions()
options.add_argument('-no-sandbox')
options.add_argument("-disable-features=VizDisplayCompositor")
options.add_argument("-incognito")
options.add_argument("enable-automation")
options.add_argument("-headless")
options.add_argument("-window-size=1920,1080")
options.add_argument("-disable-gpu")
options.add_argument("-disable-extensions")
options.add_argument("-dns-prefetch-disable")
options.add_argument("enable-features=NetworkServiceInProcess")
prefs = {
'profile.default_content_settings.popups': 0,
"profile.default_content_setting_values.automatic_downloads": 1}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
driver.get(html_path)
driver.maximize_window()
print(dir(driver))
time.sleep(3)
driver.get_screenshot_as_file(file_path)
driver.close()
if __name__ == '__main__':
get_shot()
发送带图片的html邮件
#!/usr/bin/env python
# coding=utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
msg_from = 'porsche_qa@126.com' # 发送方邮箱
passwd = 'SLZCOAJIPHFGJOWB' # 填入发送方邮箱的授权码
msg_to = 'binzichen@126.com' # 收件人邮箱
def send():
subject = "python邮件测试" # 主题
msg = MIMEMultipart('related')
content = MIMEText('<html><body><div><img src="cid:imageid" alt="imageid" style="width: 1065px;height: 450px;"></div></body></html>', 'html', 'utf-8') # 正文
msg.attach(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
file = open(r"new.png", "rb")
img_data = file.read()
file.close()
img = MIMEImage(img_data)
img.add_header('Content-ID', 'imageid')
msg.attach(img)
try:
s = smtplib.SMTP_SSL("smtp.126.com", 465) # 邮件服务器及端口号
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print('发送成功!')
except Exception:
print('发送失败!')
finally:
s.quit()
if __name__ == '__main__':
send()