admin管理员组

文章数量:1559079

效果展示:

脚本优点:

  1. 可以自己手动通过弹窗选择对应工资条表格(函数在里面不影响)

  1. 自动通过标题行“姓名”“邮箱”找到对应信息,表格经常变动不影响

  1. 脚本使用腾讯企业邮箱指定的smtp服务(通过ssl连接)

  1. 表格美观大方,竖排版,可以自己加上base64企业logo

  1. 发送成功与失败会有txt记录,方便发生错误追踪

脚本代码:

# -- coding: utf-8 --
from openpyxl import load_workbook
import smtplib
from email.mime.text import mimetext
from email.header import header
import sys
import time
import random
class test:
    def ck_log(self):
        pass
    def send_email(self, content, name, mail,sender,smtpobj,subject):
        receivers = [mail]
        print(sender)
        print(receivers)
        msg = mimetext(content, 'html', 'utf-8')
        msg['from'] = header("xxx公司")
        msg['to'] = header(name)
        msg['subject'] = header(subject, 'utf-8')
        try:
            smtpobj.sendmail(sender, receivers, msg.as_string()) 
            print(f"===发送至 {name} 成功!")
            with open("成功名单.txt","a ") as f:
                f.write(f"{name}\n")
        except smtplib.smtpexception as e:
            print(f"===发送至 {name} 失败:" str(e))
            with open("失败名单.txt","a ") as f:
                f.write(f"{name}\n")
def get_decimal_places(num):
    import decimal
    if '.' in str(num):
        num = str(float(num))   
    d = decimal.decimal(str(num))
    #print("位数:",abs(d.as_tuple().exponent))
    return abs(d.as_tuple().exponent)
if __name__ == '__main__':
    user = "" #请输入邮箱账号
    password = "" #请输入邮箱密码
    try:
        smtpobj = smtplib.smtp_ssl(host="smtp.exmail.qq", port=465) 
        print("连接邮箱服务器成功!")
        smtpobj.login(user=user, password=password)
        print("登录账号成功!")
        subject = input("请输入邮件名(例如 2023年02月工资条):")
        print("您输入的是:", subject)
        import tkinter as tk
        from tkinter import filedialog
        print("请在弹出窗口中选择工资表")
        root = tk.tk()
        root.withdraw()
        filepath = filedialog.askopenfilename()
        print('filepath:', filepath)
        wb = load_workbook(filepath, read_only=true, data_only=true)
        print("表格下的所有sheet:")
        print(wb.sheetnames)
        o = test()
        cnt = 0
        sheetname = input("请输入工资条所在的sheet名字(去除引号):")
        if "'" in sheetname:
            sheetname = str(eval(sheetname))
            print("修正输入:", sheetname)
        else:
            print("您输入的是:", sheetname)
        sheet = wb[sheetname]
        thead = ''
        sender = user
        for row in sheet:
            tbody = ''
            cnt  = 1
            if cnt == 1:
                topic_list = []
                for cell in row:
                    topic_list.append(cell.value)
                print(topic_list)
                print(len(topic_list))
                for w in range(len(topic_list)):
                    if topic_list[w] == "姓名":
                        name_row = w
                        print(name_row)
                    elif topic_list[w] == "邮箱":
                        mail_row = w
                        print(mail_row)
            else:
                for num in range(len(row)):
                    if row[num].value == none or row[num].value == 0 or row[num].value == "0":
                        inside = "-"
                        tbody  = f'{topic_list[num]}{inside}'
                    elif type(row[num].value) == type(20.00) and get_decimal_places(row[num].value) >= 5:
                        tbody  = f'{topic_list[num]}{round(row[num].value, 2)}'
                    else:
                        tbody  = f'{topic_list[num]}{row[num].value}'
            name = row[name_row].value
            mail = row[mail_row].value
            content = f'''
                

{name},您好

''' '''

请查收您的工资条,有问题请及时与xx取得联系,谢谢合作!

''' f''' {thead} {tbody}
''' #print(content) if cnt >= 2: print(name, mail) o.send_email(content, name, mail,sender,smtpobj,subject) else: print("标题行不处理!") some_time = random.randint(4, 8) print(f"等待{some_time}秒") time.sleep(some_time) smtpobj.quit() print("群发完毕,已退出邮箱登录...") input("please enter to close this exe:") except exception as e: print(f"运行出错,失败原因:{e}....") time.sleep(600) sys.exit(0)

本文标签: 腾讯企业邮箱