對于小公司而言,幾個(gè)人的工資條直接手動編輯發(fā)送即可,但對于比較大型的企業(yè)而言,成百上千人的工資使用手動編輯發(fā)送是不現(xiàn)實(shí)的(哪怕這樣的公司財(cái)務(wù)也不會少,但是多個(gè)財(cái)務(wù)浪費(fèi)的總時(shí)間還是很多的)。在學(xué)習(xí)python辦公自動化中或許沒有學(xué)到如何自動群發(fā)郵件。但是python中有郵箱模塊,只需要結(jié)合辦公自動化相關(guān)知識將excel中的工資明細(xì)提取出來,然后使用郵箱模塊進(jìn)行批量發(fā)送,就能實(shí)現(xiàn)一個(gè)簡單的python自動群發(fā)功能。那么python怎么使用郵箱模塊來實(shí)現(xiàn)上述效果呢?請繼續(xù)看下文。
一、excel的內(nèi)容
二、效果
三、需要用的庫:
- openpyxl
- smptlib
- email.mime.text
- email.header
四、實(shí)現(xiàn)步驟
4.1 獲取excel表的數(shù)據(jù)
wb = load_workbook('數(shù)據(jù)表.xlsx')
sheet = wb.active
for row in sheet:
for cell in row:
print(cell.value)
4.2 編寫郵件內(nèi)容
使用字符串拼接成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.編寫郵件內(nèi)容
content = f'''
<h3>{name},你好</h3>
<p>請查收你在2025年 5月1日 - 5月31 日的工資</p>
<table border='1px solid black'>
{thead}
{tbody}
</table>
'''
4.3 發(fā)送郵件
# 發(fā)送郵件
class Test:
def ck_log(self):
pass
def send_email(self, econtent, ename, mail):
host = 'smtp.qq.com'
user = '你的郵箱'
password = '你的授權(quán)碼'
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("郵件發(fā)送成功!")
except smtplib.SMTPException as e:
print("Error: 無法發(fā)送郵件")
print(e)
五、所有代碼
from openpyxl import load_workbook
import smtplib
from email.mime.text import MIMEText
from email.header import Header
'''
1.獲取excel表的數(shù)據(jù)
2.編寫郵件內(nèi)容
3.發(fā)送郵件
'''
# 發(fā)送郵件
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("郵件發(fā)送成功!")
except smtplib.SMTPException as e:
print("Error: 無法發(fā)送郵件")
print(e)
if __name__ == '__main__':
wb = load_workbook('數(shù)據(jù)表.xlsx')
o = Test()
cnt = 0
sheet = wb.active
thead = '<thead>'
# 1.獲取excel表的數(shù)據(jù)
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.編寫郵件內(nèi)容
content = f'''
<h3>{name},你好</h3>
<p>請查收你在2025年 5月1日 - 5月31 日的工資</p>
<table border='1px solid black'>
{thead}
{tbody}
</table>
'''
# 3.發(fā)送郵件
if cnt == 3:
print('content:', content)
print(name, mail)
o.send_email(content, name, mail)
到此這篇python怎么使用郵箱模塊實(shí)現(xiàn)自動群發(fā)工資條的文章就介紹到這了,更多Python自動化的學(xué)習(xí)內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章。