python向多个用户发送文字、图片内容邮件和excel附件

话不多说,直接上代码,需要把发件里面的smtp_info替换为自己的信息,其中password是指邮箱在开通POP/SMTP功能后获取的授权码!

复制代码
import smtplib  
from email.mime.multipart import MIMEMultipart  
from email.mime.text import MIMEText  
from email.mime.image import MIMEImage  
from email.mime.base import MIMEBase  
from email import encoders  


smtp_info = {
    'smtp_server': 'smtp.qq.com',  # 替换为对应的邮件服务器,这是qq的,还有smtp.163.com等
    'port': 465,
    'sender_email': 'lytcreate@qq.com',  # 替换为发件邮箱账号
    'password': 'xxxxxxxxxxxxxxxx',  # 替换为你自己的授权码
    'receivers': ['lytcreate@163.com', 'lytcreat@163.com']  # 替换为接收人邮箱
}

def send_main(smtp_info, subject, content, img_path, excel_path):
    # 设置SMTP服务器地址和端口
    smtp_server = smtp_info['smtp_server']
    port = smtp_info['port']  # 587:使用TLS加密, 465:SSL
    # 设置发件人和收件人信息
    sender_email = smtp_info['sender_email']  # 替换为你的QQ邮箱地址
    password = smtp_info['password']  # 替换为你的QQ邮箱授权码
    receivers = smtp_info['receivers']  # 替换为接收者的邮箱地址列表

    # 创建邮件对象
    message = MIMEMultipart()
    message['From'] = sender_email
    message['To'] = ', '.join(receivers)
    message['Subject'] = subject

    mail_body = f"""  
    <html>  
    <body>  
    <div>{content}</div>
    <img src="cid:image1" alt="Image" />  
    </body>  
    </html>  
    """
    message.attach(MIMEText(mail_body, 'html'))

    # 添加图片到邮件正文
    with open(img_path, 'rb') as f:  # 替换为你的图片路径
        img_data = f.read()
        img = MIMEImage(img_data, name=img_path.split('/')[-1])
        img.add_header('Content-ID', '<image1>')  # 设置Content-ID以便在HTML中引用
        message.attach(img)

        # 添加Excel文件作为附件
    with open(excel_path, 'rb') as f:  # 替换为你的Excel文件路径
        attachment = MIMEBase('application', 'vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        attachment.set_payload(f.read())
        encoders.encode_base64(attachment)
        attachment.add_header('Content-Disposition', 'attachment', filename=excel_path.split('/')[-1])  # 附件在邮件中的名称
        message.attach(attachment)

        # 发送邮件
    try:
        if port == 587:
            server = smtplib.SMTP(smtp_server, port)
            server.starttls()  # 端口为587时启动TLS模式
        else:
            server = smtplib.SMTP_SSL(smtp_server, port)
        server.login(sender_email, password)  # 登录到你的邮箱
        text = message.as_string()
        server.sendmail(sender_email, receivers, text)  # 发送邮件给多个接收者
        print('Email sent successfully!', subject)

    except Exception as e:
        print(f'Failed to send email: {e}', subject)


if __name__ == '__main__':
    subject = 'Python自动化测试报告'  # 邮件主题
    content = '这是一些测试内容'  # 邮件文字内容
    img_path = '123.jpg'      # 邮件正文图片
    excel_path = '123.xlsx'   # 邮件附件excel
    send_main(smtp_info, subject, content, img_path, excel_path)

发送成功效果图:

相关推荐
张龙6871 小时前
uv 全面指南:比 pip 快 100 倍的 Python 包管理器,从安装到团队落地
后端·python
@HNUSTer2 小时前
Python数据可视化科技图表绘制系列教程(八)
python·数据可视化·科技论文·专业制图·科研图表
专注仿真3 小时前
问答大模型技术方案算法实现-熵权法融合算法 + 交叉编码器重排算法
人工智能·python·算法·语言模型·问答大模型关键算法·熵权融合算法·交叉编码器重排算法
雪碧聊技术3 小时前
使用Docker,将fastApi项目部署到linux服务器
服务器·docker·fastapi·docker部署fastapi
故乡dee云3 小时前
华为云国际服务器连不上怎么办?安全组、端口、IP 和网络排查方法
服务器·网络·华为云
满怀冰雪3 小时前
22-使用 PaddleClas 快速训练图像分类模型
人工智能·python·机器学习·分类·数据挖掘·paddlepaddle
gb42152874 小时前
python中Web应用服务器
开发语言·前端·python
佳児素花痴╮4 小时前
服务器模型、网络调试工具、网络协议头分析
服务器·网络·网络协议
reasonsummer5 小时前
【办公类-109-12】20260901圆形挂牌(模版重制:文本框圆形重叠_word编辑单面_接送卡&被子卡&床卡&入园卡)
python·word