前言
每逢工资发放日,便是财务痛苦时~~
没啥文采,不过上面这句话却道出了人在不停重复劳作时,一种痛苦的心态。
身为程序猿的我们,这个时候当然要挺身而出啦,话不多说,撸起袖子写吧。
代码实现
import openpyxl
from openpyxl import load_workbook
import yagmail
import keyring
from datetime import *
wb = load_workbook("test.xlsx", data_only = True)
sheet = wb.active
yagmail.register("997131679@qq.com", "AAA")
pwd = keyring.get_password("yagmail", "997131679@qq.com")
yag = yagmail.SMTP(user="997131679@qq.com", host="smtp.qq.com", password=pwd)
count=0
table_header="<thead>"
for row in sheet:
count += 1
if count == 1:
for cell in row:
if cell.column != "B":
table_header += f"<th>{cell.value}</th>"
table_header += "</thead>"
continue
else:
row_text = ""
for cell in row:
if cell.column == "B":
continue
row_text += f"<td>{cell.value}</td>"
row_text += "</tr>"
name = row[0].value
email = row[1].value
contents = f"""
<h3>{name}, hello: </h3>
<p>Please check your {date.today().year}-{date.today().month} salary.</p>
<table border="1px solid black">{table_header}{row_text}</table>
"""
yag.send(f"{email}", f"Wenfang limited company{date.today().year}-{date.today().month}-salary status", contents)
print(f"{name}发送完毕")
print("所有发送完毕")
注意点:yagmail.register(“997131679@qq.com”, “AAA”),第二个参数"AAA"是需要去QQ邮箱设置里获取的,步骤见下图所示:
- 点击设置
- 切换到【账户】一栏,点击开启SMTP
会出现弹层,提示发送信息给对应的账户,短信发送成功后,即可获取到参数"AAA"的值
运行python文件,出现如下结果
查看邮箱,已发现新邮件