您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。