Bootstrap

selenium无头浏览器截图并以邮件发送

  • selenium
    4.11版本的selenium无需在本地下载浏览器驱动
# @Time    : 2024/10/18 17:54
# @Author  : 
# @FileName: print_sc.py
# @Description:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import base64

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

path = r'D:\chrome_driver'
service = Service(executable_path=path)


def download(url, pngName):
    # 设置chrome参数
    browser = webdriver.Chrome(options=chrome_options)
    try:
        # 获取网页内容
        browser.get(url)
        # 通过执行脚本,设置滚动条到最大宽度及最大高度
        width = browser.execute_script("return document.documentElement.scrollWidth")
        height = browser.execute_script("return document.documentElement.scrollHeight")
        browser.set_window_size(width, height)
        # 是否需要超时等待
        # time.sleep(10)
        # 保存的截图名字
        browser.save_screenshot(pngName)
        browser.quit()
    except Exception as err:
        print(err)
        browser.quit()
    finally:
        browser.quit()


def send_email(smtp_server, port, username, password, from_addr, to_addr, subject, body, image_path):
    # 创建MIME多部分消息
    msg = MIMEMultipart('related')
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = subject

    # 创建邮件主体HTML内容
    # cid:image1 用于指定嵌入图像的位置
    html_content = f"""
        <html>
        <body>
            <p>{body}</p>
            <img src="cid:image1" alt="Embedded Image">
        </body>
        </html>
        """

    # 添加HTML到邮件
    msg.attach(MIMEText(html_content, 'html'))

    # 读取和编码图像
    with open(image_path, 'rb') as img_file:
        img_data = img_file.read()

    # 创建MIMEImage对象并设置相关头信息
    image = MIMEImage(img_data, name=image_path.split('/')[-1])
    image.add_header('Content-ID', '<image1>')  # cid标识符

    # 将图片对象附加到邮件
    msg.attach(image)

    # 连接到SMTP服务器并发送邮件
    try:
        server = smtplib.SMTP(smtp_server, port)
        # server.starttls()  # 启用TLS加密
        # server.login(username, password)
        server.send_message(msg)
        print("Email sent successfully")
    except Exception as e:
        print(f"Failed to send email: {e}")
    finally:
        server.quit()


smtp_server = 'SMTp.test.com'  # 替换为你的SMTP服务器
port = 25  # 通常为587或465,为SMTP服务器提供
username = 'xxxxx'
password = 'xxxxx'
from_addr = '[email protected]'

if __name__ == '__main__':
    url = 'https://console.cn/platform'
    pngPath = 'D:/chrome_driver/'
    pngName = pngPath + "summary.png"
    download(url, pngName)

    to_addr = '[email protected]'
    subject = 'Here is your embedded image'
    body = 'Please see the image below.'
    image_path = pngName

    send_email(smtp_server, port, username, password, from_addr, to_addr, subject, body, image_path)

;