ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python发送文本附件

2022-01-19 18:03:11  阅读:174  来源: 互联网

标签:info python msg mime import 附件 mail 文本 com


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

 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()

 

标签:info,python,msg,mime,import,附件,mail,文本,com
来源: https://www.cnblogs.com/changbo/p/15823439.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有