Professional Documents
Culture Documents
Python 1
Python 1
To automate sending daily email reports in Python, you can use the smtplib library for sending
emails and the schedule library for scheduling the task. Additionally, you may use email and MIME
libraries for constructing and formatting the email content.
Below is a simple script to get you started. Make sure to replace the placeholder values such as
your_email@gmail.com, your_password, recipient@example.com, and Report Content with your actual
email credentials and report details.
python
Copy code
import smtplib
import schedule
import time
def send_email():
# Email configuration
sender_email = 'your_email@gmail.com'
sender_password = 'your_password'
recipient_email = 'recipient@example.com'
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
# Email content
msg.attach(MIMEText(body, 'plain'))
server.starttls()
server.login(sender_email, sender_password)
schedule.every().day.at("08:00").do(send_email)
while True:
schedule.run_pending()
time.sleep(1)
Open a terminal and install the required libraries using the following command:
bash
Copy code
Update Script:
Replace the placeholder values in the script with your actual email credentials (sender_email,
sender_password, recipient_email) and customize the report content.
Save the script with a .py extension (e.g., email_report.py) and run it using the following command:
bash
Copy code
python email_report.py
The script will run indefinitely, checking for scheduled tasks and sending the email at the specified time.
Please note that storing email credentials directly in the script might not be the most secure approach.
Consider using environment variables or a configuration file for better security. Additionally, be aware of
the potential security risks associated with allowing less secure app access.