The script will start running and will send the daily email report at the specified time. You can modify the script further to include dynamic data in the email report if needed.
Copy code import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import schedule import time smtplib: This library allows us to send emails using the Simple Mail Transfer Protocol (SMTP). MIMEMultipart and MIMEText: These are classes from the email.mime module that help us construct the email message. schedule: This library is used for scheduling tasks at specific times. time: This is a built-in Python library used for time-related operations. Defining the send_email Function:
def send_email(): # Email configuration sender_email = 'your_email@gmail.com' # Your email address receiver_email = 'recipient_email@gmail.com' # Recipient's email address password = 'your_password' # Your email password
# Create message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = 'Daily Report'
# Email body
body = "This is your daily report email."
msg.attach(MIMEText(body, 'plain'))
# Send email
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
text = msg.as_string()
server.sendmail(sender_email, receiver_email, text)
server.quit()
print("Email sent successfully!")
except Exception as e:
print(f"Error: {e}")
It sets up the email configuration including sender, receiver, password, and message content. It then uses the smtplib library to create an SMTP connection, login to the email server, construct the email message, and send the email. If any error occurs during the email sending process, it catches the exception and prints an error message. Scheduling the Email Sending:
schedule.every().day.at("08:00").do(send_email) This line schedules the send_email function to run daily at 8:00 AM. You can adjust the time by modifying the "08:00" string to your desired time format (24-hour format). Running the Schedule:
while True: schedule.run_pending() time.sleep(1) This infinite loop continuously checks if there are any scheduled tasks to run using schedule.run_pending(). time.sleep(1) ensures that the script doesn't consume too much CPU by pausing for 1 second in each iteration. To set up and run the script:
Replace 'your_email@gmail.com', 'recipient_email@gmail.com', and 'your_password' with your actual email credentials. Customize the email content in the body variable to include your daily report information. Adjust the scheduled time in schedule.every().day.at("08:00").do(send_email) according to your preferred sending time. Save the script and run it using python daily_report.py in your terminal or command prompt. The script will then run continuously, checking for scheduled tasks, and send the daily email report at the specified time.