SMTP对象没有select_folder()方法,因此需要使用IMAP库来访问并操作邮件文件夹。您可以使用IMAPClient库来实现此功能。以下是修改后的代码:

import os
from imapclient import IMAPClient

subtables_path = r"C:\Users\LOH2HZ\Desktop\2023.6 email sending\SubTables"  # 定义子表文件夹的路径

# 遍历 Excel 文件的每一行并附加相应的子表
for i, row in df.iterrows():  
    pm_name = row['Project manager']
    pm_email = row['Email address']
    
    if pd.isna(pm_name) or pd.isna(pm_email):
        print(f'Missing information for row {i}')
        continue

    msg = MIMEMultipart() # 清空上一个邮件的附件
    msg['Subject'] = 'Open ProNovia E-mail reminder'
    msg['From'] = sender
    msg['To'] = pm_email

    content='''
    Dear PJMs & VS managers,<br>
    <br>
    This is a warm E-mail reminder about open ProNovia process of new components in your project.<br>
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>·</strong>&nbsp;You can check your ProNovia status and push relevant associates to fast release components.<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(LINK:<a href="file://bosch.com/DfsRB/DfsCN/LOC/Hz/PTCN/Engineering/PT-BE-ETS-HZ_Act/EIS_Report/Pronovia/Reference/How%20to%20check%20components%20are%20stuck%20in%20which%20department.pdf">How to check components are stuck in which department</a>)<br>
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>·</strong>&nbsp;You can issue a CR to discontinue components if they are not needed any more or project cancelled.<br>
    <br>
    Let's make our effort together to shorten the components release duration!<br>
    <br>Thanks.<br>
    <br>PT-BE/ETS1-Hz Team<br>
    '''

    puretext = MIMEText(content,'html','utf-8') # plain html
    msg.attach(puretext)

    excel_path = os.path.join(subtables_path, f"{pm_name}.xlsx")
    if os.path.exists(excel_path):
        xlsxpart = MIMEApplication(open(excel_path, 'rb').read())
        xlsxpart.add_header('Content-Disposition', 'attachment', filename=f'{pm_name}.xlsx')
        msg.attach(xlsxpart)
        smtpObj.sendmail(sender, pm_email, msg.as_string()) # 发送邮件给该Project manager
    else:
        print(f'Subtable for {pm_name} not found')


smtpObj.quit()  # 发送完所有的邮件后关闭连接


# 保存邮件到已发送文件夹
imap_server = 'imap.example.com'
imap_username = 'your_username'
imap_password = 'your_password'

with IMAPClient(imap_server) as client:
    client.login(imap_username, imap_password)
    client.select_folder('Sent Items')  # 选择已发送文件夹
    client.append('Sent Items', msg.as_bytes())  # 将邮件保存到已发送文件夹中

请确保将imap_serverimap_usernameimap_password替换为您的实际IMAP服务器地址,用户名和密码

import ossubtables_path = rCUsersLOH2HZDesktop20236 email sendingSubTables # 定义子表文件夹的路径# 遍历 Excel 文件的每一行并附加相应的子表for i row in dfiterrows pm_name = rowProject manager pm_email = rowEmail addres

原文地址: https://www.cveoy.top/t/topic/hHD2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录