Flag This Hub

Functional Form To Send Email From Web Page

By


Mail 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.

Comments

Gregory 3 years ago

This is great stuff, im gonna try it soon.

AL 3 years ago

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?

wae 3 years ago

ascdacad

wae 3 years ago

sorry

Alex 3 years ago

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.

segfse 3 years ago

srgeqrg

sdasd 2 years ago

adsd

JOROCK 2 years ago

HI..

Eva Eskilsson 2 years ago

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

sdas 2 years ago

asda

ind 2 years ago

does not show html that good

ind 2 years ago

does not show html that good

Paul 2 years ago

"using your sendMail.php I get the message: Parse error: syntax error, unexpected T_VARIABLE in sendMail.php"

I get the same issue what was the solution please

ruckus 23 months ago

hey good one

Dave Forsyth 22 months ago

I found the solution to the syntax error. There needs to be a semi-colon after the

Rauza 21 months ago

I get the same error, where should we put semicolon?

r345345 20 months ago

534534534

ooo 18 months ago

kllll

dark angel 16 months ago

what about smtp??

ciofi 15 months ago

Eva Eskilsson you're right.The correct line code is: mail( "yourEmail@yourDomain.com", "Subject of email, change this to whatever you want", $message, "From: $email);

And... in php file, do not include first line ( )

The variables 'message','emailFrom' must be defined in your HTML form with method "GET" .

asdad 14 months ago

sssssssssssss

ha ha 13 months ago

haha

rf 3 months ago

eetgdvbgdsvgsvbs fv d dx fdb dv fw xwr wvgwef x

Michelle Lee 3 months ago

Great post. Here’s a tool that lets you build your custom web form in minutes – without coding. Just point-and-click

http://www.caspio.com/online-database/features/web

Nick Lachey 2 months ago

Fantastic post. Here’s a tool that lets you build all types of web forms with email alerts fast and without coding. Just point and click http://www.caspio.com/online-database/features/web

GauravGupta 8 days ago

I haosted a website and i want to send an email from my website name to any one what should i do?

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working