Commit
Β·
f996a4d
1
Parent(s):
76b4277
- .env +2 -2
- app.py +48 -50
- app.py.backup β app.py.backup2 +57 -55
.env
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 1 |
+
POSTMARK_API_KEY=db96eba8-a4b5-4b83-9377-2e7dcdc00d39
|
| 2 |
+
SMTP_USER=kyowoon.lee1924@gmail.com
|
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import re
|
| 3 |
-
import smtplib
|
| 4 |
import logging
|
| 5 |
-
|
| 6 |
-
from email.mime.multipart import MIMEMultipart
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
# .env νμΌμμ μ€μν νκ²½ λ³μ λ‘λ
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
# Set up logging configuration
|
|
@@ -27,61 +25,61 @@ app = FastAPI(
|
|
| 27 |
],
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
|
|
|
| 33 |
|
| 34 |
def send_email_(to_email: str):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
smtp_user = os.getenv("SMTP_USER")
|
| 38 |
-
smtp_password = os.getenv("SMTP_PASSWORD")
|
| 39 |
-
|
| 40 |
-
logging.debug(f"smtp_user: {smtp_user}")
|
| 41 |
-
logging.debug(f"smtp_password: {smtp_password}")
|
| 42 |
|
| 43 |
subject = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€."
|
| 44 |
-
body =
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
# with smtplib.SMTP(smtp_server, smtp_port) as server:
|
| 62 |
-
logging.debug(f"debug 2")
|
| 63 |
-
server.starttls()
|
| 64 |
-
logging.debug(f"debug 2")
|
| 65 |
-
server.login(smtp_user, smtp_password)
|
| 66 |
-
logging.debug(f"debug 3")
|
| 67 |
-
server.sendmail(smtp_user, to_email, msg.as_string())
|
| 68 |
-
|
| 69 |
-
# Log success message
|
| 70 |
-
logging.debug(f"Email successfully sent to {to_email}")
|
| 71 |
-
|
| 72 |
-
except Exception as e:
|
| 73 |
-
# Log any exceptions that occur during the email sending process
|
| 74 |
-
logging.error(f"Failed to send email to {to_email}: {str(e)}")
|
| 75 |
-
raise e
|
| 76 |
-
|
| 77 |
@app.post("/send-email")
|
| 78 |
def send_email(email: str, background_tasks: BackgroundTasks):
|
| 79 |
try:
|
| 80 |
-
# Schedule the
|
| 81 |
background_tasks.add_task(executor.submit, send_email_, email)
|
| 82 |
|
| 83 |
# Respond to the client immediately
|
| 84 |
return JSONResponse(status_code=202, content={"message": "You will receive an email when it's done."})
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
+
# POSTMARK_API_KEY=db96eba8-a4b5-4b83-9377-2e7dcdc00d39
|
| 2 |
+
# SMTP_USER=kyowoon.lee1924@gmail.com
|
| 3 |
+
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
import logging
|
| 6 |
+
import requests
|
|
|
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
+
# Load environment variables from .env file
|
|
|
|
|
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# Set up logging configuration
|
|
|
|
| 25 |
],
|
| 26 |
)
|
| 27 |
|
| 28 |
+
from postmarker.core import PostmarkClient
|
| 29 |
+
postmark = PostmarkClient(server_token=os.getenv("POSTMARK_API_KEY"))
|
| 30 |
|
| 31 |
+
executor = ThreadPoolExecutor(max_workers=3) # μ΅λ 3κ°μ μ€λ λλ‘ λΉλκΈ° μμ
μ²λ¦¬
|
| 32 |
|
| 33 |
def send_email_(to_email: str):
|
| 34 |
+
# postmark_api_key = os.getenv("POSTMARK_API_KEY")
|
| 35 |
+
from_email = os.getenv("SMTP_USER") # This should be your verified Postmark sender email
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
subject = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€."
|
| 38 |
+
body = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€.SRT νμΌμ 첨λΆνμ¬ μ λ¬λ립λλ€."
|
| 39 |
+
|
| 40 |
+
postmark.emails.send(
|
| 41 |
+
From=os.getenv("SMTP_USER"),
|
| 42 |
+
To=to_email,
|
| 43 |
+
Subject=subject,
|
| 44 |
+
HtmlBody=body
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# headers = {
|
| 48 |
+
# "Accept": "application/json",
|
| 49 |
+
# "X-Postmark-Server-Token": postmark_api_key,
|
| 50 |
+
# "Content-Type": "application/json",
|
| 51 |
+
# }
|
| 52 |
+
|
| 53 |
+
# data = {
|
| 54 |
+
# "From": from_email,
|
| 55 |
+
# "To": to_email,
|
| 56 |
+
# "Subject": subject,
|
| 57 |
+
# "TextBody": body,
|
| 58 |
+
# }
|
| 59 |
+
|
| 60 |
+
# try:
|
| 61 |
+
# print('hi')
|
| 62 |
+
# response = requests.post(
|
| 63 |
+
# "https://api.postmarkapp.com/email",
|
| 64 |
+
# headers=headers,
|
| 65 |
+
# json=data
|
| 66 |
+
# )
|
| 67 |
+
# response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
|
| 68 |
+
# logging.debug(f"Email successfully sent to {to_email}, Response: {response.json()}")
|
| 69 |
+
|
| 70 |
+
# except requests.exceptions.RequestException as e:
|
| 71 |
+
# # Log any exceptions that occur during the email sending process
|
| 72 |
+
# logging.error(f"Failed to send email to {to_email}: {str(e)}")
|
| 73 |
+
# raise e
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@app.post("/send-email")
|
| 76 |
def send_email(email: str, background_tasks: BackgroundTasks):
|
| 77 |
try:
|
| 78 |
+
# Schedule the email sending in the background
|
| 79 |
background_tasks.add_task(executor.submit, send_email_, email)
|
| 80 |
|
| 81 |
# Respond to the client immediately
|
| 82 |
return JSONResponse(status_code=202, content={"message": "You will receive an email when it's done."})
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
raise HTTPException(status_code=500, detail=str(e))
|
app.py.backup β app.py.backup2
RENAMED
|
@@ -1,85 +1,87 @@
|
|
| 1 |
-
# POSTMARK_API_KEY=db96eba8-a4b5-4b83-9377-2e7dcdc00d39
|
| 2 |
-
# SMTP_USER=kyowoon.lee1924@gmail.com
|
| 3 |
-
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
import logging
|
| 6 |
-
import
|
|
|
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# Set up logging configuration
|
| 16 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 17 |
|
| 18 |
app = FastAPI(
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
)
|
| 27 |
|
| 28 |
-
from postmarker.core import PostmarkClient
|
| 29 |
-
postmark = PostmarkClient(server_token=os.getenv("POSTMARK_API_KEY"))
|
| 30 |
-
|
| 31 |
executor = ThreadPoolExecutor(max_workers=3) # μ΅λ 3κ°μ μ€λ λλ‘ λΉλκΈ° μμ
μ²λ¦¬
|
| 32 |
|
|
|
|
|
|
|
| 33 |
def send_email_(to_email: str):
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
subject = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€."
|
| 38 |
-
body = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€.SRT νμΌμ 첨λΆνμ¬ μ λ¬λ립λλ€."
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# "From": from_email,
|
| 55 |
-
# "To": to_email,
|
| 56 |
-
# "Subject": subject,
|
| 57 |
-
# "TextBody": body,
|
| 58 |
-
# }
|
| 59 |
-
|
| 60 |
-
# try:
|
| 61 |
-
# print('hi')
|
| 62 |
-
# response = requests.post(
|
| 63 |
-
# "https://api.postmarkapp.com/email",
|
| 64 |
-
# headers=headers,
|
| 65 |
-
# json=data
|
| 66 |
-
# )
|
| 67 |
-
# response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
|
| 68 |
-
# logging.debug(f"Email successfully sent to {to_email}, Response: {response.json()}")
|
| 69 |
-
|
| 70 |
-
# except requests.exceptions.RequestException as e:
|
| 71 |
-
# # Log any exceptions that occur during the email sending process
|
| 72 |
-
# logging.error(f"Failed to send email to {to_email}: {str(e)}")
|
| 73 |
-
# raise e
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@app.post("/send-email")
|
| 76 |
def send_email(email: str, background_tasks: BackgroundTasks):
|
| 77 |
try:
|
| 78 |
-
# Schedule the email sending in the background
|
| 79 |
background_tasks.add_task(executor.submit, send_email_, email)
|
| 80 |
|
| 81 |
# Respond to the client immediately
|
| 82 |
return JSONResponse(status_code=202, content={"message": "You will receive an email when it's done."})
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
+
import smtplib
|
| 4 |
import logging
|
| 5 |
+
from email.mime.text import MIMEText
|
| 6 |
+
from email.mime.multipart import MIMEMultipart
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
+
# κ΅¬κΈ λλΌμ΄λΈ λ§ν¬, μ΄λ€ μΈμ΄, λ°μ μ΄λ©μΌ μ£Όμ
|
| 13 |
+
|
| 14 |
+
# .env νμΌμμ μ€μν νκ²½ λ³μ λ‘λ
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
# Set up logging configuration
|
| 18 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 19 |
|
| 20 |
app = FastAPI(
|
| 21 |
+
version="0.0.1",
|
| 22 |
+
servers=[
|
| 23 |
+
{
|
| 24 |
+
"url": "https://leekwoon-email-api.hf.space",
|
| 25 |
+
"description": "email API",
|
| 26 |
+
}
|
| 27 |
+
],
|
| 28 |
)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
executor = ThreadPoolExecutor(max_workers=3) # μ΅λ 3κ°μ μ€λ λλ‘ λΉλκΈ° μμ
μ²λ¦¬
|
| 31 |
|
| 32 |
+
|
| 33 |
+
|
| 34 |
def send_email_(to_email: str):
|
| 35 |
+
smtp_server = "smtp.gmail.com"
|
| 36 |
+
smtp_port = 2525
|
| 37 |
+
smtp_user = os.getenv("SMTP_USER")
|
| 38 |
+
smtp_password = os.getenv("SMTP_PASSWORD")
|
| 39 |
+
|
| 40 |
+
logging.debug(f"smtp_user: {smtp_user}")
|
| 41 |
+
logging.debug(f"smtp_password: {smtp_password}")
|
| 42 |
|
| 43 |
subject = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€."
|
| 44 |
+
body = f"[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€.SRT νμΌμ 첨λΆνμ¬ μ λ¬λ립λλ€."
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
|
| 48 |
+
msg = MIMEMultipart()
|
| 49 |
+
msg["From"] = smtp_user
|
| 50 |
+
msg["To"] = to_email
|
| 51 |
+
msg["Subject"] = subject
|
| 52 |
+
|
| 53 |
+
msg.attach(MIMEText(body, "plain"))
|
| 54 |
+
logging.debug(f"debug 1")
|
| 55 |
+
|
| 56 |
+
server = smtplib.SMTP('smtp.gmail.com', 3000)
|
| 57 |
+
logging.debug(f"#")
|
| 58 |
+
server.ehlo()
|
| 59 |
+
logging.debug(f"#")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# with smtplib.SMTP(smtp_server, smtp_port) as server:
|
| 62 |
+
logging.debug(f"debug 2")
|
| 63 |
+
server.starttls()
|
| 64 |
+
logging.debug(f"debug 2")
|
| 65 |
+
server.login(smtp_user, smtp_password)
|
| 66 |
+
logging.debug(f"debug 3")
|
| 67 |
+
server.sendmail(smtp_user, to_email, msg.as_string())
|
| 68 |
+
|
| 69 |
+
# Log success message
|
| 70 |
+
logging.debug(f"Email successfully sent to {to_email}")
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
# Log any exceptions that occur during the email sending process
|
| 74 |
+
logging.error(f"Failed to send email to {to_email}: {str(e)}")
|
| 75 |
+
raise e
|
| 76 |
+
|
| 77 |
@app.post("/send-email")
|
| 78 |
def send_email(email: str, background_tasks: BackgroundTasks):
|
| 79 |
try:
|
| 80 |
+
# Schedule the transcription and email sending in the background
|
| 81 |
background_tasks.add_task(executor.submit, send_email_, email)
|
| 82 |
|
| 83 |
# Respond to the client immediately
|
| 84 |
return JSONResponse(status_code=202, content={"message": "You will receive an email when it's done."})
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
raise HTTPException(status_code=500, detail=str(e))
|