Javascript send form to email
Contact forms are essential part of every website especially portfolio websites. This is how one gets contacted. Show
However, we have many portfolios flying around without a contact form or at least, a working contact form. The reason is usually that the frontend person do not know how to create the backend functionality of the contact form. That is understandable. This is part of the reasons Emailjs was created. It is also free. Emailjs is a free tool that allows you to Send Email Directly From frontend with No server code. You can send and receive up to 200 emails using the free package. This is reasonable if you ask me. You can also upgrade with just a little token. In this tutorial, I will walk you through building a contact form that sends email directly to your email address. We will build it in plain JavaScript. We will also look at React in the next tutorial. Let's get to it. Create and Setup a free Account
Add a new Email ServiceEmail Service could be Personal or Transactional. You can read more on that here. Since we just need to receive personal emails sent to us via our website, we will stick to the Personal Email Service.
You should now have the gmail authorization screen. Please select the email you want to use
You can always come back here to disconnect I also got an email to confirm my connection to Emailjs:
Nice one!!! Do keep the Service ID because you will be needing it shortly. Create Your Email TemplateThe Email Templates is used to design how we want the emails sent to us will look. So to do that, go to the side menu in the dashboard and click the Email Templates link
You will now have the template screen like so:
I hope that makes sense. You will understand more as we proceed anyways.
Enter fullscreen mode Exit fullscreen mode My screen now looks like this: Click on Save You may choose to test it by clicking the Test It Button next to the Save Button. It's pretty straight forward.
JavaScriptStarter ProjectGet the starter code here or just create a project with a file:
index.html and add the following code:
Enter fullscreen mode Exit fullscreen mode After setting it up in your local machine, you should have the following screen:
Enter fullscreen mode Exit fullscreen mode *Do not forget to put
Your USER_ID is down below under the API Keys
Enter fullscreen mode Exit fullscreen mode The line of code you see is all you need to send messages from your form to your email address through EmailJS. You will need to change "YOUR_SERVICE_ID" and "YOUR_TEMPLATE_ID" to your actual values.
All good!!! Now when you click on the Submit button of your form, it works! But we will just get an empty email and we do not know if it actually worked or not from our website. To do that we will need to add a
Enter fullscreen mode Exit fullscreen mode we will have this:
Enter fullscreen mode Exit fullscreen mode In the code above, we just listen to when the form is submited and call the
TestingEmailJs In Action (GIF)EmailJs In Action (JPEG)
Email Received
YAAAhhhh, It works!!! Congratulations on your reaching this feet!!! ConclusionThe purpose of this tutorial was to introduce EmailJs to you especially if you are a frontend developer with no backend knowledge in handling emails, you are looking for an easier way to be contactable via your website or you are just starting out in web development. So we looked at a general overview of EmailJS, how to setup an account, create Email Services and templetes. Finally, we saw how to integrate it into your code if it is written on plain JavaScript. In the end, it was successful. I hope you had fun! All Code Are Here Next, I will be showing those who use React how this can be implemented. Until then, feel free to keep in touch. Can form data be sent via JavaScript?HTML forms can send an HTTP request declaratively. But forms can also prepare an HTTP request to send via JavaScript, for example via XMLHttpRequest .
Can I send HTML form data to email?There is no feature in HTML to send the form submission directly to an email address. What about “mailto” ? Using mailto: You can set the action field of the form as 'mailto'. In this case, the web browser invokes the email client to send the form submission to the email address specified.
How do you create a form in HTML and send it to email?Method 2: Create an Email Send Form Using PHP (Advanced). Step 1: Use PHP to create a page. For this step, you'll need to have access to your website's cPanel on your hosting platform. ... . Step 2: Make the form using code. In this step, you'll write the code to create the form. ... . Step 3: Make the form send an email.. |