The Problem with Putting an E-Mail Address On Your Site

A contact us page is a useful thing to have. Many sites just provide an e-mail address for contact purposes. However, there are several problems with e-mail:

  • If you leave your e-mail address on a website it almost certainly will get picked up by spammers and soon you’ll be getting a flood of unsolicited spam with viagra and cheesy pharmacy ads, virus attachments and the like. Hopefully you’ll have some sort of spam blocker but as always the best defense is prevention.
  • When a visitor clicks on your e-mail address, they may or may not launch the appropriate e-mail program. E-Mail hot links only work if you have a client based e-mail program (like MS-Outlook) which works seemlessly with e-mail links. Many times, people prefer to use web based e-mail rather than Outlook and the click links won’t work with web-mail.
  • If your browsing the web, its often quicker to enter a message into a browser page than to go to a seperate program or browser page. It’s more seemless for the end-user.
  • With a contact us page, you can control where the message goes and what information gets collected. For example, you may want additional marketing data like how they heard about your site, or if its a support question you may want to gather certain pre-requisites like the version of software they are using, etc.
  • You may want to log messages into a database, which is much harder to do with e-mail.

Creating the Contact Us Page

The following HTML and PHP script allows you to just that (except the database part. If your really interested in DB let me know and I’ll write a second part to this article).

Here is the HTML part, which is the data collection form. Place the following code in the <body> portion of your contact us page.

<form method=post action="sendmessage.php">
  <p>If you would like to contact us, please fill out the form below and press "Send". Enter your message in the space provided below:</p>
<table align="center" border="1" bordercolor="#CCCCCC" cellpadding="10">
<tr><td>
<b>Message:</b>
  <dl>
    <dd>    <p>Message Type: <select name="MessageType" size="1">
    <option selected>- - - - - -</option>
    <option>Suggestion</option>
    <option>Bug Report</option>
    <option>Question</option>
    <option>Advertising Request</option>
    <option>Link Exchange</option>
    <option>Other</option>
    </select></p>
    <p>Message:<br /><textarea name="Message" rows="5" cols="60"></textarea></p>
  </dd>
</dl>
  <p><strong>Tell us how to get in touch with you:</strong><strong></strong></p><p>
  <dl>
    <dd>
    <table>
      <tr>
        <td>Name</td>
        <td>
 <input type="text" size="35" maxlength="256" name="UserName"/></td>
      </tr>
      <tr>
        <td>E-mail</td>
        <td>
        <input type="text" size="35" maxlength="256" name="UserEmail"/></td>
      </tr>
      <tr>
        <td>Phone</td>
        <td>
        <input type="text" size="35" maxlength="256" name="UserPhone"/></td>
      </tr>
      </table>
    </dd>
  </dl>
  <dl>
    <dd>
      <input type="checkbox" name="ContactRequested" value="ContactRequested"/> Please contact me regarding this matter.</dd>
  </dl>
  </p>
 
  <p align="right"><input type="submit" value="Send"/>   <input type="reset" value="Clear Form"/></p>
</td></tr></table></form>

Now you’ll need to create need to create a PHP page that processes the message (e.g. sendmessage.php) as follows. Again, place this code in the <body section of your page:

<?php
    /* Initialize Variables */ 
    $myemail = "webmaster@mysite.com"; 
    $homepage = "http://www.mysite.com"
?>
<p>Thank you <?php echo $UserName ?> for your comments: <br>
<?php echo $Message ?> 
<br>
<br>
<?php 
    if (isset($ContactRequested)) { 
        echo "You have requested a follow up contact with the following contact information:<br>";
        echo "e-Mail: $UserEmail";
        echo "<br>Telephone: $UserPhone <br>";
    }
?>
<br> 
<a href= <?php echo $homepage ?> title="Web MYSITE Home Page"> 
    Return to MYSITE Home Page</a><br>

<?php
   $ip = getenv("REMOTE_ADDR");
   $todayis = gmdate("l, F j, Y, g:i a") ;
   $Comments = stripcslashes($Comments);
   $messageis = 
       "Time = $todayis [GMT] \n" .
       "Message From = $UserName \n" .
       "Type = $MessageType \n" .    
       "e-Mail = $UserEmail \n" .
       "Telephone = $UserPhone \n" .
       "ContactRequested = $ContactRequested \n" .
       "Message = $Message";

   $messageme = $ip . " " . $messageis;
   mail($myemail, "Comments from $UserName", $messageme);
?>
</p>

For a demonstration, you can see how this code works on our own contact us page.

PS. If you like this code sample, please link to our site (http://www.webmastersloom.com). It will be much appreciated.

PPS. Don’t forget to paste the code into a plain text editor (e.g. notepad) before placing it into your web page otherwise you’ll pick up the extra HTML from this page.