dahyedahye commited on
Commit
f996a4d
Β·
1 Parent(s): 76b4277
Files changed (3) hide show
  1. .env +2 -2
  2. app.py +48 -50
  3. app.py.backup β†’ app.py.backup2 +57 -55
.env CHANGED
@@ -1,2 +1,2 @@
1
- SMTP_USER=kyowoon.lee1924@gmail.com
2
- SMTP_PASSWORD=rhwy amyv hdwq pspa
 
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
- 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
@@ -27,61 +25,61 @@ app = FastAPI(
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))
 
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 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
16
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
17
 
18
  app = FastAPI(
19
- # version="0.0.1",
20
- # servers=[
21
- # {
22
- # "url": "https://leekwoon-email-api.hf.space",
23
- # "description": "email API",
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
- # 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))
 
 
 
 
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))