-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmails.py
150 lines (123 loc) · 5.64 KB
/
Emails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import shutil
import logging
import zipfile
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
# Configure logging
logging.basicConfig(
filename="file_organizer.log",
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s"
)
# Email configuration
SMTP_SERVER = 'smtp.gmail.com' # Example for Gmail; adjust as needed
SMTP_PORT = 587
EMAIL_ADDRESS = '[email protected]' # Your email address
EMAIL_PASSWORD = 'yqzy pesz ufzu inpx' # Your email password
RECIPIENT_EMAIL = '[email protected]' # Where to send alerts
# Function to send an email alert
def send_email(subject, events, completion_status, failures=None):
try:
msg = MIMEMultipart()
msg['From'] = EMAIL_ADDRESS
msg['To'] = RECIPIENT_EMAIL
msg['Subject'] = subject
# Build body content with events, completion status, and failure details
body_content = f"Status: {completion_status}\n\nMajor Events:\n"
for event in events:
body_content += f"- {event}\n"
if failures:
body_content += "\nFailures:\n"
for failure in failures:
body_content += f"- {failure}\n"
msg.attach(MIMEText(body_content, 'plain'))
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.send_message(msg)
logging.info("Email sent successfully.")
except Exception as e:
logging.error(f"Failed to send email: {str(e)}")
# Function to move a file into date and type-specific folders
def move_file_to_date_and_type_folder(src_path, date_dir, type_subfolder, events, failures):
try:
type_folder = os.path.join(date_dir, type_subfolder)
os.makedirs(type_folder, exist_ok=True)
filename = os.path.basename(src_path)
dest_path = os.path.join(type_folder, filename)
if os.path.exists(dest_path):
logging.warning(f"Duplicate file found and deleted: {src_path}")
os.remove(src_path)
events.append(f"Deleted duplicate file: {filename}")
return
shutil.move(src_path, dest_path)
logging.info(f"Moved file from {src_path} to {dest_path}")
events.append(f"Moved {filename} to {type_subfolder} folder")
except Exception as e:
logging.error(f"Error moving file {src_path}: {str(e)}")
failures.append(f"Error moving file {filename}: {str(e)}")
# Function to extract zip files and organize extracted files
def extract_and_organize_zip_file(zip_path, date_dir, events, failures):
extract_path = os.path.join(date_dir, "EXTRACTED")
os.makedirs(extract_path, exist_ok=True)
try:
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_path)
logging.info(f"Extracted {zip_path} to {extract_path}")
os.remove(zip_path) # Optionally delete the zip file after extraction
events.append(f"Extracted and organized contents of {zip_path}")
# Organize extracted files within the date folder
organize_files_in_directory(extract_path, date_dir, events, failures)
except zipfile.BadZipFile:
error_msg = f"Failed to extract {zip_path}: Bad zip file"
logging.error(error_msg)
failures.append(error_msg)
send_email("Extraction Error", events, "Error", [error_msg])
# Function to organize files into date and type folders
def organize_files_in_directory(directory, date_dir, events, failures):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path):
extension = filename.lower().strip()
if extension.endswith('.csv'):
move_file_to_date_and_type_folder(file_path, date_dir, 'CSV', events, failures)
elif extension.endswith('.dat'):
move_file_to_date_and_type_folder(file_path, date_dir, 'DAT', events, failures)
else:
move_file_to_date_and_type_folder(file_path, date_dir, 'OTHERS', events, failures)
# Directories
source_dir = 'downloads'
base_dir = 'organized_downloads'
os.makedirs(base_dir, exist_ok=True)
# Today's date folder
today_date = datetime.now().strftime('%Y-%m-%d')
date_dir = os.path.join(base_dir, today_date)
os.makedirs(date_dir, exist_ok=True)
# Main processing
events = []
failures = []
try:
events.append("Started processing files")
for filename in os.listdir(source_dir):
file_path = os.path.join(source_dir, filename)
if os.path.isfile(file_path):
if filename.lower().endswith('.zip'):
# Extract and organize zip files
extract_and_organize_zip_file(file_path, date_dir, events, failures)
elif filename.lower().endswith('.csv'):
move_file_to_date_and_type_folder(file_path, date_dir, 'CSV', events, failures)
elif filename.lower().endswith('.dat'):
move_file_to_date_and_type_folder(file_path, date_dir, 'DAT', events, failures)
else:
move_file_to_date_and_type_folder(file_path, date_dir, 'OTHERS', events, failures)
completion_status = "Completed successfully" if not failures else "Completed with errors"
events.append("File organization completed")
send_email("File Organization Complete", events, completion_status, failures)
except Exception as e:
error_msg = f"Unexpected error: {str(e)}"
logging.error(error_msg)
failures.append(error_msg)
send_email("File Organizer Error", events, "Error",[error_msg])