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