File size: 2,107 Bytes
a55a905
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import streamlit.components.v1 as components

# Load EmailJS credentials securely from secrets.toml
public_key = st.secrets["emailjs"]["public_key"]
service_id = st.secrets["emailjs"]["service_id"]
template_id = st.secrets["emailjs"]["template_id"]

st.title("📬 Contact Us")
st.write("Feel free to reach out to us by filling the form below.")

# Contact form using Streamlit
with st.form(key="contact_form"):
    name = st.text_input("Your Name")
    email = st.text_input("Your Email")
    message = st.text_area("Your Message")
    submit_button = st.form_submit_button("Send")

    if submit_button:
        if name and email and message:
            st.success("Sending your message...")

            js_code = f"""
                var templateParams = {{
                    name: "{name}",
                    email: "{email}",
                    message: `{message}`
                }};

                emailjs.send("{service_id}", "{template_id}", templateParams, "{public_key}")
                    .then(function(response) {{
                        console.log("SUCCESS!", response.status, response.text);
                        window.parent.postMessage({{"type": "success", "message": "Email sent successfully!"}}, "*");
                    }}, function(error) {{
                        console.log("FAILED...", error);
                        window.parent.postMessage({{"type": "error", "message": "Failed to send email."}}, "*");
                    }});
            """

            components.html(f"""
                <html>
                <head>
                    <script src="https://cdn.jsdelivr.net/npm/emailjs-com@2.6.4/dist/email.min.js"></script>
                    <script>
                        (function() {{
                            emailjs.init("{public_key}");
                            {js_code}
                        }})();
                    </script>
                </head>
                <body></body>
                </html>
            """, height=0)

        else:
            st.error("Please fill out all the fields.")