automation - When a file is downloaded in a specific folder , It should be emailed to me - Stack Overflow

admin2025-04-16  0

So I am facing this problem where I have to download hundreds of files from Windchill, that are stored in a common drive folder on our server, so that everyone can access it. Then I have to copy and Paste that same file to email and send it off.

Is there any way to automate this process, as in whenever I download a file in that folder the file which is downloaded by me is emailed to me so that I could forward it easily.

Mind you this drive can be accessed by anyone and anyone could download files in that folder. I only want to get the files downloaded by me to be emailed to me.

I tried doing power automation , but that is only good to get a notification that the file has been downloaded but I want that file too.

So I am facing this problem where I have to download hundreds of files from Windchill, that are stored in a common drive folder on our server, so that everyone can access it. Then I have to copy and Paste that same file to email and send it off.

Is there any way to automate this process, as in whenever I download a file in that folder the file which is downloaded by me is emailed to me so that I could forward it easily.

Mind you this drive can be accessed by anyone and anyone could download files in that folder. I only want to get the files downloaded by me to be emailed to me.

I tried doing power automation , but that is only good to get a notification that the file has been downloaded but I want that file too.

Share asked Mar 11 at 7:24 Aryan Aryan 331 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If the server tracks the owner of each file (which most Windows and Linux systems do), we can check if the file was downloaded by you.

Here is a python script monitors the folder, checks the file’s owner, and if it matches your username, it emails the file to you.

import os
import time
import smtplib
import mimetypes
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from email.message import EmailMessage
import getpass
import pwd  # For Linux

Download_folder = r"/path/to/file-downloaded/folder"

Email_sender = "[email protected]"
Email_pass = "your_email_password"
Email_receive = "[email protected]"
SMTP_SERVER = "smtp.gmail"
SMTP_PORT = 587

class FileDownloadHandler(FileSystemEventHandler):
    def on_created(self, event):
        if event.is_directory:
            return

        file_path = event.src_path
        time.sleep(2)  # let the file download

        if is_file_downloaded_by_me(file_path):
            print(f"File downloaded by you: {file_path}")
            send_email(file_path)
        else:
            print(f"File ignored (not yours): {file_path}")

def is_file_downloaded_by_me(file_path):
    
    current_user = getpass.getuser()

    try:
        file_stat = os.stat(file_path)
        file_owner = pwd.getpwuid(file_stat.st_uid).pw_name

        return file_owner == current_user
    except Exception as e:
        print(f"Error checking file owner: {e}")
        return False

def send_email(file_path):
    #Send an email with the downloaded file as an attachment
    msg = EmailMessage()
    msg["From"] = Email_sender
    msg["To"] = Email_receive
    msg["Subject"] = f"New File Downloaded: {os.path.basename(file_path)}"
    msg.set_content(f"A new file was downloaded by you: {os.path.basename(file_path)}\nAttached is the file.")

   
    mime_type, _ = mimetypes.guess_type(file_path)
    mime_type = mime_type or "application/octet-stream"
    with open(file_path, "rb") as f:
        msg.add_attachment(f.read(), maintype=mime_type.split("/")[0], subtype=mime_type.split("/")[1], filename=os.path.basename(file_path))

    
    try:
        with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
            server.starttls()
            server.login(Email_sender, Email_pass)
            server.send_message(msg)
        print(f"Email sent successfully for {file_path}")
    except Exception as e:
        print(f"Failed to send email: {e}")

if __name__ == "__main__":
    event_handler = FileDownloadHandler()
    observer = Observer()
    observer.schedule(event_handler, Download_folder, recursive=False)

    print(f"Monitoring {Download_folder} for new downloads...")
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

Run this script in the background:

On Windows: Use Task Scheduler

On Linux: Use nohups

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744809467a268195.html

最新回复(0)