You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from email_alert import EmailAlert
email_alert = EmailAlert(
alert_email="recipient@example.com",
smtp_user="your_email@example.com",
smtp_password="your_password"
)
email_alert.send_email_alert("Test Alert", "This is a test message.")
Sử dụng phương thức send_email_alert để gửi thông báo:
Ví dụ:
Thay đổi mã nguồn đoạn: def __init__(self, config_path="email_config.ini")
Demo project cần gửi ALert từ database:
import sqlite3
from email_alert import EmailAlert
def check_and_send_alerts(db_path="alerts.db", email_config="email_config.ini"):
"""Kiểm tra cơ sở dữ liệu và gửi email nếu phát hiện cảnh báo mới."""
email_alert = EmailAlert(config_path=email_config)
if not os.path.exists(db_path):
logging.error(f"Không tìm thấy cơ sở dữ liệu tại {db_path}.")
return
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
cursor.execute("SELECT message FROM alerts WHERE status = 'new'")
alerts = cursor.fetchall()
if alerts:
message = "New Alerts:\n" + "\n".join([alert[0] for alert in alerts])
email_alert.send_email_alert("Security Alerts", message)
logging.info("Cảnh báo đã được gửi thành công.")
# Đánh dấu cảnh báo đã gửi
cursor.execute("UPDATE alerts SET status = 'sent' WHERE status = 'new'")
conn.commit()
else:
logging.info("Không có cảnh báo mới để gửi.")
except Exception as e:
logging.error(f"Lỗi khi xử lý cảnh báo: {e}")
finally:
conn.close()
About
Using app password of Gmail to send alert (or anything) to your inbox