-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily_email_report.py
More file actions
86 lines (75 loc) · 3.09 KB
/
daily_email_report.py
File metadata and controls
86 lines (75 loc) · 3.09 KB
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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import schedule
import time
import os
import logging
# Setup logging
logging.basicConfig(filename='email_scheduler.log', level=logging.INFO,
format='%(asctime)s %(levelname)s:%(message)s')
# Function to send the email with an attachment
def send_email_with_attachment(subject, body, to_email, file_path):
# Email settings
smtp_server = 'smtp.gmail.com'
smtp_port = 587
from_email = 'kibejay61@gmail.com'
from_password = 'wnpn tihx zgbx etiv' # Use app password
# Create the message
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
# Attach the email body
msg.attach(MIMEText(body, 'plain'))
# Check if the file exists before attaching
if os.path.isfile(file_path):
try:
# Attach the file
with open(file_path, "rb") as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename= {os.path.basename(file_path)}')
msg.attach(part)
except Exception as e:
logging.error(f"Error attaching file: {file_path}, error: {e}")
print(f"Error attaching file: {e}")
return
else:
logging.error(f"File not found: {file_path}")
print(f"File not found: {file_path}")
return
try:
# Setup the SMTP server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls() # Use TLS encryption
server.login(from_email, from_password) # Log into the email account
# Send the email
server.sendmail(from_email, to_email, msg.as_string())
logging.info(f'Email with attachment sent to {to_email}')
print(f'Email with attachment sent to {to_email}')
# Close the server connection
server.quit()
except Exception as e:
logging.error(f"Error sending email: {e}")
print(f"Error sending email: {e}")
# Define the daily report content and schedule
def send_daily_report():
subject = "Daily Report with Attachment"
body = "Hello James, find the report below. This is your daily report.\n\n- Summary of tasks\n- Analytics overview\n- Progress report"
to_email = "techspaceerror404@gmail.com"
file_path = "C:\\Users\\lenovo\\PythonAutomation\\Reports\\jay.pdf"
# Replace with the actual path to your file
send_email_with_attachment(subject, body, to_email, file_path)
# Schedule the email to be sent every day at a specific time (e.g., 11:14 AM)
schedule.every().day.at("23:10").do(send_daily_report)
# Run the scheduler in an infinite loop
if __name__ == "__main__":
logging.info("Daily report email scheduler started.")
print("Daily report email scheduler started.")
while True:
schedule.run_pending()
time.sleep(60) # Check every minute