To automate sending daily email reports in Python, you can use libraries
like smtplib
and email
for sending emails, and schedule
for scheduling the script to
run daily. Here's a step-by-step guide to set it up:
Step 1: Prepare Your Report Data Before you can send daily
email reports, you need to generate the report data you want to send. This
could be data from a database, CSV files, or any other data source. For this
example, let's assume you have a function generate_report()
that generates your daily report.
Step 2: Set Up Your Email
Configuration You'll need to provide your email credentials (SMTP
server, username, and password) and configure the email message. You can use a
Gmail account for sending emails. Replace 'youremail@gmail.com'
and 'yourpassword'
with your
actual email credentials.
Copy paste
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Email configuration
smtp_server = 'smtp.gmail.com'
smtp_port = 587
sender_email = 'youremail@gmail.com'
sender_password = 'yourpassword'
recipient_email = 'recipient@example.com'
Step 3: Create the Email Message Function Create a function that generates the email message with the report attached as a text file.
Copy paste
def create_email_message(subject, message, from_email, to_email, attachment_path):
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
# Attach the message
msg.attach(MIMEText(message, 'plain'))
# Attach the report file
with open(attachment_path, 'rb') as file:
attachment = MIMEText(file.read())
msg.add_header('Content-Disposition', f'attachment; filename="{attachment_path}"')
msg.attach(attachment)
return msg
Step 4: Create the Send Email
Function Create a function that sends the email using the smtplib
library.
Copy paste
def send_email(subject, message, from_email, to_email, attachment_path):
msg = create_email_message(subject, message, from_email, to_email, attachment_path)
try:
# Connect to the SMTP server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(from_email, sender_password)
# Send the email
server.sendmail(from_email, to_email, msg.as_string())
# Close the SMTP server connection
server.quit()
print('Email sent successfully')
except Exception as e:
print(f'Email sending failed: {e}')
Step 5: Schedule the Email Sending
To send the email report daily, you can use the schedule library to schedule the script to run at a specific time.
Install the schedule library if you haven't already:
pip install schedule
Now, create a script that generates the report, sends the email, and schedules it to run daily:
Copy paste
import schedule
import time
def main():
report_data = generate_report() # Generate your report data
subject = 'Daily Report'
message = 'Here is your daily report.'
attachment_path = 'report.txt'
# Save the report data to a file (e.g., report.txt)
with open(attachment_path, 'w') as file:
file.write(report_data)
send_email(subject, message, sender_email, recipient_email, attachment_path)
if __name__ == '__main__':
schedule.every().day.at('08:00').do(main) # Adjust the time as needed
while True:
schedule.run_pending()
time.sleep(1)
This script will run the main
function every day at 8:00 AM. Adjust the time according to your preferred
schedule.
Step 6: Run the Script Save the script, and run it. It will send your daily report automatically at the scheduled time.
Make sure to enable "Less secure apps" in your Gmail settings or use an App Password if you encounter authentication issues with Gmail.
No comments:
Post a Comment