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 73 74 75 76 77 78 |
#!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib import subprocess from pathlib import Path from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication def certificate(File,domain): cmd = 'cd %s && tar zcf %s.tar.gz %s%s'%(File,domain,File,domain) p = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) p.stdout.read().decode('gbk') if p.poll() == 0: my_file = Path('%s%s.tar.gz'%(File,domain)) if my_file.is_file(): return 0 else: return 1 def sendMail(strong): #content = 'hello, this is email content.' textApart = MIMEText(strong['content']) #imageFile = '1.png' #imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1]) #imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile) #pdfFile = '算法设计与分析基础第3版PDF.pdf' #pdfApart = MIMEApplication(open(pdfFile, 'rb').read()) #pdfApart.add_header('Content-Disposition', 'attachment', filename=pdfFile) zipFile = strong['zipFile'] #zipFile='222.tar.gz' zipApart = MIMEApplication(open(zipFile, 'rb').read()) zipApart.add_header('Content-Disposition', 'attachment', filename=zipFile) m = MIMEMultipart() m.attach(textApart) #m.attach(imageApart) #m.attach(pdfApart) m.attach(zipApart) m['Subject'] = strong['title'] m['from']= '发件人:%s'%(strong['fromaddr']) try: #server = smtplib.SMTP(strong['smtp']) server = smtplib.SMTP_SSL(strong['smtp'],465) server.login(strong['fromaddr'], strong['password']) server.sendmail(strong['fromaddr'], strong['toaddrs'], m.as_string()) print('success') server.quit() except smtplib.SMTPException as e: print('error:', e) #打印错误 if __name__ == '__main__': domain='local.aniplay.co' File='/etc/letsencrypt/live/' strong={ 'fromaddr':'xxxxxxxxx@126.com', 'password': 'xxxxxxx', 'toaddrs': ['xxxxxxx@qq.com','xxxxxxx@qq.com'], 'zipFile': '%s%s.tar.gz'%(File,domain), 'smtp': 'smtp.126.com', 'title': 'Let’s Encrypt - %s'%(domain), 'content': '%s 最新证书已下发,请查看下载附件!'%(domain) } res=certificate(File,domain) print(res) if res == 0: request=sendMail(strong) if request==0: cmd = 'mv %s%s.tar.gz /home && rm -rf /home/%s.tar.gz' % (File,domain,domain) p = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) p.stdout.read().decode('gbk') if p.poll() == 0: print('已删除') |
- 本文固定链接: https://www.yoyoask.com/?p=4879
- 转载请注明: shooter 于 SHOOTER 发表