Bootstrap SomeTools Icon
返回
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import formataddr
from email.header import Header
from configparser import ConfigParser


def send_message(receiver_email, subject, content, pic_path=[]):
    global server
    config = ConfigParser()
    config.read('config.ini',encoding='utf-8')
    sender_email = config.get('Settings', 'send_email')
    auth_code = config.get('Settings', 'code')
    smtp_server = config.get('Settings', 'smtp_server')
    smtp_port = config.get('Settings', 'smtp_port')
    sender_name = config.get('Settings', 'sender_name')
    receiver_name = config.get('Settings', 'receiver_name')
    try:
        if len(pic_path) == 0:
            # 创建邮件对象
            msg = MIMEText(content, 'plain', 'utf-8')  # 邮件内容,plain表示纯文本
            msg['From'] = formataddr([sender_name, sender_email])  # 发件人信息
            msg['To'] = formataddr([receiver_name, receiver_email])  # 收件人信息
            msg['Subject'] = Header(subject, 'utf-8')  # 邮件标题

            # 使用SSL连接到SMTP服务器
            server = smtplib.SMTP_SSL(smtp_server, smtp_port)
            # 登录QQ邮箱
            server.login(sender_email, auth_code)
            # 发送邮件
            server.sendmail(sender_email, [receiver_email], msg.as_string())
            print("邮件发送成功")

        else:
            # 创建邮件对象
            msg = MIMEMultipart()
            msg['From'] = formataddr([sender_name, sender_email])  # 发件人信息
            msg['To'] = formataddr([receiver_name, receiver_email])  # 收件人信息
            msg['Subject'] = Header(subject, 'utf-8')  # 邮件标题

            # 添加邮件正文
            msg.attach(MIMEText(content, 'plain', 'utf-8'))

            # # 打开图片文件并将其附加为图片附件
            # with open(pic_path, 'rb') as img_file:
            #     img = MIMEImage(img_file.read())
            #     img.add_header('Content-Disposition', 'attachment', filename=pic_path.split('/')[-1])
            #     msg.attach(img)
            for image_path in pic_path:
                with open(image_path, 'rb') as img_file:
                    img = MIMEImage(img_file.read())
                    img.add_header('Content-Disposition', 'attachment', filename=image_path.split('/')[-1])
                    msg.attach(img)
            # 使用SSL连接到SMTP服务器
            server = smtplib.SMTP_SSL(smtp_server, smtp_port)
            # 登录QQ邮箱
            server.login(sender_email, auth_code)
            # 发送邮件
            server.sendmail(sender_email, [receiver_email], msg.as_string())
            print("图片邮件发送成功")
    except Exception as e:
        print(f"Error occurred: {e}")
    finally:
        # 关闭服务器连接
        server.quit()
file_path = ["./pic/吗喽.jpg", "./pic/tree.png"]
send_message(receiver_email='2144008161@qq.com', subject="1", content="222", pic_path=file_path)

ini文件内容:
[Settings]
send_email=2645816460@qq.com
code=ushclrbwkozcdjdi
smtp_server=smtp.qq.com
smtp_port=465
sender_name=EVA
receiver_name="执行者"