satdetect-dev / app /notifier.py
coderuday21's picture
Cursor
Deploy satdetect-dev with Priyanka audit/exception logs.
d70361b
Raw History Blame Contribute Delete
14.1 kB
"""
Email notification module.
Sends HTML-formatted detection reports via the manager's email API (HTTPS)
or SMTP (e.g. Gmail) when API URL is not set.
Credentials and API URL from environment variables.
"""
import base64
import logging
import os
import re
import smtplib
import ssl
from datetime import datetime, timezone
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from pathlib import Path
logger = logging.getLogger(__name__)
EMAIL_RE = re.compile(r"^[^\s@]+@[^\s@]+\.[^\s@]+$")
TEMPLATE_PATH = Path(__file__).resolve().parent.parent / "templates" / "ChangeDetection.html"
# Manager's email API (multipart/form-data). When set, used instead of SMTP.
EMAIL_API_URL = os.environ.get(
"EMAIL_API_URL",
"https://emailservice.managemybusinessess.com/api/email/send",
)
def _smtp_settings():
return {
"host": os.environ.get("SMTP_HOST", "smtp.gmail.com"),
"port": int(os.environ.get("SMTP_PORT", "587")),
"user": os.environ.get("SMTP_USER", "vedangofficeserver@gmail.com"),
"password": os.environ.get("SMTP_PASS", ""),
}
def _valid_email(email: str) -> bool:
return bool(email and EMAIL_RE.match(email.strip()))
def _load_template() -> str:
"""Read the HTML email template from disk."""
if not TEMPLATE_PATH.exists():
logger.error("Email template not found at %s", TEMPLATE_PATH)
return "<p>Change Detection report — template file missing.</p>"
return TEMPLATE_PATH.read_text(encoding="utf-8")
def _build_region_rows(regions: list) -> str:
"""Render region rows as HTML <tr> elements for the email template."""
if not regions:
return ""
rows = []
for r in regions[:20]:
bg = "#f9f9fb" if r.get("id", 0) % 2 == 0 else "#ffffff"
sub = r.get("subType") or "—"
conf = f'{r.get("confidence", 0) * 100:.0f}%'
area = f'{r.get("area", 0):,}'
rows.append(
f'<tr style="background:{bg};">'
f'<td style="padding:6px 10px;">{r.get("id","")}</td>'
f'<td style="padding:6px 10px;">{r.get("objectType","")}</td>'
f'<td style="padding:6px 10px;">{sub}</td>'
f'<td style="padding:6px 10px;">{conf}</td>'
f'<td style="padding:6px 10px; text-align:right;">{area}</td>'
f"</tr>"
)
return "\n".join(rows)
def _report_link_section(report_url: str) -> str:
if not report_url:
return ""
safe = report_url.replace('"', "%22")
return (
'<p style="margin:0 0 20px; text-align:center;">'
f'<a href="{safe}" style="display:inline-block; background:#2e33c5; color:#fff; '
'text-decoration:none; padding:12px 24px; border-radius:8px; font-size:14px; font-weight:600;">'
"View full report online</a></p>"
)
def build_email_body(
title: str,
method: str,
zone: str,
village: str,
change_pct: float,
changed_px: int,
total_px: int,
regions: list,
report_url: str = "",
) -> str:
"""Populate the HTML template with detection results."""
html = _load_template()
location = ", ".join(filter(None, [village, zone])) or "—"
region_rows = _build_region_rows(regions)
now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
replacements = {
"{{title}}": title or "Untitled run",
"{{method}}": method or "—",
"{{location}}": location,
"{{change_pct}}": f"{change_pct:.2f}",
"{{changed_px}}": f"{changed_px:,}",
"{{total_px}}": f"{total_px:,}",
"{{regions_count}}": str(len(regions)),
"{{region_rows}}": region_rows,
"{{timestamp}}": now,
"{{report_link_section}}": _report_link_section(report_url),
}
for key, val in replacements.items():
html = html.replace(key, val)
if regions:
html = html.replace("{{#regions}}", "").replace("{{/regions}}", "")
else:
html = re.sub(r"\{\{#regions\}\}.*?\{\{/regions\}\}", "", html, flags=re.DOTALL)
return html
def _send_via_api(recipient: str, subject: str, html_body: str, *, attach_html: bool = True):
"""Send email via manager's API (POST multipart/form-data).
The vendor API validates that ``FileName``/``AttachmentBase64`` are always
present, even when the caller only wants the HTML body to render inline —
so we always include them (using the HTML body itself as the attachment).
"""
if not EMAIL_API_URL or not EMAIL_API_URL.strip():
return False, "EMAIL_API_URL is not set."
url = EMAIL_API_URL.strip()
try:
import requests
attachment_source = html_body if attach_html else "<html><body></body></html>"
attachment_b64 = base64.b64encode(attachment_source.encode("utf-8")).decode("ascii")
files = {
"ToEmail": (None, recipient),
"Subject": (None, subject),
"Body": (None, html_body),
"FileName": (None, "ChangeDetection.html" if attach_html else "signin.html"),
"AttachmentBase64": (None, attachment_b64),
}
resp = requests.post(
url,
files=files,
timeout=30,
headers={"Accept": "*/*"},
)
if resp.status_code >= 200 and resp.status_code < 300:
logger.info("Email API: sent to %s", recipient)
return True, None
msg = f"API returned {resp.status_code}"
try:
body = resp.text or resp.reason
if body:
msg = f"{msg}: {body[:200]}"
except Exception:
pass
logger.warning("Email API failed: %s", msg)
return False, msg
except Exception as exc:
logger.exception("Email API request failed: %s", exc)
return False, f"{type(exc).__name__}: {exc}"
def _send_via_smtp(recipient: str, subject: str, html_body: str):
"""Send email via SMTP (e.g. Gmail)."""
settings = _smtp_settings()
smtp_user = settings["user"]
smtp_pass = settings["password"]
smtp_host = settings["host"]
smtp_port = settings["port"]
if not smtp_user or not smtp_pass:
return False, "SMTP credentials are not configured on the server."
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
msg["From"] = smtp_user
msg["To"] = recipient
msg.attach(MIMEText(html_body, "html", "utf-8"))
try:
context = ssl.create_default_context()
with smtplib.SMTP(smtp_host, smtp_port, timeout=20) as server:
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(smtp_user, smtp_pass)
server.sendmail(smtp_user, recipient, msg.as_string())
logger.info("SMTP email sent to %s", recipient)
return True, None
except smtplib.SMTPAuthenticationError:
logger.exception("SMTP authentication failed")
return False, "SMTP authentication failed. Check the Gmail app password."
except Exception as exc:
logger.error(
"Failed to send notification email to %s: %s: %s",
recipient,
type(exc).__name__,
exc,
)
return False, f"{type(exc).__name__}: {exc}"
def _send_html_email(recipient: str, subject: str, html_body: str, *, attach_html: bool = True):
"""Send email: use manager's API if EMAIL_API_URL is set, else SMTP."""
if not _valid_email(recipient):
return False, "Enter a valid recipient email address."
if EMAIL_API_URL and EMAIL_API_URL.strip():
return _send_via_api(recipient, subject, html_body, attach_html=attach_html)
return _send_via_smtp(recipient, subject, html_body)
def send_notification(
recipient: str,
title: str,
method: str,
zone: str,
village: str,
change_pct: float,
changed_px: int,
total_px: int,
regions: list,
report_url: str = "",
):
"""Send a detection report email and return (success, error_message)."""
html_body = build_email_body(
title, method, zone, village, change_pct, changed_px, total_px, regions,
report_url=report_url,
)
subject = f"Change Detection Report — {title or 'Untitled run'}"
return _send_html_email(recipient, subject, html_body)
def send_password_reset_email(recipient: str, reset_link: str, expire_minutes: int = 30):
"""Send the Forgot Password reset link. Returns (success, error_message)
same as send_notification — callers should log a failure, not surface it
to the requester (would leak whether the account exists)."""
html_body = f"""
<html>
<body style="font-family: Arial, sans-serif; color: #222;">
<h2>Reset your password</h2>
<p>We received a request to reset the password for your Vedangsoft Change Detection account.</p>
<p><a href="{reset_link}" style="display:inline-block;padding:10px 20px;background:#2e33c5;color:#fff;text-decoration:none;border-radius:6px;">Reset password</a></p>
<p>Or copy this link into your browser:<br><span style="word-break:break-all;">{reset_link}</span></p>
<p>This link expires in {expire_minutes} minutes. If you didn't request this, you can safely ignore this email.</p>
</body>
</html>
"""
return _send_html_email(recipient, "Reset your Vedangsoft Change Detection password", html_body)
def send_login_otp_email(recipient: str, code: str, expire_minutes: int = 10):
html_body = f"""
<html>
<body style="font-family: Arial, sans-serif; color: #222;">
<h2>Your sign-in code</h2>
<p>Use this one-time password to finish signing in to Vedangsoft Change Detection:</p>
<p style="font-size:28px; letter-spacing:8px; font-weight:700;">{code}</p>
<p>This code expires in {expire_minutes} minutes. If you did not try to sign in, you can ignore this email.</p>
</body>
</html>
"""
return _send_html_email(
recipient,
f"{code} is your Vedangsoft sign-in code",
html_body,
attach_html=False,
)
def _sms_api_url() -> str:
explicit = os.environ.get("SMS_API_URL", "").strip()
if explicit:
return explicit
email_url = (EMAIL_API_URL or "").strip()
if "/api/email/send" in email_url:
return email_url.replace("/api/email/send", "/api/sms/send")
return ""
def _send_via_sms_api(phone_e164: str, message: str):
url = _sms_api_url()
if not url:
return False, "SMS_API_URL is not set."
digits = re.sub(r"\D", "", phone_e164 or "")
if not digits:
return False, "Missing mobile number."
try:
import requests
files = {
"ToMobile": (None, digits),
"Mobile": (None, digits),
"Message": (None, message),
"Body": (None, message),
}
resp = requests.post(
url,
files=files,
timeout=30,
headers={"Accept": "*/*"},
)
if 200 <= resp.status_code < 300:
logger.info("SMS API: sent to %s", "*" * max(0, len(digits) - 4) + digits[-4:])
return True, None
msg = f"API returned {resp.status_code}"
try:
body = resp.text or resp.reason
if body:
msg = f"{msg}: {body[:200]}"
except Exception:
pass
logger.warning("SMS API failed: %s", msg)
return False, msg
except Exception as exc:
logger.exception("SMS API request failed: %s", exc)
return False, f"{type(exc).__name__}: {exc}"
def _send_via_fast2sms(phone_e164: str, code: str):
key = os.environ.get("SMS_API_KEY", "").strip()
if not key:
return False, "SMS_API_KEY is not set."
digits = re.sub(r"\D", "", phone_e164 or "")
if digits.startswith("91") and len(digits) == 12:
national = digits[2:]
else:
national = digits
if len(national) != 10:
return False, "Fast2SMS only supports 10-digit Indian numbers."
try:
import requests
resp = requests.get(
"https://www.fast2sms.com/dev/bulkV2",
params={
"authorization": key,
"route": "otp",
"variables_values": code,
"numbers": national,
"flash": "0",
},
timeout=30,
)
if 200 <= resp.status_code < 300:
logger.info("Fast2SMS: sent OTP to ****%s", national[-4:])
return True, None
return False, f"Fast2SMS returned {resp.status_code}: {(resp.text or '')[:200]}"
except Exception as exc:
logger.exception("Fast2SMS request failed: %s", exc)
return False, f"{type(exc).__name__}: {exc}"
def send_login_otp_sms(phone_e164: str, code: str, expire_minutes: int = 10):
"""Send a 6-digit login OTP. `phone_e164` is expected as '+CCNNNNNNNNNN'."""
if not phone_e164 or not phone_e164.startswith("+"):
return False, "Enter a valid mobile number."
message = (
f"{code} is your Vedangsoft Change Detection sign-in OTP. "
f"Valid for {expire_minutes} minutes. Do not share this code."
)
provider = os.environ.get("SMS_PROVIDER", "").strip().lower()
if provider == "fast2sms":
return _send_via_fast2sms(phone_e164, code)
return _send_via_sms_api(phone_e164, message)
def send_test_email(recipient: str):
"""Send a small test email to verify delivery."""
now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
html_body = f"""
<html>
<body style="font-family: Arial, sans-serif; color: #222;">
<h2>AI Change Detection Email Test</h2>
<p>This is a test email from the AI Change Detection application.</p>
<p>If you received this, the email configuration is working.</p>
<p><strong>Timestamp:</strong> {now}</p>
</body>
</html>
"""
return _send_html_email(recipient, "AI Change Detection — Test Email", html_body)