Spaces:
Running
on
Zero
Running
on
Zero
Update notifier.py
Browse files- notifier.py +36 -0
notifier.py
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import smtplib
|
| 3 |
+
import requests
|
| 4 |
+
from email.mime.text import MIMEText
|
| 5 |
+
from email.mime.multipart import MIMEMultipart
|
| 6 |
+
|
| 7 |
+
EMAIL_USER = os.getenv("EMAIL_USER")
|
| 8 |
+
EMAIL_PASS = os.getenv("EMAIL_PASS")
|
| 9 |
+
|
| 10 |
+
def get_user_ip():
|
| 11 |
+
try:
|
| 12 |
+
return requests.get("https://api.ipify.org", timeout=5).text
|
| 13 |
+
except requests.RequestException:
|
| 14 |
+
return "Unavailable"
|
| 15 |
+
|
| 16 |
+
def send_email_notification(ip_address, user_message):
|
| 17 |
+
if not EMAIL_USER or not EMAIL_PASS:
|
| 18 |
+
print("[Notifier] Missing EMAIL_USER or EMAIL_PASS.")
|
| 19 |
+
return
|
| 20 |
+
|
| 21 |
+
subject = "RezAI Used"
|
| 22 |
+
body = f"A new user interacted with RezAi.\n\nIP Address: {ip_address}\nMessage: {user_message}"
|
| 23 |
+
|
| 24 |
+
msg = MIMEMultipart()
|
| 25 |
+
msg["From"] = EMAIL_USER
|
| 26 |
+
msg["To"] = EMAIL_USER
|
| 27 |
+
msg["Subject"] = subject
|
| 28 |
+
msg.attach(MIMEText(body, "plain"))
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
with smtplib.SMTP("smtp.gmail.com", 587) as server:
|
| 32 |
+
server.starttls()
|
| 33 |
+
server.login(EMAIL_USER, EMAIL_PASS)
|
| 34 |
+
server.sendmail(EMAIL_USER, EMAIL_USER, msg.as_string())
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"[Notifier] Email sending error: {e}")
|