前言
在现代工作环境中,我们经常需要向多个收件人发送个性化的邮件。通过使用Python编程语言,我们可以自动化这个过程,从Excel文件中读取收件人和相关数据,并发送定制的邮件。
首先,导入所需的库:
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 from email.header import Header
然后,设置发件人邮箱和密码:
sender_email = '[email protected]' sender_password = 'your_password'
接下来,设置SMTP服务器和端口号(根据你使用的邮件服务提供商):
smtp_server = 'smtp.example.com' smtp_port = 587
创建SMTP连接并登录到邮箱:
server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # 开启TLS加密 server.login(sender_email, sender_password)
读取原始Excel文件:
df = pd.read_excel('path_to_excel_file.xlsx')
获取唯一的员工姓名列表:
employee_names = df['员工姓名'].unique()
获取员工姓名和对应的邮箱地址,假设这些信息存储在一个字典中:
employee_emails = { '张三': '[email protected]', '李四': '[email protected]', '王五': '[email protected]', # 添加更多员工和邮箱信息 }
遍历员工数据并发送邮件:
for employee_name in employee_names: employee_data = df[df['员工姓名'] == employee_name] # 创建员工的数据 # 生成员工的 Excel 文件 employee_data_filename = f'{employee_name}.xlsx' employee_data.to_excel(employee_data_filename, index=False) # 创建邮件 msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = employee_emails.get(employee_name, '') # 根据员工姓名获取邮箱 msg['Subject'] = '拆分数据通知' body = f"尊敬的{employee_name},您的拆分数据已经准备好,请查收附件。" msg.attach(MIMEText(body, 'plain')) # 添加附件 with open(employee_data_filename, 'rb') as file: part = MIMEApplication(file.read(), Name=os.path.basename(employee_data_filename)) part.add_header('Content-Disposition', 'attachment', filename=Header(os.path.basename(employee_data_filename), 'utf-8').encode()) msg.attach(part) # 发送邮件 server.sendmail(sender_email, employee_emails.get(employee_name, ''), msg.as_string()) # 删除生成的员工数据文件 os.remove(employee_data_filename) # 退出SMTP连接 server.quit()
最后,关闭与SMTP服务器的连接。
总结
通过上述Python脚本,我们可以批量发送个性化的邮件。我们首先设置发件人邮箱和密码,然后指定SMTP服务器和端口号。接下来,我们读取包含员工信息的Excel文件,并获取唯一的员工姓名列表和对应的邮箱地址。然后,我们遍历员工数据,并为每个员工创建邮件,附带相应的附件。最后,我们通过SMTP服务器发送邮件,并在发送完成后删除生成的员工数据文件。
完整代码:
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 from email.header import Header # 设置发件人邮箱和密码 sender_email = '[email protected]' sender_password = '授权码' # 设置SMTP服务器和端口(QQ邮箱的SMTP服务器和端口) smtp_server = 'smtp.163.com' smtp_port = 25 # 创建SMTP连接 server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # 开启TLS加密 # 登录邮箱 server.login(sender_email, sender_password) # 读取原始 Excel 文件 df = pd.read_excel('C:\\Users\\liuchunlin2\\Desktop\\测试数据\\员工.xlsx') # 获取唯一的员工姓名列表 employee_names = df['员工姓名'].unique() # 获取员工姓名和对应的邮箱地址,假设这些信息存储在一个字典中 employee_emails = { '刘备': '[email protected]', '孙权': '[email protected]', '曹操': '[email protected]', # 添加更多员工和邮箱信息 } # 遍历员工数据并发送邮件 for employee_name in employee_names: # 创建员工的数据 employee_data = df[df['员工姓名'] == employee_name] # 生成员工的 Excel 文件 employee_data_filename = f'{employee_name}.xlsx' employee_data.to_excel(employee_data_filename, index=False) employee_data_filename = f'{employee_name}.xlsx' # 创建邮件 msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = employee_emails.get(employee_name, '') # 根据员工姓名获取邮箱 msg['Subject'] = '拆分数据通知' body = f"尊敬的{employee_name},您的拆分数据已经准备好,请查收附件。" msg.attach(MIMEText(body, 'plain')) # 添加附件 with open(employee_data_filename, 'rb') as file: part = MIMEApplication(file.read(), Name=os.path.basename(employee_data_filename)) part.add_header('Content-Disposition', 'attachment', filename=Header(os.path.basename(employee_data_filename), 'utf-8').encode()) msg.attach(part) # 发送邮件 server.sendmail(sender_email, employee_emails.get(employee_name, ''), msg.as_string()) # 删除生成的员工数据文件 os.remove(employee_data_filename) # 退出SMTP连接 server.quit()
标签:Python,server,员工,个性化,employee,filename,data,email,邮件 From: https://www.cnblogs.com/lcl-cn/p/17801752.html