wtorek, 5 listopada

Certainly! To automate sending daily email reports in Python, you can use the `smtplib` library to send emails and the `schedule` library to schedule

python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import schedule
import time
def send_email(subject, body, to_email, smtp_server, smtp_port, sender_email, sender_password):
Set up the MIME
message = MIMEMultipart()
message[’From’] = sender_email
message[’To’] = to_email
message[’Subject’] = subject
Attach the body to the email
message.attach(MIMEText(body, 'plain’))
Connect to the SMTP server and send the email
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, to_email, message.as_string())
def daily_report():
Your report generation logic here
Replace the following lines with your actual report content
report_subject = „Daily Report”
report_body = „This is your daily report content.”
Replace with your email configuration
to_email = „[email protected]
smtp_server = „smtp.gmail.com”
smtp_port = 587
sender_email = „[email protected]
sender_password = „your_password”
Send the email
send_email(report_subject, report_body, to_email, smtp_server, smtp_port, sender_email, sender_password)
Schedule the daily report to be sent at a specific time
schedule.every().day.at(„08:00”).do(daily_report)
Run the scheduler in an infinite loop
while True:
schedule.run_pending()
time.sleep(1)
Now, let’s walk through the setup:
1. Install required libraries:
bash
pip install smtplib schedule
2. Update script with your details:
– Replace the placeholder content in the `daily_report` function with your actual report generation logic.
– Update the email configuration in the `daily_report` function with your email details.
3. Less secure apps access:
If you are using Gmail, you may need to enable „Less secure app access” in your Google account settings. Be cautious with this option, as it may reduce the security of your account.
4. Run the script:
Save the script to a file, for example, `daily_email_report.py`, and run it:
bash
python daily_email_report.py
The script will run indefinitely, checking for scheduled tasks every second. Adjust the schedule as needed.

Zobacz również   Zapobieganie problemom z nadgarstkami w pracy: ergonomiczne rozwiązania dla biurka i nie tylko

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

*

Ads Blocker Image Powered by Code Help Pro

Wykryto blokadę reklam!!!

Wykryliśmy, że używasz rozszerzeń do blokowania reklam. Prosimy, wesprzyj nas, wyłączając blokadę reklam.