Jul
1st

How to Send E-mail from a Web Form

Files under HTML, Website Starting | Posted by Brian

I can’t tell you how many times I’ve been asked about how to do this.  It makes sense though.  Designers often have small clients that just want a simple web form, but don’t have a database to store the data in.  How do you get information from the website visitors to your client?  The simplest way is probably e-mail.

It’s been said that Dreamweaver has a way to do this, but no one has been able to tell me what that way is.  Frankly, I’m a developer so I don’t really care about what Dreamweaver has or doesn’t have, I just want it to work.  Turns out, there’s a really easy script that you can add to your site (or your client’s) that will allow visitors to fill out a form and then have that data e-mailed to the appropriate recipient.

First, you’ll need to setup a form on a page called userinfo.html.  This should be fairly easy to do whether you’re creating the site freehand or with web editing software.  When you name each element, that’s how our script will label them in the e-mail, so be sure to label them clearly.  Dreamweaver seems to like to label things a37_infrared, so let’s just make sure they’re human readable names.  For example, to create a field to receive the users first name, it should look something like:

<input name="firstname" type="text" />

In the e-mail, this will output something like “Firstname: Joe”.  That should be close enough for our purposes.  Once you’ve finished the form, just set the action to the PHP script we’re about to create.  That makes our form tag (in userinfo.html) look something like this:

<form name="userform" action="processform.php" method="POST">

Now, for the part you’ve been waiting for.  Make processform.php (this is a new page and separate from userinfo.html) a nice HTML page that let’s the user know that you have received their information.  Again, do this in any way that is easy for you to create your HTML pages.

For this to work, it does require that the server can run PHP.  The server also needs the ability to send e-mail, but most servers do both of these just fine.  It shouldn’t be a problem for most people, but it seemed worth mentioning.

Now, at the very top of the processform.php add the following lines:

<?php
$body = “”;
$subject = “You received a new request from example.com”;
foreach ($_POST as $key => $value) $body .= ucwords($key) . “: $value\n”;
mail (‘myname@example.com‘, $subject, $body);
?>

You can now replace the text after “$subject” with the subject line of the e-mail you would like to receive.  Then replace “myname@example.com” with your e-mail address (or obviously that of your client’s).  Now every time they fill out your form it will send you e-mail with their details.

Not bad for 4 lines of PHP, eh?

  • Share/Save/Bookmark

Leave a Reply




<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>