Transpolymer2 / contact.py
transpolymer's picture
Update contact.py
33dbc5c verified
raw
history blame
2.21 kB
import streamlit as st
import streamlit.components.v1 as components
def show():
st.title("πŸ“¬ Contact Us")
st.write("Feel free to reach out to us by filling the form below.")
public_key = st.secrets["emailjs"]["public_key"]
service_id = st.secrets["emailjs"]["service_id"]
template_id = st.secrets["emailjs"]["template_id"]
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)
# Call the function to display the contact form
show()