File size: 1,543 Bytes
a019ba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os, smtplib, hmac, hashlib, json, base64
from email.mime.text import MIMEText
from email.utils import formataddr
from modules.utils import log_event

def build_tracking_url(identifier: str, payload: dict) -> str:
    """
    /t/{token} にアクセス→Redirect
    payload 例: {"id":"lead-1","redirect":"https://example.com"}
    """
    secret = os.getenv("TRACKING_SECRET", "dev")
    data = json.dumps(payload, ensure_ascii=False)
    sig = hmac.new(secret.encode(), data.encode(), hashlib.sha256).digest()
    token = base64.urlsafe_b64encode(data.encode() + b"." + sig).decode()
    base = os.getenv("PUBLIC_BASE_URL", "http://localhost:7860")
    return f"{base}/t/{token}"

def send_email(to_email: str, subject: str, body_text: str):
    host = os.getenv("SMTP_HOST")
    port = int(os.getenv("SMTP_PORT", "587"))
    user = os.getenv("SMTP_USER")
    password = os.getenv("SMTP_PASSWORD")
    from_name = os.getenv("SMTP_FROM_NAME", "Agent Studio")
    from_email = os.getenv("SMTP_FROM_EMAIL", user)

    if not (host and user and password and from_email):
        raise RuntimeError("SMTP設定が不足しています。")

    msg = MIMEText(body_text, "plain", "utf-8")
    msg["Subject"] = subject
    msg["From"] = formataddr((from_name, from_email))
    msg["To"] = to_email

    with smtplib.SMTP(host, port) as server:
        server.starttls()
        server.login(user, password)
        server.sendmail(from_email, [to_email], msg.as_string())

    log_event("email_sent", {"to": to_email, "subject": subject})