这篇文章主要介绍“如何用Python实现邮箱自动群发工资条”,在日常操作中,相信很多人在如何用Python实现邮箱自动群发工资条问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用Python实现邮箱自动群发工资条”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
openpyxl
smptlib
email.mime.text
email.header
wb = load_workbook('数据表.xlsx')
sheet = wb.active
for row in sheet:
for cell in row:
print(cell.value)
使用字符串拼接成html
for row in sheet:
tbody = '<tr>'
cnt += 1
if cnt == 1:
for cell in row:
thead += f'<th>{cell.value}</th>'
thead += '</thead>'
else:
for cell in row:
tbody += f'<td>{cell.value}</td>'
tbody += '</tr>'
name = row[0].value
mail = row[1].value
# 2.编写邮件内容
content = f'''
<h4>{name},你好</h4>
<p>请查收你在2025年 5月1日 - 5月31 日的工资</p>
<table border='1px solid black'>
{thead}
{tbody}
</table>
'''
# 发送邮件
class Test:
def ck_log(self):
pass
def send_email(self, econtent, ename, mail):
host = 'smtp.qq.com'
user = '你的邮箱'
password = '你的授权码'
receivers = [mail]
subject = '员工工资表'
msg = MIMEText(econtent, 'html', 'utf-8')
msg['From'] = Header('有限公司')
msg['To'] = Header(ename)
msg['Subject'] = Header(subject, 'utf-8')
try:
obj = smtplib.SMTP_SSL(host, 465)
obj.login(user, password)
obj.sendmail(user, receivers, msg.as_string())
print("邮件发送成功!")
except smtplib.SMTPException as e:
print("Error: 无法发送邮件")
print(e)
from openpyxl import load_workbook
import smtplib
from email.mime.text import MIMEText
from email.header import Header
'''
1.获取excel表的数据
2.编写邮件内容
3.发送邮件
'''
# 发送邮件
class Test:
def ck_log(self):
pass
def send_email(self, econtent, ename, mail):
host = 'smtp.qq.com'
user = '1479898695@qq.com'
password = 'bijoplffwqqlbaci'
receivers = [mail]
subject = '员工工资表'
msg = MIMEText(econtent, 'html', 'utf-8')
msg['From'] = Header('有限公司')
msg['To'] = Header(ename)
msg['Subject'] = Header(subject, 'utf-8')
try:
obj = smtplib.SMTP_SSL(host, 465)
obj.login(user, password)
obj.sendmail(user, receivers, msg.as_string())
print("邮件发送成功!")
except smtplib.SMTPException as e:
print("Error: 无法发送邮件")
print(e)
if __name__ == '__main__':
wb = load_workbook('数据表.xlsx')
o = Test()
cnt = 0
sheet = wb.active
thead = '<thead>'
# 1.获取excel表的数据
for row in sheet:
tbody = '<tr>'
cnt += 1
if cnt == 1:
for cell in row:
thead += f'<th>{cell.value}</th>'
thead += '</thead>'
else:
for cell in row:
tbody += f'<td>{cell.value}</td>'
tbody += '</tr>'
name = row[0].value
mail = row[1].value
# 2.编写邮件内容
content = f'''
<h4>{name},你好</h4>
<p>请查收你在2025年 5月1日 - 5月31 日的工资</p>
<table border='1px solid black'>
{thead}
{tbody}
</table>
'''
# 3.发送邮件
if cnt == 3:
print('content:', content)
print(name, mail)
o.send_email(content, name, mail)
到此,关于“如何用Python实现邮箱自动群发工资条”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。