Python 邮件发送脚本 - 自动发送 ProNovia 提醒邮件并附件子表
import os
import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
subtables_path = r'C:\Users\LOH2HZ\Desktop\2023.6 email sending\SubTables' # 定义子表文件夹的路径
# 假设 df 是一个 Pandas DataFrame 对象,包含项目经理姓名和邮箱地址
for i, row in df.iterrows(): # 遍历 Excel 文件的每一行并附加相应的子表
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 # 请将 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> <strong>·</strong> You can check your ProNovia status and push relevant associates to fast release components.<br>
(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.docx">How to check components are stuck in which department</a>)<br>
<br> <strong>·</strong> 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() # 发送完所有的邮件后关闭连接
代码解释:
-
导入必要的库
import os: 用于操作文件和目录import pandas as pd: 用于处理 Excel 文件from email.mime.multipart import MIMEMultipart: 用于创建邮件对象from email.mime.text import MIMEText: 用于创建邮件正文from email.mime.application import MIMEApplication: 用于创建邮件附件
-
定义子表文件夹路径
subtables_path = r'C:\Users\LOH2HZ\Desktop\2023.6 email sending\SubTables': 使用r前缀表示原始字符串,避免转义字符问题。
-
遍历 Excel 文件
for i, row in df.iterrows():: 遍历dfDataFrame 的每一行,i表示行索引,row表示当前行数据。
-
获取项目经理姓名和邮箱地址
pm_name = row['Project manager']: 获取当前行中名为 'Project manager' 的列的值。pm_email = row['Email address']: 获取当前行中名为 'Email address' 的列的值。
-
检查信息是否完整
if pd.isna(pm_name) or pd.isna(pm_email):: 检查pm_name或pm_email是否为空值,如果是则跳过当前行。
-
创建邮件对象
msg = MIMEMultipart(): 创建一个MIMEMultipart对象,用于存储邮件的各个部分。msg['Subject'] = 'Open ProNovia E-mail reminder': 设置邮件主题。msg['From'] = sender: 设置发件人邮箱地址。msg['To'] = pm_email: 设置收件人邮箱地址。
-
创建邮件正文
content='''...''': 定义邮件正文内容,使用三引号方便编写多行字符串。puretext = MIMEText(content,'html','utf-8'): 创建一个MIMEText对象,设置邮件正文的内容类型为 HTML,编码方式为 UTF-8。msg.attach(puretext): 将邮件正文添加到msg对象中。
-
创建邮件附件
excel_path = os.path.join(subtables_path, f"{pm_name}.xlsx"): 使用os.path.join()方法拼接子表文件路径。if os.path.exists(excel_path):: 检查子表文件是否存在,如果存在则将其作为附件添加到邮件中。xlsxpart = MIMEApplication(open(excel_path, 'rb').read()): 创建一个MIMEApplication对象,读取子表文件内容并将其作为附件。xlsxpart.add_header('Content-Disposition', 'attachment', filename=f'{pm_name}.xlsx'): 设置附件的头部信息,包括附件的显示方式、类型和文件名。msg.attach(xlsxpart): 将附件添加到msg对象中。
-
发送邮件
smtpObj.sendmail(sender, pm_email, msg.as_string()): 使用smtpObj对象的sendmail()方法发送邮件,其中sender是发件人邮箱地址,pm_email是收件人邮箱地址,msg.as_string()将邮件对象转换为字符串格式。
-
关闭连接
smtpObj.quit(): 发送完所有邮件后,关闭与邮件服务器的连接。
注意:
- 在实际使用中,你需要根据你的环境设置
sender变量和smtpObj对象,并安装必要的库。 - 确保子表文件夹路径正确,并且每个项目经理都有对应的子表文件。
- 可以根据你的需要修改邮件正文和附件的内容。
希望以上内容能够帮助你!如果你还有其他问题,请随时提问。
原文地址: https://www.cveoy.top/t/topic/oYJq 著作权归作者所有。请勿转载和采集!