1. About.com
  2. Computing & Technology
  3. PHP / MySQL

Discuss in my forum

Using the MAIL function

By , About.com Guide

Sending the Mail
 <?php $to = "you@yoursite.com"; $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 you@yoursite.com 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

©2012 About.com. All rights reserved. 

A part of The New York Times Company.