Making a Contact Page

Articles > Webmaster > Making a Contact Page

There was once a time that if you wanted people to contact you you would simply put your email address on your website:

To learn more send email to somebody@somewhere.com.

 

Unfortunately, this approach is no longer a good idea.

The Problem

There are two problems with directly embedding your email address. First off, not only will the people you want to be in touch with learn your email address, so will lots of people you do not want to talk to. There are companies that send robots1 out over the internet to harvest email addresses and then sell those addresses to spammers. If your email address is on your site then you can expect to get a lot of spam.

The other problem is that the email link will only work if the visitor is at home and uses a standard email client (Outlook Express, Eudora, Mail, etc.) to read their email. If they are in an internet cafe or use a web-based email program (Gmail, Hotmail, etc.) then clicking on the mail link will simply give them an error message, not a new email message.

The Solution

So what can you do? The simplest approach is to make a form2 on your web page, and have your web server send the email when the form is submitted. In the simplest case your HTML would look like this:

<form method="post" action="submit.php">
  Email: <input name="sender" type="text"/><br />
  Subject: <input name="subject" type="text"  /><br>
  <textarea name="message" cols="30" rows="5"></textarea>
  <input name="submit" type="submit"  />
</form>

which produces a very simple form:


Email:
Subject:


Note that the form action is submit.php, not submit.html. PHP is an extension to HTML that allows the web server do do things with the web page (like send an email) before giving it to the client browser.3 Your submit.php file should send the email and thank the visitor:

...

<?php
  $from = $_POST["sender"];
  $to = "yourname@yoursite.com";
  $subject = $_POST["subject"];
  $body = $_POST["message"];
  mail($to, $subject, $body, "From:$from");
?>

Thanks for your message!

...

What Happened?

When the visitor clicks the submit button your submit.php file is requested from the web server. Before sending the page to the client, the web server looks for any code appearing between <?php and ?> and executes it.

The code fetches the form values from the special _POST array and stores them in temporary variables (from, to, subject, and body). Then it calls the built-in function mail() with the variables passed as parameters, which send you an email message.

Testing

Note that this only works if an http server is involved — you cannot test this using the "Preview with Firefox" feature of Adobe DreamWeaver because that uses the file:// protocol and not the http:// protocol. The simplest way to test the code is to publish it to your web server on a temporary page and, once you know it is working, move the code to your final contact page.

Notes

  1. Robots are programs that act like web surfers — following links and reading the pages that they find. Not all robots are malicious; Google uses robots to index your website so that it will show up when people perform a search. Return to text.
  2. Don't know what a form is? You can find out at W3 Schools. Return to text.
  3. PHP is a big topic. The best place to start is the official PHP website. Return to text.