發工資啦!教你用Python實現郵箱自動群發工資條

一、excel的內容

在這裡插入圖片描述

二、效果

在這裡插入圖片描述

三、需要用的庫:

  • openpyxl
  • smptlib
  • email.mime.text
  • email.header

四、實現步驟

4.1 獲取excel表的數據

wb = load_workbook('數據表.xlsx')
sheet = wb.active
for row in sheet:
	for cell in row:
        print(cell.value)

4.2 編寫郵件內容

使用字符串拼接成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'''
            <h3>{name},你好</h3>
            <p>請查收你在2025年 5月1日 - 5月31 日的工資</p>
            <table border='1px solid black'>
            {thead}
            {tbody}
            </table>
        '''

4.3 發送郵件

# 發送郵件
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 = '[email protected]'
        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'''
            <h3>{name},你好</h3>
            <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實現郵箱自動群發工資條的文章就介紹到這瞭,更多相關Python自動群發工資條內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: