共计 1293 个字符,预计需要花费 4 分钟才能阅读完成。
Excel 文件格式
姓名 | 部门 | 基本工资 | 提成 | 邮箱 |
---|---|---|---|---|
刘备 | 首领 | 3000 | 300 | 666@qq.com |
张飞 | 小兵 | 1000 | 100 | 666@qq.com |
代码
from openpyxl import load_workbook
from email.mime.text import MIMEText
from email.utils import formataddr
from email.header import Header
import smtplib
## Excel 文件地址
wb = load_workbook('./000.xlsx')
sh = wb.active
## 修改自己的 SMTP 服务器信息
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
server.login("666@qq.com", "K0000000iFm")
title = '<tr>'
for i, row in enumerate(sh.rows):
if i == 0:
for ceil in row:
title += f'<td>{ceil.value}</td>'
title += '</tr>'
else:
info = '<tr>'
for ceil in row:
info += f'<td>{ceil.value}</td>'
info += '</tr>'
name = row[0].value
to_email = row[4].value
msg = f'''
<h3> 您好:{name}</h3>
<p> 请查收本月工资信息详情 </p>
<table border='1'>
{title}
{info}
</table>
'''msg_body = MIMEText(msg,'html','utf-8')
msg_body['From'] = formataddr(['人事部', '10086@qq.com'])
msg_body['Subject'] = Header('工资条', 'utf-8')
try:
server.sendmail("666@qq.com", to_email, msg_body.as_string())
print(f'成功发送工资条:{name}')
except smtplib.SMTPException:
print("Error: 无法发送邮件 ")
server.quit()
Zmail
使得在 Python 3 中发送和接收邮件变得更简单。安装命令:pip install zmail
。
import zmail
def base_use():
server = zmail.server(
"mail@qq.com",
"123456",
smtp_host="smtp.qq.com",
smtp_port=587,
smtp_tls=True,
smtp_ssl=False,
)
info = {
"subject": " 主题 ",
"from": "Aboss <mail@aboss.top>",
"content_text": " 邮件 \n 内容 ",
"attachments": ["1.jpg", "2.jpg"],
}
server.send_mail("qq@qq.com", info)
if __name__ == "__main__":
base_use()
正文完