Spaces:
Sleeping
Sleeping
Upload contact.py
Browse files- contact.py +55 -0
contact.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import smtplib
|
3 |
+
from email.mime.text import MIMEText
|
4 |
+
|
5 |
+
# Function to send email
|
6 |
+
def send_email(name, user_email, message):
|
7 |
+
email_sender = st.secrets["gmail"]["email"]
|
8 |
+
email_password = st.secrets["gmail"]["app_password"]
|
9 |
+
email_receiver = email_sender
|
10 |
+
|
11 |
+
subject = f"New Contact Form Message from {name}"
|
12 |
+
body = f"Name: {name}\nEmail: {user_email}\n\nMessage:\n{message}"
|
13 |
+
|
14 |
+
msg = MIMEText(body)
|
15 |
+
msg["Subject"] = subject
|
16 |
+
msg["From"] = user_email
|
17 |
+
msg["To"] = email_receiver
|
18 |
+
|
19 |
+
try:
|
20 |
+
server = smtplib.SMTP("smtp.gmail.com", 587)
|
21 |
+
server.starttls()
|
22 |
+
server.login(email_sender, email_password)
|
23 |
+
server.sendmail(user_email, email_receiver, msg.as_string())
|
24 |
+
server.quit()
|
25 |
+
return True
|
26 |
+
except Exception as e:
|
27 |
+
st.error(f"β Error sending email: {e}")
|
28 |
+
return False
|
29 |
+
|
30 |
+
# UI
|
31 |
+
def show():
|
32 |
+
st.title("π Contact Us - TransPolymer")
|
33 |
+
st.write("We're here to help! Please use the form below to contact us.")
|
34 |
+
|
35 |
+
col1, col2 = st.columns(2)
|
36 |
+
|
37 |
+
with col1:
|
38 |
+
st.subheader("π¬ Contact Form")
|
39 |
+
name = st.text_input("Name")
|
40 |
+
user_email = st.text_input("Your Email")
|
41 |
+
message = st.text_area("Message")
|
42 |
+
|
43 |
+
if st.button("π¨ Submit"):
|
44 |
+
if name and user_email and message:
|
45 |
+
if send_email(name, user_email, message):
|
46 |
+
st.success("β
Message sent! Weβll reply to your email soon.")
|
47 |
+
else:
|
48 |
+
st.error("β Failed to send. Please try again.")
|
49 |
+
else:
|
50 |
+
st.warning("β οΈ Fill in all fields.")
|
51 |
+
|
52 |
+
with col2:
|
53 |
+
st.subheader("π Contact Information")
|
54 |
+
st.write("*TransPolymer Project Team*")
|
55 |
+
st.markdown("[π§ transpolymer2@gmail.com](mailto:transpolymer2@gmail.com)")
|