|
|
import os |
|
|
import re |
|
|
import μ€lib |
|
|
import logging |
|
|
from email.mime.text import MIMEText |
|
|
from email.mime.multipart import MIMEMultipart |
|
|
from concurrent.futures import ThreadPoolExecutor |
|
|
from fastapi import FastAPI, HTTPException, BackgroundTasks |
|
|
from fastapi.responses import JSONResponse |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
|
|
app = FastAPI( |
|
|
version="0.0.1", |
|
|
servers=[ |
|
|
{ |
|
|
"url": "https://leekwoon-email-api.hf.space", |
|
|
"description": "email API", |
|
|
} |
|
|
], |
|
|
) |
|
|
|
|
|
executor = ThreadPoolExecutor(max_workers=3) # μ΅λ 3κ°μ μ€λ λλ‘ λΉλκΈ° μμ
μ²λ¦¬ |
|
|
|
|
|
|
|
|
|
|
|
def send_email_(to_email: str): |
|
|
smtp_server = "smtp.gmail.com" |
|
|
smtp_port = 587 |
|
|
smtp_user = os.getenv("SMTP_USER") |
|
|
smtp_password = os.getenv("SMTP_PASSWORD") |
|
|
|
|
|
logging.debug(f"smtp_user: {smtp_user}") |
|
|
logging.debug(f"smtp_password: {smtp_password}") |
|
|
|
|
|
subject = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€." |
|
|
body = f"[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€.SRT νμΌμ 첨λΆνμ¬ μ λ¬λ립λλ€." |
|
|
|
|
|
try: |
|
|
|
|
|
msg = MIMEMultipart() |
|
|
msg["From"] = smtp_user |
|
|
msg["To"] = to_email |
|
|
msg["Subject"] = subject |
|
|
|
|
|
msg.attach(MIMEText(body, "plain")) |
|
|
logging.debug(f"debug 1") |
|
|
|
|
|
server = smtplib.SMTP('smtp.gmail.com', 3000) |
|
|
logging.debug(f"#") |
|
|
server.ehlo() |
|
|
logging.debug(f"#") |
|
|
|
|
|
# with smtplib.SMTP(smtp_server, smtp_port) as server: |
|
|
logging.debug(f"debug 2") |
|
|
server.starttls() |
|
|
logging.debug(f"debug 2") |
|
|
server.login(smtp_user, smtp_password) |
|
|
logging.debug(f"debug 3") |
|
|
server.sendmail(smtp_user, to_email, msg.as_string()) |
|
|
|
|
|
# Log success message |
|
|
logging.debug(f"Email successfully sent to {to_email}") |
|
|
|
|
|
except Exception as e: |
|
|
# Log any exceptions that occur during the email sending process |
|
|
logging.error(f"Failed to send email to {to_email}: {str(e)}") |
|
|
raise e |
|
|
|
|
|
@app.post("/send-email") |
|
|
def send_email(email: str, background_tasks: BackgroundTasks): |
|
|
try: |
|
|
# Schedule the transcription and email sending in the background |
|
|
background_tasks.add_task(executor.submit, send_email_, email) |
|
|
|
|
|
# Respond to the client immediately |
|
|
return JSONResponse(status_code=202, content={"message": "You will receive an email when it's done."}) |
|
|
|
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |