Saturday 16 May 2009

Create a contact page in PHP

PHPIf you have a web site, you probably want to allow visitors to contact you. But without reveal your electronic address.

Of course, it can easily fall in the robots hands which scan the web to make listings. Then your box would be full of spams!

The solution is a contact page, where people can send a message to you without seeing your e-mail directly.

~

How to send a e-mail with PHP

We must create a very simple form with a field for the visitor's e-mail, to answer him, and a text area. Let's start on a already "dressed" page with your site theme: contact.php. The single form is created with this HTML code:

<form method="post" action="contact.php">
<p>
 <label for="email">Your e-mail:</label>
 <input type="text" id="email" name="email" />
 <br /><label for="message">Your message:</label>
 <textarea id="message" name="message"></textarea>
 <br /><button type="submit">Send this message</button>
</p>
</form>

~

Then, we must receive the result in PHP. It's better to place the PHP code before the form, the message indicating the sent will be more visible. We can even don't print the form again if the message is correctly sent, but let's stay simple. ;-)

The mail function requires 3 or 4 parameters:

  • the e-mail recipient,
  • the subject,
  • the message,
  • and optionally some headers.

Here, I will indicate headers with you visitor's e-mail. This will allow you to answer him only by clicking the "answer" button of your e-mail application. And also an UTF-8 encoding, this depends on your site, but UTF-8 is recommended.

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  if (empty ($_POST['email']) || empty ($_POST['message']))
    echo '<p>Error, you must type your e-mail and a message!</p>';
  else
    if (@mail ('me@example.net', 'Contact from you site', $_POST['message'],
    "From: $_POST[email]\nContent-Type: text/plain; charset=utf-8"))
      echo '<p>Your message has been sent. Thanks.</p>';
    else
      echo '<p>Your message can not be sent. Please try again later.</p>';
}
?>

In short, if the fields are not empty, we sent the e-mail. If it goes, everything is OK, else PHP does not give a really clear information about the origin of the error. :-| The recipient is yourself, so the adress is probably correct. Of course, you must replace me@example.net by your own e-mail address! But this stay totally invisible to the web-surfer because the server writes to you and not directly the person.

Note: if your server is configured with magic_quotes_gpc to on, PHP will add antislashes \ before every apostrophes '. You'll have to use the function stripslashes before sending. ;-)

~

You can ask in the comments if you would like a following to this article. For example

  • make a captcha (text or image) to prevent the bots (again) to validate this form,
  • or how to verify the e-mail validity in Javascript and/or PHP...


cafĂ© Did this article help you? 
Buy me a coffee!

Leave a comment (all comments are moderated, don't waste time with spam)

Azure Dev