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

Using the MAIL function

By , About.com Guide

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

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.