Using the MAIL function

The Mail () Function

Many websites offer a way for you to send them an email from a simple form on their site. Providing the form as opposed to simply listing your email address not only looks nicer but also serves two purposes.
  1. First, the form lets the website owner decide what information it is important to collect and prompts the users to fill in the answers to each of their questions. This way the user doesn’t forget to include important information.
  2. Second, if you list your email directly on your site it can be picked up by bots designed to ‘farm’ email addresses. What that means for you is SPAM. Nobody likes to have their inbox flooded with SPAM, and using a form can help prevent that.

The mail function is phrased as: mail (to, subject, body, headers)

An example is:

mail ( "", "Contact Us Form", "This is an email from your site", "From: you@yoursite.

Creating the Form

<form method="post" action="contact.php">  Email: <input name="email" type="text"><br> Message:<br>  <textarea name="message" rows="15" cols="40"></textarea><br>  <input type="submit">  </form>

The above form will collect the user’s email and message, and then pass them to the page contact.php

Sending the Mail

<?php  $to = "";  $subject = "Contact Us";  $email = $_REQUEST['email'] ;  $message = $_REQUEST['message'] ;  $headers = "From: $email";  $sent = mail($to, $subject, $message, $headers) ;  if($sent)  {print "Your mail was sent successfully"; } else  {print "We encountered an error sending your mail"; } ?>

Save the above code as contact.php

What this code does is:

  1. Define who you want the mail sent to. We have used but you should replace this with your email address.
  2. Define the default subject for your mail.
  3. Collect the message and email fields from the form and assign them to variables
  4. Create a ‘from’ email header
  5. Send the email

While PHP contact scripts can vary in complexity and features, they all use this mail function as their base. We suggest you follow up by reading our tutorial on creating a more detailed contact form



Not receiving your mail? Here are a few common problems to check when working with the mail () function:

  1. Does your account have SPAM filters?
    If it does, check your junk mail folder (or trash, or whatever your particular program calls it.) It may have been received and filtered out. Fortunately most programs allow you to add it to a list of safe senders so it won’t be filtered out in the future.
  2. Are you sending to a legal address?
    Depending on how your host has things configured, sometimes you can’t send a form mail to if your form is on You may need to setup an email that corresponds to the site you are sending from, or see if your host allows you to add your other email address as an allowed recipient. Since all hosts are different, the best thing you can do is contact them.
  3. Do you need authentication?
    When you send email from your mail program, do you need to provide a password for outgoing mail (outgoing mail authentication?). If you are unsure because you set up your mail a long time ago, the best thing to do is ask your host. If you do need outgoing authentication, the mail () function won’t work. Fortunately, there are still ways to send mail from your website with PHP.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s