Spaces:
Sleeping
Sleeping
Ilyas KHIAT
commited on
Commit
·
b17e1ae
1
Parent(s):
3623aa3
emailing and prompt enhance
Browse files
main.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException, UploadFile, File,Request,Depends,status
|
2 |
from fastapi.security import OAuth2PasswordBearer
|
3 |
-
from pydantic import BaseModel, Json
|
4 |
from typing import Optional
|
5 |
from pinecone import Pinecone, ServerlessSpec
|
6 |
from uuid import uuid4
|
@@ -14,6 +14,10 @@ from typing import Literal
|
|
14 |
import time
|
15 |
from fastapi.middleware.cors import CORSMiddleware
|
16 |
|
|
|
|
|
|
|
|
|
17 |
load_dotenv()
|
18 |
|
19 |
## setup pinecone index
|
@@ -61,6 +65,37 @@ else:
|
|
61 |
|
62 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
class UserInput(BaseModel):
|
66 |
query: str
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File,Request,Depends,status,BackgroundTasks
|
2 |
from fastapi.security import OAuth2PasswordBearer
|
3 |
+
from pydantic import BaseModel, Json,EmailStr
|
4 |
from typing import Optional
|
5 |
from pinecone import Pinecone, ServerlessSpec
|
6 |
from uuid import uuid4
|
|
|
14 |
import time
|
15 |
from fastapi.middleware.cors import CORSMiddleware
|
16 |
|
17 |
+
import smtplib
|
18 |
+
from email.mime.text import MIMEText
|
19 |
+
|
20 |
+
|
21 |
load_dotenv()
|
22 |
|
23 |
## setup pinecone index
|
|
|
65 |
|
66 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
67 |
|
68 |
+
# Pydantic model for the form data
|
69 |
+
class ContactForm(BaseModel):
|
70 |
+
name: str
|
71 |
+
email: EmailStr
|
72 |
+
message: str
|
73 |
+
|
74 |
+
# Function to send email
|
75 |
+
def send_email(form_data: ContactForm):
|
76 |
+
sender_email = os.getenv("SENDER_EMAIL")
|
77 |
+
sender_password = os.getenv("SENDER_PASSWORD")
|
78 |
+
receiver_email = os.getenv("RECEIVER_EMAIL") # Your email
|
79 |
+
|
80 |
+
# Setup the message content
|
81 |
+
message = MIMEText(f"Name: {form_data.name}\nEmail: {form_data.email}\nMessage: {form_data.message}")
|
82 |
+
message['Subject'] = f"New contact form submission from {form_data.name}"
|
83 |
+
message['From'] = sender_email
|
84 |
+
message['To'] = receiver_email
|
85 |
+
|
86 |
+
try:
|
87 |
+
# Connect to the SMTP server and send the email
|
88 |
+
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
|
89 |
+
server.login(sender_email, sender_password)
|
90 |
+
server.sendmail(sender_email, receiver_email, message.as_string())
|
91 |
+
except Exception as e:
|
92 |
+
raise HTTPException(status_code=500, detail=str(e))
|
93 |
+
|
94 |
+
# Endpoint to handle form submission
|
95 |
+
@app.post("/send_email/")
|
96 |
+
async def send_contact_form(form_data: ContactForm, background_tasks: BackgroundTasks):
|
97 |
+
background_tasks.add_task(send_email, form_data)
|
98 |
+
return {"message": "Email sent successfully!"}
|
99 |
|
100 |
class UserInput(BaseModel):
|
101 |
query: str
|