1. Home
  2. Computing & Technology
  3. PHP / MySQL

Using the MAIL function

By Angela Bradley, About.com

3 of 4

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

3 of 4

Index: Using the MAIL function

  1. The Mail () Function
  2. Creating the Form
  3. Sending the Mail
  4. Troubleshooting

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. PHP Email Form - Email Form Script - PHP Send E-mail

©2008 About.com, a part of The New York Times Company.

All rights reserved.