0% found this document useful (0 votes)
15 views4 pages

Daily File Automation and Email Report

This document outlines a Python script for automating file operations and sending notification emails. It includes configuration settings for email and directories, functions for moving files, sending emails, and scheduling daily tasks. The script is designed to run daily at a specified time, logging the operations performed and any errors encountered.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Daily File Automation and Email Report

This document outlines a Python script for automating file operations and sending notification emails. It includes configuration settings for email and directories, functions for moving files, sending emails, and scheduling daily tasks. The script is designed to run daily at a specified time, logging the operations performed and any errors encountered.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import os

import shutil

import smtplib

from [Link] import MIMEText

from [Link] import MIMEMultipart

import schedule

import time

# --- Configuration ---

SENDER_EMAIL = "your_email@[Link]" # Replace with your email

SENDER_PASSWORD = "your_app_password" # Replace with your app


password (NOT your regular password)

RECEIVER_EMAIL = "recipient_email@[Link]" # Replace with


recipient email

SMTP_SERVER = "[Link]" # For Gmail, adjust for other providers

SMTP_PORT = 587 # For TLS, adjust for other providers

SOURCE_DIR = "C:\\Users\\YourUser\\Documents\\AutomationSource" #
Replace with your source directory

DEST_DIR = "C:\\Users\\YourUser\\Documents\\AutomationDestination" #
Replace with your destination directory

LOG_FILE = "automation_log.txt"

# --- Function 1: File Operations ---

def perform_file_operations():

print(f"[{[Link]()}] Starting file operations...")

if not [Link](DEST_DIR):

[Link](DEST_DIR)

print(f"[{[Link]()}] Created destination directory: {DEST_DIR}")


files_moved = 0

for filename in [Link](SOURCE_DIR):

source_path = [Link](SOURCE_DIR, filename)

dest_path = [Link](DEST_DIR, filename)

if [Link](source_path):

try:

[Link](source_path, dest_path)

print(f"[{[Link]()}] Moved: {filename} to {DEST_DIR}")

files_moved += 1

except Exception as e:

print(f"[{[Link]()}] Error moving {filename}: {e}")

with open(LOG_FILE, "a") as f:

[Link](f"[{[Link]()}] File operations completed. Moved


{files_moved} files.\n")

print(f"[{[Link]()}] File operations completed. Moved


{files_moved} files.")

# --- Function 2: Send Email ---

def send_notification_email(subject, body):

print(f"[{[Link]()}] Attempting to send email...")

msg = MIMEMultipart()

msg['From'] = SENDER_EMAIL

msg['To'] = RECEIVER_EMAIL

msg['Subject'] = subject

[Link](MIMEText(body, 'plain'))

try:

with [Link](SMTP_SERVER, SMTP_PORT) as server:


[Link]() # Secure the connection

[Link](SENDER_EMAIL, SENDER_PASSWORD)

server.send_message(msg)

print(f"[{[Link]()}] Email sent successfully to


{RECEIVER_EMAIL}")

with open(LOG_FILE, "a") as f:

[Link](f"[{[Link]()}] Email sent: '{subject}'\n")

except Exception as e:

print(f"[{[Link]()}] Failed to send email: {e}")

with open(LOG_FILE, "a") as f:

[Link](f"[{[Link]()}] Failed to send email: {e}\n")

# --- Function 3: Main Automation Task ---

def daily_automation_task():

print(f"\n[{[Link]()}] Running daily automation task...")

# 1. Perform file operations

perform_file_operations()

# 2. Send a summary email

subject = "Daily Automation Report"

body = f"""

Dear User,

This is your daily automation report.

File operations completed. Please check the '{DEST_DIR}' directory for


moved files.

Log file: {LOG_FILE}


Best regards,

Your Automation Script

"""

send_notification_email(subject, body)

print(f"[{[Link]()}] Daily automation task completed.")

# --- Scheduling Tasks ---

def setup_schedule():

print(f"[{[Link]()}] Setting up daily schedule...")

# Schedule the daily_automation_task to run every day at 10:00 AM

[Link]().[Link]("10:00").do(daily_automation_task)

# You can add other schedules here, e.g.:

# [Link](10).[Link](some_other_task)

# [Link]().[Link](yet_another_task)

print(f"[{[Link]()}] Schedule set. Waiting for tasks to run...")

# --- Main Program Execution ---

if __name__ == "__

You might also like