Skip to content

Commit 8588bb9

Browse files
authored
Add files via upload
1 parent c5bdf5a commit 8588bb9

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Email Scheduler/code.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import smtplib
2+
from email.mime.multipart import MIMEMultipart
3+
from email.mime.text import MIMEText
4+
from email.mime.base import MIMEBase
5+
from email import encoders
6+
import schedule
7+
import time
8+
from datetime import datetime
9+
10+
def send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, login, password, attachment_path=None):
11+
# Create a multipart message
12+
msg = MIMEMultipart()
13+
msg['From'] = from_email
14+
msg['To'] = ', '.join(to_emails)
15+
msg['Subject'] = subject
16+
17+
# Attach the body with the msg instance
18+
msg.attach(MIMEText(body, 'plain'))
19+
20+
# Attach any file if specified
21+
if attachment_path:
22+
attachment = open(attachment_path, "rb")
23+
part = MIMEBase('application', 'octet-stream')
24+
part.set_payload(attachment.read())
25+
encoders.encode_base64(part)
26+
part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")
27+
msg.attach(part)
28+
29+
# Create a secure SSL context and send the email
30+
with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
31+
server.login(login, password)
32+
text = msg.as_string()
33+
server.sendmail(from_email, to_emails, text)
34+
35+
def daily_report():
36+
# Define email parameters
37+
subject = "Daily Report - " + datetime.now().strftime("%Y-%m-%d")
38+
body = "Please find attached the daily report."
39+
to_emails = ["[email protected]"]
40+
from_email = "[email protected]"
41+
smtp_server = "smtp.example.com"
42+
smtp_port = 465
43+
44+
password = "your-email-password"
45+
attachment_path = "path/to/your/report.csv" # Change this to the path of your report
46+
47+
send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, login, password, attachment_path)
48+
49+
# Schedule the script to run daily at a specific time
50+
schedule.every().day.at("09:00").do(daily_report) # Change the time as needed
51+
52+
if __name__ == "__main__":
53+
while True:
54+
schedule.run_pending()
55+
time.sleep(1)

Email Scheduler/readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Step 1: Install Required Packages
2+
Make sure you have the necessary packages installed. If not, you can install them using pip:
3+
4+
pip install schedule
5+
6+
Step 2
7+
8+
Run code.py
9+
10+
Step 3: Set Up and Run the Script
11+
Define Email Parameters:
12+
13+
Update the to_emails, from_email, smtp_server, smtp_port, login, password, and attachment_path variables with your own email details and report file path.
14+
Schedule the Email:
15+
16+
Adjust the time in schedule.every().day.at("09:00").do(daily_report) to the desired time you want the email to be sent each day.
17+
Run the Script:
18+
19+
Save the script in a .py file (e.g., daily_email_report.py).
20+
Run the script using Python:
21+
22+
The script will now run continuously, checking every second if it needs to send the daily report. Make sure to keep the script running in an environment that is always on, like a server or a cloud instance.
23+
24+
Explanation
25+
smtplib: Used to connect to the SMTP server and send the email.
26+
MIME: Used to create the email message with attachments.
27+
schedule: A lightweight scheduling library to run the report-sending function at a specific time daily.
28+
while True loop**: Keeps the script running indefinitely, checking if it’s time to send the report.
29+
Feel free to customize the script further according to your specific requirements!

0 commit comments

Comments
 (0)