Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from email import encoders
- from email.mime.base import MIMEBase
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- import itertools
- import os
- import shutil
- import smtplib
- import time
- INTERVAL = 10
- FOLDER = r'C:\Users\myname\Desktop\pictures'
- SOURCE_EMAIL = '@gmail.com'
- PASSWORD = 'p'
- DEST_EMAIL = '@gmail.com'
- SUBJECT = 'Test sa slanjem i brisanjem 4' # The subject line
- MESSAGE = 'sa attachmentom'
- def add_attachment(msg, file_location):
- part = MIMEBase('application', 'octet-stream')
- with open(file_location, 'rb') as attachment:
- part.set_payload(attachment.read())
- encoders.encode_base64(part)
- part.add_header('Content-Disposition', f'attachment; filename= {os.path.basename(file_location)}')
- msg.attach(part)
- def wait():
- print(f'scanning again in {INTERVAL} seconds')
- time.sleep(INTERVAL)
- def main():
- for count in itertools.count():
- if count >= 3:
- break
- try:
- contents = os.listdir(FOLDER)
- except OSError as e:
- print(f'error reading directory contents: {e}')
- wait()
- continue
- if not contents:
- print('folder was empty')
- wait()
- continue
- msg = MIMEMultipart()
- msg['From'] = SOURCE_EMAIL
- msg['To'] = DEST_EMAIL
- msg['Subject'] = SUBJECT
- msg.attach(MIMEText(MESSAGE, 'plain'))
- # attach files
- for file in contents:
- filepath = os.path.join(FOLDER, file)
- try:
- add_attachment(msg, filepath)
- except OSError as e:
- print(f"error with file '{file}': {e}")
- print(f'skipping file: {file}')
- continue
- print(f"attaching file '{file}'")
- os.remove(filepath)
- print(f"deleted file '{file}'")
- # send email
- server = smtplib.SMTP('smtp.gmail.com', 587)
- server.starttls()
- server.login(SOURCE_EMAIL, PASSWORD)
- text = msg.as_string()
- server.sendmail(SOURCE_EMAIL, DEST_EMAIL, text)
- server.quit()
- print('pictures sent')
- wait()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement