File size: 4,393 Bytes
d712cef | 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | """
send_and_move_email_cli.py
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Atomic CLI: SEND via Gmail first, then MOVE to EmailsSent DB.
If sending fails for any reason, nothing is moved.
Input (stdin JSON): { "id": <int> }
Output (stdout JSON): { "ok": true/false, "message": "...", "error": "..." }
"""
import json
import sys
import os
import sqlite3
# ββ Paths ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
REVIEW_DB = os.path.join(os.environ.get('WORKSPACE_ROOT', '.'), 'Database/EmailsUnderReview/emailsUnderReview.db')
OUTBOX_DB = os.path.join(os.environ.get('WORKSPACE_ROOT', '.'), 'Database/EmailsSent/email_to_be_sent.db')
DATABASE_JSON = os.path.join(os.environ.get('WORKSPACE_ROOT', '.'), 'backend/database.json')
# Import existing helpers from the same AgenticControl directory
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from Email_sender import send_email_from_database
from Send_email_db import approve_and_move_email
def send_then_move(email_id: int):
# ββ Step 1: Fetch the row from review DB ββββββββββββββββββββββββββββββββββ
try:
conn = sqlite3.connect(REVIEW_DB)
cursor = conn.cursor()
cursor.execute(
"SELECT id, body_json, timestamp, followup_date, status, "
"company_name, generated_subject, company_email, Unique_application_id "
"FROM tracking WHERE id = ?",
(email_id,)
)
row = cursor.fetchone()
conn.close()
except sqlite3.Error as e:
print(json.dumps({"ok": False, "error": f"DB fetch error: {e}"}))
return
if not row:
print(json.dumps({"ok": False, "error": f"Email ID {email_id} not found in review DB"}))
return
db_row = {
"body_json": row[1], # body_json
"company_email": row[7], # company_email
"company_name": row[5], # company_name
"unique_id": row[8], # Unique_application_id
}
# ββ Step 2: SEND the email via Gmail ββββββββββββββββββββββββββββββββββββββββ
try:
result = send_email_from_database(db_row, DATABASE_JSON, existing_unique_id=db_row["unique_id"])
if result is None:
# send_email_from_database prints the error itself but returns None on failure
print(json.dumps({"ok": False, "error": "Gmail send failed β check server logs. Email NOT moved."}))
return
except Exception as e:
print(json.dumps({"ok": False, "error": f"Send exception: {e}. Email NOT moved."}))
return
# ββ Step 3: Only if send succeeded, MOVE the row ββββββββββββββββββββββββββββ
moved = approve_and_move_email(email_id)
if moved:
print(json.dumps({
"ok": True,
"message": f"Email {email_id} sent and moved to EmailsSent. Gmail ID: {result.get('id', '?')}",
}))
else:
# Row was sent but move failed β log it clearly
print(json.dumps({
"ok": False,
"error": f"Email WAS sent (Gmail ID: {result.get('id', '?')}) but DB move FAILED. Check DB manually.",
}))
# ββ CLI entry ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == '__main__':
try:
raw = sys.stdin.read().strip()
if not raw:
print(json.dumps({"ok": False, "error": "No input provided"}))
sys.exit(1)
payload = json.loads(raw)
email_id = payload.get('id')
if email_id is None:
print(json.dumps({"ok": False, "error": "Missing 'id' in payload"}))
sys.exit(1)
send_then_move(int(email_id))
except Exception as e:
print(json.dumps({"ok": False, "error": str(e)}))
sys.exit(1)
|