Functional Form To Send Email From Web Page
82Mail Form PHP/HTML
There are many cases when building a web-page when you have the need to send an email to your own email address, such as the case of a "contact-us" form.
These are typically used for requesting information or sending a message to the relevant party affiliated with the website, for example, a sales person following up on leads.
There is an adventatious side-effect to using a form instead of a link to an email address. This is that web-crawlers will not be able to see the email address and therefore not harvest it for spam databases.
For the purpose of this tutorial, I will assume that the reader has some basic scripting and html experience. For this example, I am going to use PHP which, if you are hosting your site with any major web-host you will have available to you.
Creating A Form
Create a form as usual in HTML. You will have your regular input values and submit button. The difference here is that you will change your action to point to a created PHP page, e.g. action="sendMail.php"
The form for this example must contain two input fields, emailFrom and message.
PHP Send Mail Page
This would be a seperately created .php file which would reside in the same directory as the page wanting to send the email. The example below is the entire page and can be copied without any knowledge of PHP.
<code>
<? PHP
$message = $_GET['message'] ;
$email = $_GET['emailFrom'] ;
mail( "yourEmail@yourDomain.com", "Subject of email, change this to whatever you want", $message, "From: $emailFrom" );
print "Your email has been sent, click xxxxx (insert html link) to return to the main page";
?>
This is all that is needed to easily send an email from your webform. If you get any errors, you should contact your hosting provider and ensure that you have permissions to run PHP scripts.
Please leave a comment if you have any questions.
PrintShare it! — Rate it: up down flag this hub
Comments
So the web server is the one who is sending out the email? so the web server is the "email client"? cause I am confuse who is sending out the email from the form. In normal case if u send an email out...the email client talks to your email provider's smtp server, then the smtp server to the receiptent's SMTP server....then POP then the receipent receive the email.....how about here?
ascdacad
sorry
The webserver is in fact sending out the email, it is not a simple "mailto:" tag which would open up the end users email client on their desktop.
This is not ideal, as a lot of people use webmail and this would invoke a call to open a "default" client which is likely not even used.
This uses sendmail from the webserver (which should be setup for you already), and will relay to the default mail server, either itself or another mail server.
srgeqrg
adsd
HI..
Hi,
using your sendMail.php I get the message: Parse error: syntax error, unexpected T_VARIABLE in sendMail.php
Is it supposed to be "From: $emailFrom" or just "From: $email" ?
Thanks
asda









Gregory says:
16 months ago
This is great stuff, im gonna try it soon.