發(fā)工資啦!教你用Python實現(xiàn)郵箱自動群發(fā)工資條
更新時間:2021年05月10日 14:29:59 作者:Harris-H
這篇文章主要介紹了發(fā)工資啦!教你用Python實現(xiàn)郵箱自動群發(fā)工資條,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
一、excel的內(nèi)容
二、效果
三、需要用的庫:
- openpyxl
- smptlib
- email.mime.text
- email.header
四、實現(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)
到此這篇關(guān)于發(fā)工資啦!教你用Python實現(xiàn)郵箱自動群發(fā)工資條的文章就介紹到這了,更多相關(guān)Python自動群發(fā)工資條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python虛擬環(huán)境庫virtualenvwrapper安裝及使用
這篇文章主要介紹了Python虛擬環(huán)境庫virtualenvwrapper安裝及使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06python3+PyQt5重新實現(xiàn)自定義數(shù)據(jù)拖放處理
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5重新實現(xiàn)自定義數(shù)據(jù)拖放處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04python中時間轉(zhuǎn)換datetime和pd.to_datetime詳析
這篇文章主要給大家介紹了關(guān)于python中時間轉(zhuǎn)換datetime和pd.to_datetime的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08