Corin1998 commited on
Commit
a019ba8
·
verified ·
1 Parent(s): 4fc9f3a

Create emailer.py

Browse files
Files changed (1) hide show
  1. modules/emailer.py +39 -0
modules/emailer.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, smtplib, hmac, hashlib, json, base64
2
+ from email.mime.text import MIMEText
3
+ from email.utils import formataddr
4
+ from modules.utils import log_event
5
+
6
+ def build_tracking_url(identifier: str, payload: dict) -> str:
7
+ """
8
+ /t/{token} にアクセス→Redirect
9
+ payload 例: {"id":"lead-1","redirect":"https://example.com"}
10
+ """
11
+ secret = os.getenv("TRACKING_SECRET", "dev")
12
+ data = json.dumps(payload, ensure_ascii=False)
13
+ sig = hmac.new(secret.encode(), data.encode(), hashlib.sha256).digest()
14
+ token = base64.urlsafe_b64encode(data.encode() + b"." + sig).decode()
15
+ base = os.getenv("PUBLIC_BASE_URL", "http://localhost:7860")
16
+ return f"{base}/t/{token}"
17
+
18
+ def send_email(to_email: str, subject: str, body_text: str):
19
+ host = os.getenv("SMTP_HOST")
20
+ port = int(os.getenv("SMTP_PORT", "587"))
21
+ user = os.getenv("SMTP_USER")
22
+ password = os.getenv("SMTP_PASSWORD")
23
+ from_name = os.getenv("SMTP_FROM_NAME", "Agent Studio")
24
+ from_email = os.getenv("SMTP_FROM_EMAIL", user)
25
+
26
+ if not (host and user and password and from_email):
27
+ raise RuntimeError("SMTP設定が不足しています。")
28
+
29
+ msg = MIMEText(body_text, "plain", "utf-8")
30
+ msg["Subject"] = subject
31
+ msg["From"] = formataddr((from_name, from_email))
32
+ msg["To"] = to_email
33
+
34
+ with smtplib.SMTP(host, port) as server:
35
+ server.starttls()
36
+ server.login(user, password)
37
+ server.sendmail(from_email, [to_email], msg.as_string())
38
+
39
+ log_event("email_sent", {"to": to_email, "subject": subject})