· 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
25
26def add_attachment(msg, file_location):
27 # Setup the attachment
28 filename = os.path.basename(file_location)
29 attachment = open(file_location, "rb")
30 part = MIMEBase('application', 'octet-stream')
31 part.set_payload(attachment.read())
32 encoders.encode_base64(part)
33 part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
34 attachment.close()
35 msg.attach(part)
36
37while (count < 3):
38 count = count + 1
39 with ("pictures.log", "r") as log:
40 if os.path.exists(file_location) and os.path.getsize(file_location) > 0 and not picture_file_name in log:
41 server = smtplib.SMTP('smtp.gmail.com', 587)
42 server.starttls()
43 server.login(email, password)
44 text = msg.as_string()
45 server.sendmail(email, send_to_email, text)
46 server.quit()
47 print('poslano')
48
49 for file in os.listdir( r"C:\Users\Borna\Desktop\pictures" ):
50 add_attachment(msg, os.path.join(r"C:\Users\Borna\Desktop\pictures", file))
51
52 for the_file in os.listdir(folder):
53 file_path = os.path.join(folder, the_file)
54 try:
55 if os.path.isfile(file_path):
56 os.unlink(file_path)
57 elif os.path.isdir(file_path):
58 shutil.rmtree(file_path)
59 except Exception as e:
60 print(e)
61 print('obrisano')
62 time.sleep(10)
63 print('ponovno skeniranje')
64
65 else:
66 print('empty')
67 time.sleep(10)