· 6 years ago · Aug 23, 2019, 10:28 PM
1import smtplib
2from email.mime.text import MIMEText
3from email.mime.multipart import MIMEMultipart
4from email.mime.base import MIMEBase
5from email import encoders
6import os.path
7import os, shutil
8import time
9
10count = 0
11email = '@gmail.com'
12password = 'p'
13send_to_email = '@gmail.com'
14subject = 'Test sa slanjem i brisanjem 4' # The subject line
15message = 'sa attachmentom'
16file_location = 'C:\\Users\\myname\\Desktop\\pictures'
17folder = r'C:\Users\myname\Desktop\pictures'
18
19msg = MIMEMultipart()
20msg['From'] = email
21msg['To'] = send_to_email
22msg['Subject'] = subject
23
24
25while (count < 3):
26 count = count + 1
27 with ("pictures.log", "r") as log:
28 if os.path.exists(file_location) and os.path.getsize(file_location) > 0 and not picture_file_name in log:
29 server = smtplib.SMTP('smtp.gmail.com', 587)
30 server.starttls()
31 server.login(email, password)
32 text = msg.as_string()
33 server.sendmail(email, send_to_email, text)
34 server.quit()
35 print('poslano')
36
37 def add_attachment(msg, file_location):
38 # Setup the attachment
39 filename = os.path.basename(file_location)
40 attachment = open(file_location, "rb")
41 part = MIMEBase('application', 'octet-stream')
42 part.set_payload(attachment.read())
43 encoders.encode_base64(part)
44 part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
45 attachment.close()
46 msg.attach(part)
47
48 for file in os.listdir( r"C:\Users\Borna\Desktop\pictures" ):
49 add_attachment(msg, os.path.join(r"C:\Users\Borna\Desktop\pictures", file))
50
51 for the_file in os.listdir(folder):
52 file_path = os.path.join(folder, the_file)
53 try:
54 if os.path.isfile(file_path):
55 os.unlink(file_path)
56 elif os.path.isdir(file_path):
57 shutil.rmtree(file_path)
58 except Exception as e:
59 print(e)
60 print('obrisano')
61 time.sleep(10)
62 print('ponovno skeniranje')
63
64 else:
65 print('empty')
66 time.sleep(10)