参考楼主发的链接,简单调整了下。支持自动识别列来作为邮件内容。 三个文件: main.py:群发工资条 esender.py:邮件工具 data_tpl.csv: 示例数据 下载后修改main.py中的发送邮件配置和data.csv即可。 特别说明: 需要将工资表格另存为csv文件,并重命名为data.csv 不支持多行合并表头,请简化为单行单列表头,参考data_tpl.csv 发送邮箱测试过qq邮箱,其他的自行测试 esender.py import smtplib from email.mime.text import MIMEText from email.header import Header import re import base64 class EmailSender: def __init__(self, user,user_name, password): self.user = user self.user_name = user_name self.password = password self.smtp_server = self._get_smtp_server(user) def _get_smtp_server(self, user): # 根据邮箱地址判断SMTP服务器 if re.match(r'.*@qq\.com$', user): return 'smtp.qq.com' elif re.match(r'.*@163\.com$', user): return 'smtp.163.com' elif re.match(r'.*@example\.com$', user): # 示例企业邮箱,需要替换为实际的企业邮箱域名 return 'smtp.example.com' else: raise ValueError("Unsupported email domain") def send_email(self, reciver_name, receiver, cc_name, cc, subject, content): message = MIMEText(content, 'html', 'utf-8') if self.smtp_server == 'stmp.qq.com': message['From'] = Header(f'''=?UTF-8?B?{base64.b64encode(self.user_name.encode("utf-8"))}?= <{self.user}>''') else: message['From'] = Header(f'''{self.user_name} <{self.user}>''', 'utf-8') message['To'] = Header(f'''{reciver_name} <{receiver}>''', 'utf-8') message["Cc"] = Header(f'''{cc_name} <{cc}>''', "utf-8") message['Subject'] = Header(subject, 'utf-8') try: if self.smtp_server in ['smtp.qq.com', 'smtp.163.com']: # 使用SSL方式登录邮箱,发送邮件,端口通常是465 with smtplib.SMTP_SSL(self.smtp_server, 465) as smtp_obj: smtp_obj.login(self.user, self.password) smtp_obj.sendmail(self.user, [receiver, cc], message.as_string()) else: # 对于企业邮箱或其他邮箱,可能需要使用不同的端口和加密方式,这里只是示例 with smtplib.SMTP(self.smtp_server, 587) as smtp_obj: smtp_obj.starttls() # 启动TLS加密,端口通常是587 smtp_obj.login(self.user, self.password) smtp_obj.sendmail(self.user, [receiver, cc], message.as_string()) print('邮件发送成功!') except smtplib.SMTPException as e: print('邮件发送失败,错误信息:', e)main.py #引用系统操作模块 import os # 导入邮件发送方法 import esender # 引入时间方法 import datetime import time now_time = datetime.datetime.now() # 获得当前时间 pay_month = datetime.datetime.now() - datetime.timedelta(28) sent_time = time.time() # 发送者邮箱配置 sender_name = "财务" sender_email = "caiwu@qq.com" sender_email_pwd = "rumpbwnkpbzkbxxx" # 抄送邮箱配置 cc_name = "领导" cc = "lindao@qq.com" #声明文件 print(f''' **************************************************************************************** 使用说明: 1、用于批量发送工资条,自动识别数据列 2、文件请另存为csv文件 3、格式要求:姓名必须为第二列,邮箱必须为组后一列 ''') #判断文件是否存在 if os.path.exists("data.csv"): wb = open("data.csv", mode='r', encoding="utf-8") else: input("数据文件 data.csv 不存在,请检查后重新运行本程序,程序结束,按回车退出程序") exit() # 登录邮件服务器 # 使用示例 email_sender = esender.EmailSender(sender_email, sender_name, sender_email_pwd) # 读取表头 header_txt = wb.readline() header = header_txt.split(",") thead_html = "" for th in header[:-1]: thead_html += f''' <th>{th.strip()}</th> ''' # 读取数据并发送邮件 for row_txt in wb.readlines(): row = row_txt.split(",") mail_content = "" tbody_html = "" for td in row[:-1]: tbody_html += f''' <td>{td.strip()}</td> ''' # 注意!姓名必须为第二列 name = row[1].strip() # 注意!邮箱必须为组后一列 reciver = row[-1].strip() # 构建邮件内容 mail_content = f'''<p> {name} 你好!{pay_month.year}年{pay_month.month}月工资表 </p> <table> <caption> <h2> {name}{pay_month.year}年{pay_month.month}月工资表</h2> </caption> <thead><tr>{thead_html}</tr></thead> <tbody><tr>{tbody_html}</tr></tbody> </table> ''' # 发送邮件 email_sender.send_email(name, reciver,cc_name, cc, f"{name} {pay_month.year}年{pay_month.month}月份工资条", mail_content) # 写日志 log = f"发送时间:{now_time} 收件人:{name} 邮件地址:{reciver} 邮件主题:{pay_month.year}年{pay_month.month}月份工资表记录 己发送\n" f = open(file='sentlog.log', mode='a', encoding='utf-8') f.write(log) f.close() print(log) wait = input("程序执行完毕,按回车键退出!")data_tpl.csv 姓名,工龄工资,补贴1,补贴2,补贴3,补贴4,补贴5,总计,电子邮箱 张三,1100,20,20,1001,20,100,100,123456@qq.com 李四,1100,20,20,1001,20,100,100,123456@qq.com (提示:威海市麦克技术服务有限公司为您提供威海市专业的劳务派遣公司、威海市劳务外包公司、威海市最好的劳务派遣公司、威海市劳务服务外包公司、威海市物业管理服务、威海市保安服务、威海市人事代理、威海市人力资源外包服务公司、威海市区域人力资源服务商) |