Commit
Β·
de2d9d4
1
Parent(s):
dba1588
- .env +3 -2
- __pycache__/app.cpython-39.pyc +0 -0
- app.py +47 -59
- app.py.backup2 +2 -2
- app.py.backup3 +85 -0
- mailgun +1 -0
- tmp.ipynb +40 -14
.env
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
| 1 |
+
MAILGUN_API_KEY=80bd48a630cf006a54f9f2ed69c5089b-2b755df8-9c0bc598
|
| 2 |
+
MAILGUN_DOMAIN_NAME=sandbox1be2d4a019c749858ae79e16c91cb3c4.mailgun.org
|
| 3 |
+
FROM_EMAIL=leekwoon@unist.ac.kr
|
__pycache__/app.cpython-39.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-39.pyc and b/__pycache__/app.cpython-39.pyc differ
|
|
|
app.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 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 |
-
|
| 8 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from dotenv import load_dotenv
|
|
@@ -15,71 +11,63 @@ load_dotenv()
|
|
| 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 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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 |
-
|
| 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 |
try:
|
| 78 |
-
#
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
# Respond to the client immediately
|
| 82 |
-
return JSONResponse(status_code=202, content={"message": "
|
| 83 |
|
| 84 |
except Exception as e:
|
|
|
|
| 85 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import logging
|
| 4 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
| 11 |
# Set up logging configuration
|
| 12 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
|
| 14 |
+
# Initialize FastAPI app
|
| 15 |
app = FastAPI(
|
|
|
|
| 16 |
servers=[
|
| 17 |
{
|
| 18 |
"url": "https://leekwoon-email-api.hf.space",
|
|
|
|
| 19 |
}
|
| 20 |
],
|
| 21 |
)
|
| 22 |
|
| 23 |
+
def send_email_via_mailgun(to_email: str, subject: str, body: str):
|
| 24 |
+
mailgun_api_key = os.getenv("MAILGUN_API_KEY")
|
| 25 |
+
mailgun_domain = os.getenv("MAILGUN_DOMAIN_NAME")
|
| 26 |
+
from_email = os.getenv("FROM_EMAIL")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
request_url = f"https://api.mailgun.net/v3/{mailgun_domain}/messages"
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(
|
| 32 |
+
request_url,
|
| 33 |
+
auth=("api", mailgun_api_key),
|
| 34 |
+
data={
|
| 35 |
+
"from": from_email,
|
| 36 |
+
"to": [to_email],
|
| 37 |
+
"subject": subject,
|
| 38 |
+
"text": body
|
| 39 |
+
}
|
| 40 |
+
)
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
logging.debug(f"Email successfully sent to {to_email} via Mailgun")
|
| 43 |
+
|
| 44 |
+
except requests.exceptions.RequestException as e:
|
| 45 |
+
logging.error(f"Failed to send email to {to_email} via Mailgun: {str(e)}")
|
| 46 |
+
raise HTTPException(status_code=500, detail="Failed to send email via Mailgun")
|
| 47 |
+
|
| 48 |
+
@app.post("/send-chat-email")
|
| 49 |
+
def send_chat_email(chat_content: str, to_email: str, background_tasks: BackgroundTasks):
|
| 50 |
+
"""
|
| 51 |
+
Sends an email with the provided chat content to the specified email address.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
chat_content (str): The content of the chat to be sent.
|
| 55 |
+
to_email (str): The email address to send the chat content to.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
JSONResponse: A response indicating the email sending status.
|
| 59 |
+
"""
|
| 60 |
try:
|
| 61 |
+
# Email subject and body
|
| 62 |
+
email_subject = "Chat Transcript"
|
| 63 |
+
email_body = f"{chat_content}"
|
| 64 |
+
|
| 65 |
+
# Schedule the email to be sent in the background
|
| 66 |
+
background_tasks.add_task(send_email_via_mailgun, to_email, email_subject, email_body)
|
| 67 |
|
| 68 |
+
# Respond to the client immediately with an acceptance message
|
| 69 |
+
return JSONResponse(status_code=202, content={"message": "The email is being sent."})
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
# Raise an HTTP 500 error if something goes wrong
|
| 73 |
raise HTTPException(status_code=500, detail=str(e))
|
app.py.backup2
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
-
import
|
| 4 |
import logging
|
| 5 |
from email.mime.text import MIMEText
|
| 6 |
from email.mime.multipart import MIMEMultipart
|
|
@@ -33,7 +33,7 @@ executor = ThreadPoolExecutor(max_workers=3) # μ΅λ 3κ°μ μ€λ λλ‘ λΉ
|
|
| 33 |
|
| 34 |
def send_email_(to_email: str):
|
| 35 |
smtp_server = "smtp.gmail.com"
|
| 36 |
-
smtp_port =
|
| 37 |
smtp_user = os.getenv("SMTP_USER")
|
| 38 |
smtp_password = os.getenv("SMTP_PASSWORD")
|
| 39 |
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import μ€lib
|
| 4 |
import logging
|
| 5 |
from email.mime.text import MIMEText
|
| 6 |
from email.mime.multipart import MIMEMultipart
|
|
|
|
| 33 |
|
| 34 |
def send_email_(to_email: str):
|
| 35 |
smtp_server = "smtp.gmail.com"
|
| 36 |
+
smtp_port = 587
|
| 37 |
smtp_user = os.getenv("SMTP_USER")
|
| 38 |
smtp_password = os.getenv("SMTP_PASSWORD")
|
| 39 |
|
app.py.backup3
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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))
|
mailgun
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80bd48a630cf006a54f9f2ed69c5089b-2b755df8-9c0bc598
|
tmp.ipynb
CHANGED
|
@@ -49,9 +49,24 @@
|
|
| 49 |
},
|
| 50 |
{
|
| 51 |
"cell_type": "code",
|
| 52 |
-
"execution_count":
|
| 53 |
"metadata": {},
|
| 54 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
"source": [
|
| 56 |
"import os\n",
|
| 57 |
"from postmarker.core import PostmarkClient\n",
|
|
@@ -64,21 +79,32 @@
|
|
| 64 |
"# Specify the file path\n",
|
| 65 |
"file_path = './README.md'\n",
|
| 66 |
"\n",
|
| 67 |
-
"# Send the email with the attachment\n",
|
| 68 |
-
"postmark.emails.send(\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
" From='leekwoon@unist.ac.kr',\n",
|
| 70 |
" To='leekwoon@unist.ac.kr',\n",
|
| 71 |
-
" Subject=
|
| 72 |
-
" HtmlBody=body
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"
|
| 77 |
-
"
|
| 78 |
-
" }\n",
|
| 79 |
-
" ]\n",
|
| 80 |
-
")"
|
| 81 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
],
|
| 84 |
"metadata": {
|
|
|
|
| 49 |
},
|
| 50 |
{
|
| 51 |
"cell_type": "code",
|
| 52 |
+
"execution_count": 4,
|
| 53 |
"metadata": {},
|
| 54 |
+
"outputs": [
|
| 55 |
+
{
|
| 56 |
+
"data": {
|
| 57 |
+
"text/plain": [
|
| 58 |
+
"{'ErrorCode': 0,\n",
|
| 59 |
+
" 'Message': 'OK',\n",
|
| 60 |
+
" 'MessageID': '4a1f44e1-d0e8-4d3a-a50c-3b9a2e366fa0',\n",
|
| 61 |
+
" 'SubmittedAt': '2024-09-03T10:46:41.7877421Z',\n",
|
| 62 |
+
" 'To': 'leekwoon@unist.ac.kr'}"
|
| 63 |
+
]
|
| 64 |
+
},
|
| 65 |
+
"execution_count": 4,
|
| 66 |
+
"metadata": {},
|
| 67 |
+
"output_type": "execute_result"
|
| 68 |
+
}
|
| 69 |
+
],
|
| 70 |
"source": [
|
| 71 |
"import os\n",
|
| 72 |
"from postmarker.core import PostmarkClient\n",
|
|
|
|
| 79 |
"# Specify the file path\n",
|
| 80 |
"file_path = './README.md'\n",
|
| 81 |
"\n",
|
| 82 |
+
"# # Send the email with the attachment\n",
|
| 83 |
+
"# postmark.emails.send(\n",
|
| 84 |
+
"# From='leekwoon@unist.ac.kr',\n",
|
| 85 |
+
"# To='leekwoon@unist.ac.kr',\n",
|
| 86 |
+
"# Subject=subject,\n",
|
| 87 |
+
"# HtmlBody=body,\n",
|
| 88 |
+
"# )\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"email = postmark.emails.Email(\n",
|
| 91 |
" From='leekwoon@unist.ac.kr',\n",
|
| 92 |
" To='leekwoon@unist.ac.kr',\n",
|
| 93 |
+
" Subject='Postmark test',\n",
|
| 94 |
+
" HtmlBody='<html><body><strong>Hello</strong> dear Postmark user.</body></html>'\n",
|
| 95 |
+
")\n",
|
| 96 |
+
"email['X-Accept-Language'] = 'en-us, en'\n",
|
| 97 |
+
"email.attach('./README.md')\n",
|
| 98 |
+
"email.attach_binary(content=b'content', filename='readme.md')\n",
|
| 99 |
+
"email.send()\n"
|
|
|
|
|
|
|
|
|
|
| 100 |
]
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"cell_type": "code",
|
| 104 |
+
"execution_count": null,
|
| 105 |
+
"metadata": {},
|
| 106 |
+
"outputs": [],
|
| 107 |
+
"source": []
|
| 108 |
}
|
| 109 |
],
|
| 110 |
"metadata": {
|