gpt4 book ai didi

发工资啦!教你用Python实现邮箱自动群发工资条

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章发工资啦!教你用Python实现邮箱自动群发工资条由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1、excel的内容

发工资啦!教你用Python实现邮箱自动群发工资条

2、效果

发工资啦!教你用Python实现邮箱自动群发工资条

3、需要用的库:

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

4、实现步骤

4.1 获取excel表的数据

?
1
2
3
4
5
wb = load_workbook( '数据表.xlsx' )
sheet = wb.active
for row in sheet:
     for cell in row:
         print (cell.value)

4.2 编写邮件内容

使用字符串拼接成html 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 发送邮件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 发送邮件
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)

5、所有代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 '''
             <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自动群发工资条内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/weixin_45750972/article/details/116525212 。

最后此篇关于发工资啦!教你用Python实现邮箱自动群发工资条的文章就讲到这里了,如果你想了解更多关于发工资啦!教你用Python实现邮箱自动群发工资条的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com