· 6 years ago · Aug 24, 2019, 12:54 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 += 1
38 if os.path.exists(file_location) and os.path.getsize(file_location) > 0:
39 for file in os.listdir( r"C:\Users\Borna\Desktop\pictures" ):
40 add_attachment(msg, os.path.join(r"C:\Users\Borna\Desktop\pictures", file))
41 print(file)
42
43 server = smtplib.SMTP('smtp.gmail.com', 587)
44 server.starttls()
45 server.login(email, password)
46 text = msg.as_string()
47 server.sendmail(email, send_to_email, text)
48 server.quit()
49 print('pictures sent')
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('content of file deleted')
61 time.sleep(10)
62 print('scanning again')
63 else:
64 print('scanning again empty')
65 time.sleep(10)