python批量文件夹下多附件发送邮件问题


import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os, time

# 在这里填入信息
sender = 'xxx.com'      # 发送人邮箱,
authcode = 'xxx'  # 发送人邮箱授权码
smtpserver = 'smtp.qq.com'  # 例如smtp.126.com
smtpport = 465  # smtp服务端口,通常为465,可根据邮箱服务的信息进行更改
s = smtplib.SMTP_SSL(smtpserver, smtpport)
s.login(sender, authcode)

files = os.listdir("./1月10")
file_att=[]
for file in files:
    if '日出' in file:

        receivers = 'xxx@qq.com'  # 收件人的邮箱
        subject = '日出'  # 主题
        # 初始化信息对象
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = receivers
        msg.attach(MIMEText('邮件', 'plain', 'utf-8'))
        # 构造附件1,传送当前目录下文件
        file_att.append(MIMEText(open(f'./1月10/{file}','rb').read(),'base64','utf-8'))   # rb以二进制方式读取
        print(f'./1月10/{file}')


for a in range(len(file_att)):
    file_att[a]["Content-Disposition"] = f'attachment; filename = "{file}"'
    # 将附件添加到MIMEMultipart中
    msg.attach(file_att[a])


try:
    # s.sendmail(sender, receivers, msg.as_string())
    print('发送成功')
    time.sleep(1)
except Exception:
    print('发送失败')

代码没有报错 但是发送邮件接受到没有附件内容一直是空白的试了好久没有搞定 有知道只能批量发文件夹下多附件的邮件吗 谢谢

把 open 移到 append 语句外 , append 后 直接 attach 也不能解决?