Bạn có thể dựng giao diện giống thế này, link tải lại đây
Đây
Tự Động Daily Email Chắc chắn rồi! Dưới đây là hướng dẫn từng bước để tạo một tập lệnh Python tự động gửi báo cáo email hàng ngày và cách thiết lập nó để chạy hàng ngày.
Bạn sẽ cần cài đặt thư viện smtplib để gửi email và email để tạo nội dung email.
Nếu bạn cần đọc dữ liệu từ một file CSV hoặc Excel, bạn có thể sử dụng thư viện như pandas.
pip install pandas
Dưới đây là một tập lệnh mẫu để gửi email:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import pandas as pd from datetime import date def send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, smtp_user, smtp_password): # Setup the MIME message = MIMEMultipart() message['From'] = from_email message['To'] = ", ".join(to_emails) message['Subject'] = subject # Add body to email message.attach(MIMEText(body, 'plain')) # Connect to the server server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # Login to the email account server.login(smtp_user, smtp_password) # Send the email server.sendmail(from_email, to_emails, message.as_string()) # Quit the server server.quit() def generate_report(): # Example: read data from a CSV file df = pd.read_csv('daily_report.csv') # Generate a summary or any report you want report = df.describe().to_string() return report def main(): # Define email parameters from_email = "[email protected]" to_emails = ["[email protected]", "[email protected]"] smtp_server = "smtp.example.com" smtp_port = 587 smtp_user = "[email protected]" smtp_password = "your_password" # Generate the report report = generate_report() subject = f"Daily Report - {date.today().strftime('%Y-%m-%d')}" body = f"Dear team,\n\nPlease find the daily report attached below:\n\n{report}\n\nBest regards,\nYour Name" # Send the email send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, smtp_user, smtp_password) if __name__ == "__main__": main()
Để tự động hóa việc chạy tập lệnh hàng ngày, bạn có thể sử dụng cron job.
crontab -e
0 8 * * * /usr/bin/python3 /path/to/your_script.py
Lưu ý thay thế /path/to/your_script.py bằng đường dẫn thực tế đến tập lệnh Python của bạn.
Để bảo mật thông tin đăng nhập email, bạn nên sử dụng các biến môi trường hoặc các phương pháp bảo mật khác để tránh lưu trực tiếp thông tin nhạy cảm trong mã nguồn.
Cải tiến form
Vì hiện tại tôi không thể gửi email thực tế hoặc hiển thị kết quả của một email đã được gửi, nhưng tôi có thể cung cấp cho bạn cách tạo ảnh chụp màn hình bằng Python sau khi gửi email.
Bạn có thể sử dụng thư viện pyautogui để tự động chụp màn hình và lưu nó vào file.
Trước tiên, bạn cần cài đặt thư viện pyautogui:
pip install pyautogui
Dưới đây là đoạn mã Python đã được cập nhật để bao gồm việc chụp màn hình sau khi gửi email:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import pandas as pd from datetime import date import pyautogui import time def send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, smtp_user, smtp_password): # Setup the MIME message = MIMEMultipart() message['From'] = from_email message['To'] = ", ".join(to_emails) message['Subject'] = subject # Add body to email message.attach(MIMEText(body, 'plain')) # Connect to the server server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # Login to the email account server.login(smtp_user, smtp_password) # Send the email server.sendmail(from_email, to_emails, message.as_string()) # Quit the server server.quit() def generate_report(): # Example: read data from a CSV file df = pd.read_csv('daily_report.csv') # Generate a summary or any report you want report = df.describe().to_string() return report def main(): # Define email parameters from_email = "[email protected]" to_emails = ["[email protected]", "[email protected]"] smtp_server = "smtp.example.com" smtp_port = 587 smtp_user = "[email protected]" smtp_password = "your_password" # Generate the report report = generate_report() subject = f"Daily Report - {date.today().strftime('%Y-%m-%d')}" body = f"Dear team,\n\nPlease find the daily report attached below:\n\n{report}\n\nBest regards,\nYour Name" # Send the email send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, smtp_user, smtp_password) # Wait a few seconds to ensure email is sent time.sleep(5) # Take a screenshot screenshot = pyautogui.screenshot() screenshot.save('email_sent_screenshot.png') if __name__ == "__main__": main()
Sau khi chạy tập lệnh này, một ảnh chụp màn hình sẽ được lưu với tên email_sent_screenshot.png trong thư mục hiện tại của bạn.
Hình ảnh này sẽ hiển thị màn hình tại thời điểm ngay sau khi email được gửi.
Để chạy tập lệnh này hàng ngày bằng cron job, bạn có thể làm theo các bước đã đề cập trước đó.
Sau khi thiết lập cron job, hãy đảm bảo rằng pyautogui có quyền chụp màn hình trên hệ thống của bạn (đặc biệt nếu bạn đang sử dụng hệ điều hành có tính năng bảo mật nghiêm ngặt như macOS).
Dưới đây là hướng dẫn từng bước để tạo ra một ảnh chụp màn hình quy trình gửi email tự động bằng Python.
Bạn có thể tự thực hiện các bước này để thấy kết quả.
Mỗi bước sẽ được mô tả chi tiết cùng với mã nguồn để bạn dễ dàng theo dõi.
Chạy lệnh sau để cài đặt các thư viện cần thiết:
pip install pandas pyautogui
Tạo một file Python (ví dụ: daily_email_report.py) với nội dung sau:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import pandas as pd from datetime import date import pyautogui import time def send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, smtp_user, smtp_password): # Setup the MIME message = MIMEMultipart() message['From'] = from_email message['To'] = ", ".join(to_emails) message['Subject'] = subject # Add body to email message.attach(MIMEText(body, 'plain')) # Connect to the server server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # Login to the email account server.login(smtp_user, smtp_password) # Send the email server.sendmail(from_email, to_emails, message.as_string()) # Quit the server server.quit() def generate_report(): # Example: read data from a CSV file df = pd.read_csv('daily_report.csv') # Generate a summary or any report you want report = df.describe().to_string() return report def main(): # Define email parameters from_email = "[email protected]" to_emails = ["[email protected]", "[email protected]"] smtp_server = "smtp.example.com" smtp_port = 587 smtp_user = "[email protected]" smtp_password = "your_password" # Generate the report report = generate_report() subject = f"Daily Report - {date.today().strftime('%Y-%m-%d')}" body = f"Dear team,\n\nPlease find the daily report attached below:\n\n{report}\n\nBest regards,\nYour Name" # Send the email send_email(subject, body, to_emails, from_email, smtp_server, smtp_port, smtp_user, smtp_password) # Wait a few seconds to ensure email is sent time.sleep(5) # Take a screenshot screenshot = pyautogui.screenshot() screenshot.save('email_sent_screenshot.png') if __name__ == "__main__": main()
Tạo một file CSV với tên daily_report.csv trong cùng thư mục với tập lệnh Python.
File CSV này có thể có nội dung mẫu như sau:
Column1,Column2,Column3 1,2,3 4,5,6 7,8,9
Chạy tập lệnh Python bằng cách sử dụng lệnh sau:
python daily_email_report.py
Sau khi tập lệnh chạy xong, bạn sẽ thấy một file ảnh chụp màn hình với tên email_sent_screenshot.png trong cùng thư mục.
Ảnh này sẽ hiển thị màn hình máy tính tại thời điểm tập lệnh hoàn tất việc gửi email.
Nếu bạn muốn tự động hóa việc chạy tập lệnh này hàng ngày, bạn có thể thiết lập một cron job trên hệ thống Linux như đã hướng dẫn ở phần trước.
crontab -e
0 8 * * * /usr/bin/python3 /path/to/your_script.py
Thay thế /path/to/your_script.py bằng đường dẫn thực tế đến tập lệnh Python của bạn.
Kết quả cuối cùng là một tập lệnh Python có khả năng tự động gửi email báo cáo hàng ngày và chụp ảnh màn hình khi hoàn tất.
Bằng cách này, bạn có thể theo dõi quy trình và đảm bảo rằng email đã được gửi đúng cách.
#Mtips5s #Contact
Fanpage: https://www.facebook.com/mtipscoder
Group trao đổi, chia sẻ: https://www.facebook.com/groups/mtipscoder
Website: https://mtips5s.com
Youtube: https://mtips5s.com
Twitter(X): @takagiks99
Instagram: @khuongkara
Threads: @khuongkara
Google Maps: @khuongkara
#Base Code #Souce Code
Bộ công cụ My Self: @tools.mtips5s.com
Github: @github
Npm: @npm
Docker: @docker
Chúc các bạn thành công!
Leave A Comment