-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
166 lines (139 loc) · 4.93 KB
/
main.py
File metadata and controls
166 lines (139 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from fastapi import FastAPI, Request, Form
from fastapi.middleware.cors import CORSMiddleware
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import json
import os
app = FastAPI()
# Allow CORS for local development
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Define the path for the JSON files
DATA_FILE_PATH = "form_data.json"
CONTACT_FILE_PATH = "contact_form_data.json"
@app.get("/")
async def read_root():
return {"message": "Welcome to the FastAPI server!"}
@app.post("/submit-form")
async def submit_form(request: Request):
data = await request.json()
name = data.get("name")
email = data.get("email")
phone = data.get("phone")
message = data.get("message")
medical_issue = data.get("medical_issue")
preferred_contact = data.get("preferred_contact")
print(f"Received data: name={name}, email={email}, phone={phone}, message={message}, medical_issue={medical_issue}, preferred_contact={preferred_contact}")
# Create a dictionary with the form data
form_data = {
"name": name,
"email": email,
"phone": phone,
"message": message,
"medical_issue": medical_issue,
"preferred_contact": preferred_contact
}
# Load existing data from the JSON file
if os.path.exists(DATA_FILE_PATH):
with open(DATA_FILE_PATH, "r") as file:
data = json.load(file)
else:
data = []
# Append the new form data
data.append(form_data)
# Save the updated data back to the JSON file
with open(DATA_FILE_PATH, "w") as file:
json.dump(data, file, indent=4)
# Email configuration using Gmail with App Password
sender_email = "noursalem@gmail.com" # Your Gmail address
receiver_email = "noursalem@gmail.com" # The email where you want to receive messages
password = "kxra osiz zlgx cjza" # Your App Password from Google
# SMTP server settings for Gmail
smtp_server = "smtp.gmail.com"
smtp_port = 587
# Create the email content
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = receiver_email
msg["Subject"] = "New Contact Form Submission"
body = f"""
Name: {name}
Email: {email}
Phone: {phone}
Message: {message}
Medical Issue: {medical_issue}
Preferred Contact Method: {preferred_contact}
"""
msg.attach(MIMEText(body, "plain"))
# Send the email
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
return {"message": "Form submitted successfully!"}
except Exception as e:
return {"error": str(e)}
@app.post("/submit-contact-form")
async def submit_contact_form(request: Request):
data = await request.json()
name = data.get("name")
email = data.get("email")
message = data.get("message")
date = data.get("date")
print(f"Received contact form data: name={name}, email={email}, message={message}, date={date}")
# Create a dictionary with the contact form data
contact_form_data = {
"name": name,
"email": email,
"message": message,
"date": date
}
# Load existing data from the contact JSON file
if os.path.exists(CONTACT_FILE_PATH):
with open(CONTACT_FILE_PATH, "r") as file:
data = json.load(file)
else:
data = []
# Append the new contact form data
data.append(contact_form_data)
# Save the updated data back to the contact JSON file
with open(CONTACT_FILE_PATH, "w") as file:
json.dump(data, file, indent=4)
# Email configuration using Gmail with App Password
sender_email = "noursalem741@gmail.com" # Your Gmail address
receiver_email = "noursalem741@gmail.com" # The email where you want to receive messages
password = "kxra osie zlgk cjza" # Your App Password from Google
# SMTP server settings for Gmail
smtp_server = "smtp.gmail.com"
smtp_port = 587
# Create the email content
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = receiver_email
msg["Subject"] = "New Contact Form Submission"
body = f"""
Name: {name}
Email: {email}
Message: {message}
Date: {date}
"""
msg.attach(MIMEText(body, "plain"))
# Send the email
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
return {"message": "Contact form submitted successfully!"} # Ensure JSON response with message
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)