transpolymer commited on
Commit
a55a905
Β·
verified Β·
1 Parent(s): 3c1e1fb

Update contact.py

Browse files
Files changed (1) hide show
  1. contact.py +56 -55
contact.py CHANGED
@@ -1,55 +1,56 @@
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)")
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
3
+
4
+ # Load EmailJS credentials securely from secrets.toml
5
+ public_key = st.secrets["emailjs"]["public_key"]
6
+ service_id = st.secrets["emailjs"]["service_id"]
7
+ template_id = st.secrets["emailjs"]["template_id"]
8
+
9
+ st.title("πŸ“¬ Contact Us")
10
+ st.write("Feel free to reach out to us by filling the form below.")
11
+
12
+ # Contact form using Streamlit
13
+ with st.form(key="contact_form"):
14
+ name = st.text_input("Your Name")
15
+ email = st.text_input("Your Email")
16
+ message = st.text_area("Your Message")
17
+ submit_button = st.form_submit_button("Send")
18
+
19
+ if submit_button:
20
+ if name and email and message:
21
+ st.success("Sending your message...")
22
+
23
+ js_code = f"""
24
+ var templateParams = {{
25
+ name: "{name}",
26
+ email: "{email}",
27
+ message: `{message}`
28
+ }};
29
+
30
+ emailjs.send("{service_id}", "{template_id}", templateParams, "{public_key}")
31
+ .then(function(response) {{
32
+ console.log("SUCCESS!", response.status, response.text);
33
+ window.parent.postMessage({{"type": "success", "message": "Email sent successfully!"}}, "*");
34
+ }}, function(error) {{
35
+ console.log("FAILED...", error);
36
+ window.parent.postMessage({{"type": "error", "message": "Failed to send email."}}, "*");
37
+ }});
38
+ """
39
+
40
+ components.html(f"""
41
+ <html>
42
+ <head>
43
+ <script src="https://cdn.jsdelivr.net/npm/emailjs-com@2.6.4/dist/email.min.js"></script>
44
+ <script>
45
+ (function() {{
46
+ emailjs.init("{public_key}");
47
+ {js_code}
48
+ }})();
49
+ </script>
50
+ </head>
51
+ <body></body>
52
+ </html>
53
+ """, height=0)
54
+
55
+ else:
56
+ st.error("Please fill out all the fields.")