您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何使用Python郵箱自動群發(fā)工資條,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Python主要應(yīng)用于:1、Web開發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲;4、嵌入式應(yīng)用開發(fā);5、游戲開發(fā);6、桌面應(yīng)用開發(fā)。
openpyxl
smptlib
email.mime.text
email.header
wb = load_workbook('數(shù)據(jù)表.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.編寫郵件內(nèi)容 content = f''' <h4>{name},你好</h4> <p>請查收你在2025年 5月1日 - 5月31 日的工資</p> <table border='1px solid black'> {thead} {tbody} </table> '''
# 發(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''' <h4>{name},你好</h4> <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)于如何使用Python郵箱自動群發(fā)工資條就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。