python发送文本附件

最近有自动发送需要优化的图片资源列表需求,因此有了下面脚本

 1 #!/usr/local/bin/python3
 2 # coding:utf-8
 3 
 4 # ====================================================
 5 # Author: changbo - 541330702@qq.com
 6 # Last modified: 2022-01-19
 7 # Filename: piccount.py
 8 # Description:  find big pic and then sent to someone 
 9 # http://www.cnblogs.com/changbo
10 # ====================================================
11 
12 import os
13 import time
14 import threading
15 from email.mime.text import MIMEText
16 from smtplib import SMTP_SSL
17 from email.mime.multipart import MIMEMultipart
18 from email.mime.base import MIMEBase
19 from email import encoders
20 
21 
22 nowYear = time.strftime("%Y", time.localtime())
23 nowTime = time.strftime("%Y%m%d%H", time.localtime())
24 fileNmame = 'Big_pics' + nowTime + '.txt'
25 list_file = os.listdir("/usr/local/openresty/nginx/logs")
26 msg = MIMEText('hello,send by Python...', 'plain', 'utf-8')
27 fileNmame1 = '1' + fileNmame
28 mail_info = {
29     'From': 'xxxx@qq.com',
30     'Password': 'xxxxx',
31     'To': 'xxxxx@qq.com',
32     'Cc': 'xxxxx@qq.com',
33     'Mail_server': 'smtp.qq.com',
34     'Mail_text': '''
35         <html><body><h1>你好</h1>  <p>附件为xxxxxx</p>  
36         <p>with my best wishes!</p>
37         </body></html>
38 '''
39 }
40 
41 
42 def logFind():
43     for logfile in list_file:
44         if "2022" in logfile:
45             tempfile = "/usr/local/openresty/nginx/logs/%s" % logfile
46             bashcommd = 'cat %s|grep -v mp4|grep Uploadfile| tr -d ":|,"  | awk -F "\\""  \'{if(int($31)>1048576) print $16}\' | awk \'{print $2}\' | sort -n | uniq -c | sort -nr | head -n 10| awk \'{print $2}\'' % tempfile
47             tmpresult = os.popen(bashcommd)
48             resultpic = tmpresult.read()
49             print(resultpic)
50             with open('/root/scripts/' + fileNmame, 'a+') as f:
51                 f.write(resultpic)
52                 f.close()
53 
54 
55 def deWeight():
56     bashcommd2 = 'sort /root/scripts/%s |uniq -c |awk \'{print $2}\' |grep -v ^$ >> /root/scripts/%s' % (fileNmame, fileNmame1)
57     os.system(bashcommd2)
58 
59 
60 def sendEmail():
61     smtp = SMTP_SSL(mail_info['Mail_server'])
62 
63     smtp.ehlo(mail_info['Mail_server'])
64     smtp.login(mail_info['From'], mail_info['Password'])
65 
66     msg = MIMEMultipart()
67     msg['Subject'] = 'xxx图片优化'
68     msg['From'] = mail_info['From']
69     msg['To'] = mail_info['To']
70     msg['Cc'] = mail_info['Cc']
71 
72     msg.attach(MIMEText(mail_info['Mail_text'], 'html', 'utf-8'))
73     with open('/root/scripts/' + fileNmame1, 'rb') as f:
74         mime = MIMEBase('text/plain', 'txt', filename=fileNmame1)
75         mime.add_header('Content-Disposition', 'attachment', filename=fileNmame1)
76         mime.add_header('Content-ID', '<0>')
77         mime.add_header('X-Attachment-Id', '0')
78         mime.set_payload(f.read())
79         encoders.encode_base64(mime)
80         msg.attach(mime)
81 
82     smtp.sendmail(mail_info['From'], mail_info['To'].split(',') + mail_info['Cc'].split(','), msg.as_string())
83     smtp.quit()
84 
85 
86 if __name__ == '__main__':
87     thread1 = threading.Thread(target=logFind)
88     thread1.start()
89     thread1.join()
90     thread2 = threading.Thread(target=deWeight)
91     thread2.start()
92     thread2.join()
93     thread3 = threading.Thread(target=sendEmail)
94     thread3.start()
95     thread3.join()

 

posted @ 2022-01-19 17:51  知_行  阅读(70)  评论(0编辑  收藏  举报