· 6 years ago · Aug 23, 2019, 10:50 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 = 'INT'
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\Borna\Desktop\pictures'
18
19msg = MIMEMultipart()
20msg['From'] = email
21msg['To'] = send_to_email
22msg['Subject'] = subject
23
24msg.attach(MIMEText(message, 'plain'))
25def add_attachment( msg, file_location ):
26 filename = os.path.basename(file_location)
27 attachment = open(file_location, "rb")
28 part = MIMEBase('application', 'octet-stream')
29 part.set_payload(attachment.read())
30 encoders.encode_base64(part)
31 part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
32 attachment.close()
33 msg.attach(part)
34
35while (count < 3):
36 count += 1
37 if os.path.exists(file_location) and os.path.getsize(file_location) > 0:
38 if os.path.exists(file_location) and os.path.getsize(file_location) > 0:
39 with ("pictures.log", "r") as log:
40 if 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('pictures sent')
48
49 for the_file in os.listdir(folder):
50 file_path = os.path.join(folder, the_file)
51 try:
52 if os.path.isfile(file_path):
53 os.unlink(file_path)
54 elif os.path.isdir(file_path):
55 shutil.rmtree(file_path)
56 except Exception as e:
57 print(e)
58 print('content of file deleted')
59 time.sleep(10)
60 print('scanning again')
61 else:
62 print('scanning again empty')
63 time.sleep(10)