Configuring the Form component to send emails

HI all,

I want to set up my form component to send emails to my email account when a form is submitted.

To do this, I created a custom code component as a test, which works fine.

export function ContactForm({ className }: { className?: string }) {
const submitForm = async (event: any) => {
event.preventDefault();
const formData = new FormData(event.target);

    console.log(JSON.stringify(Object.fromEntries(formData)))

    formData.append('access_key', 'MY_API_KEY');
    const res = await fetch('https://api.web3forms.com/submit', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/json',
        },
        body: JSON.stringify(Object.fromEntries(formData))
    }).then(res => res.json());

    if(res.success) {
        console.log('success')
    }
}
return (
    <form className={className} onSubmit={submitForm}>
        <div className="input-box">
            <input type="text" placeholder="Full Name*" name="name" required/>
            <input type="text" placeholder="Subject*" name="subject" required/>
            <input type="text" placeholder="Your email*" name="email" required/>
            <input type="text" placeholder="Write your message*" name="message" required/>
            <button type="submit">Send</button>
        </div>
    </form>
)

}

However, I would like to use the built-in form component instead for a couple of reasons:

  1. Gives me better flexibility in terms of styling; turning off simple form mode would enable me to use style presets
  2. Would like to use native features as much as possible to reduce workload

My current approach is to integrate an HTTP request when the submit button is clicked, but I am not able to access the formData object from the native form component (or at least can’t figure out how to access it)

Can someone kindly point out how I can set up my API integration?
Any pointers would be much appreciated if there is a better way to approach this problem.

Thank you!

Hi Kai, if you click the undefined text it should open up a dynamic value dialog where you can see the data available on the page. From there, you should be able to set up what data gets sent to the integration URL. I recommend going over these docs if you haven’t yet: Introduction to interactions and state | Learn Plasmic

Hi @jason ,

Thanks for the reply.

I have actually tried this, but the form → value object which I assume is the correct one, is showing undefined.

I don’t see any other form-related object aside from the isSubmitting object which is not what I’m looking for.

Is there a possibility that I’m setting up the form incorrectly?

Hi Kaai, both the values and form->value should work fine. The reason you’re not seeing anything is probably because your form fields are all empty. You can try using interactive mode to add some data into the form during development. That way, you can see the data more clearly.

1 Like

Hi @jason thank you very much, turning on interactive mode and entering values did the trick! You rock! Loving plasmic so far! :slight_smile:

1 Like