Here’s a Python script that automates sending daily email reports using smtplib, email, and schedule. You’ll need to provide your email credentials and set up access to a mail server (like Gmail or Outlook).
Security Note: Avoid hardcoding credentials in production scripts. Use environment variables or secure secrets management instead.
import smtplib
import ssl
from email.message import EmailMessage
import schedule
import time
from datetime import datetime
# Email configuration
EMAIL_ADDRESS = "your_email@example.com"
EMAIL_PASSWORD = "your_app_password" # Use an app-specific password for Gmail
# Function to generate the report content
def generate_report():
# Placeholder: generate actual report content dynamically
report = f"Daily Report for {datetime.now().strftime('%Y-%m-%d')}\n\nEverything is running smoothly."
return report
# Function to send email
def send_email():
subject = "Daily Report"
body = generate_report()
msg = EmailMessage()
msg.set_content(body)
msg["Subject"] = subject
msg["From"] = EMAIL_ADDRESS
msg["To"] = "recipient@example.com" # Can be a list of recipients
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.send_message(msg)
print("Email sent successfully.")
except Exception as e:
print(f"Failed to send email: {e}")
# Schedule the task to run daily at 8:00 AM
schedule.every().day.at("08:00").do(send_email)
print("Scheduler started. Waiting to send daily report...")
while True:
schedule.run_pending()
time.sleep(60)
To Run:
- Install dependencies:
pip install schedule - Replace placeholders with your email and credentials.
- Run with:
python daily_email_report.py
