# 导入所需模块
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import os

# 定义发件人和 SMTP 服务器信息
sender = 'example@example.com'
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp_username = 'example_username'
smtp_password = 'example_password'

# 连接 SMTP 服务器并登录
smtpObj = smtplib.SMTP(smtp_server, smtp_port)
smtpObj.starttls()
smtpObj.login(smtp_username, smtp_password)

# 读取 Excel 文件并遍历每一行
df = pd.read_excel(r'C:\Users\LOH2HZ\Desktop\2023.6 email sending\Emails.xlsx')
subtables_path = r'C:\Users\LOH2HZ\Desktop\2023.6 email sending\SubTables'  # 定义子表文件夹的路径
for i, row in df.iterrows():
    # 获取 Project manager 的姓名和邮箱
    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.docx">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)
        # 发送邮件给该 Project manager
        smtpObj.sendmail(sender, pm_email, msg.as_string())
    else:
        print(f'Subtable for {pm_name} not found')

# 关闭连接
smtpObj.quit()

该代码使用 Python 脚本实现自动发送邮件,邮件内容提醒 Project Manager 处理 ProNovia 项目的开放组件,并附带相应的子表文件。

代码功能:

  1. 导入模块: 导入必要的模块,包括 pandas 用于处理 Excel 文件,smtplib 用于发送邮件,email.mime 用于构建邮件内容和附件,os 用于操作文件系统。
  2. 定义发件人和 SMTP 服务器信息: 设置发送邮件所需的账户信息,包括发件人邮箱地址、SMTP 服务器地址、端口号、用户名和密码。
  3. 连接 SMTP 服务器并登录: 使用 smtplib 模块连接 SMTP 服务器,并使用用户名和密码进行登录。
  4. 读取 Excel 文件: 使用 pandas 模块读取包含 Project Manager 信息的 Excel 文件。
  5. 遍历每一行并发送邮件: 循环遍历 Excel 文件的每一行,获取 Project Manager 的姓名和邮箱地址。检查信息是否完整,如果缺少信息则跳过该行。
  6. 构建邮件内容: 创建邮件内容,包括邮件主题、发件人、收件人,以及邮件正文,使用 HTML 格式来使邮件内容更美观。
  7. 添加附件: 从子表文件夹中找到对应 Project Manager 的子表文件,将其添加为邮件附件。
  8. 发送邮件: 使用 smtpObj.sendmail() 方法将邮件发送给目标 Project Manager。
  9. 关闭连接: 发送完所有邮件后,关闭与 SMTP 服务器的连接。

使用方法:

  1. 确保已经安装必要的 Python 模块。
  2. 修改代码中的发件人和 SMTP 服务器信息。
  3. 将 Excel 文件和子表文件放在相应的路径下。
  4. 运行代码,即可自动发送邮件。

代码说明:

  1. 代码中使用了 os.path.join 方法来构建文件路径,避免了路径错误。
  2. 代码中使用了 pd.isna 方法来判断 Excel 文件中的数据是否为空,防止出现错误。
  3. 代码中使用了 HTML 格式来构建邮件内容,使邮件内容更美观。
  4. 代码中使用了 MIMEApplication 类来添加邮件附件,并设置了附件的 Content-Disposition 属性,以确保附件能够被正确打开。
  5. 代码中使用了 smtpObj.quit 方法来关闭与 SMTP 服务器的连接,避免资源浪费。

其他建议:

  1. 可以使用更强大的邮件库,例如 yagmail,来简化邮件发送过程。
  2. 可以使用日志记录模块,记录邮件发送过程中的错误信息,方便调试。
  3. 可以添加邮件发送成功和失败的提示信息,提高用户体验。

注意:

  1. 代码中的邮箱地址、用户名和密码需要替换为您的实际信息。
  2. 确保您已经设置了 SMTP 服务器的安全性,例如使用 SSL/TLS 协议。
  3. 请勿将您的邮箱密码直接写入代码中,以防止安全风险。

该脚本可以帮助您轻松实现自动发送邮件带附件功能,提高工作效率,并节省时间和精力。

Python 自动发送邮件带附件:ProNovia 项目提醒

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

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