How do I submit a contact form and receive an email by using HTML?
How to send an email directly from your HTML form?
It would have been a nice feature for a web developer if the browsers let them route the form submission directly to an email address. But this is not the case. The reason is that if the browsers allowed emailing directly from the form page, that will reveal the visitor's email address. A malicious hacker can collect the email address of the visitors to the web page and then spam them. In order to protect the web users, no client-side language can send email without the user’s intervention.
Is there an HTML code to send email from a form submission?
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.
1
</pre>
2
<form
action="mailto:you@yourwebsite.com">
However, this method is not compatible across browsers. It is not user-friendly too; on form submission, it throws a warning message.
So, How to get an email from an HTML form?
Before jumping into the solution, I will take you through the general structure of a web form. A webform has two parts: the front-end of the form that you see in your browser and a back-end script that runs on the webserver.
Your web browser displays the Form using the HTML front-end code. When you submit the form, the browser sends the information you submitted in the form to the back-end.
The link to the back-end script is mentioned in the ‘action’ attribute of the HTML form tag.
1
<form
action="http://yourwebsite.com/yourform-processor.php">
The browser picks the link (URL) mentioned in the action attribute and sends the form submission data to that URL. The web server passes the form submission data to the script in the action URL (your form-processor.php in the example.)
Now, the back-end script can send emails, save form submission to a database or even direct the user to a payment page.
How to create a back-end Script?
There are a number of scripting languages created for back-end programming. PHP is one of the popular and widely supported scripting platform (Others include Perl, Ruby, ASP- for windows only- etc) Almost all hosting services support PHP on their webservers.
Working of a simple PHP script
The PHP script runs on your web server. When a form is submitted, the form mail script collects the data submitted in the form, composes an email and sends the email to the address it is configured.
Hope this works !!
Also, if you are new t o HTML , you can learn HTML from basics from here-