1. Computing

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

Related Video
Create Email Signatures in Outlook Express
Create Email Aliases and Forwarding Addresses in Gmail
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Troubleshooting
  5. PHP Email Form - Email Form Script - PHP Send E-mail

©2013 About.com. All rights reserved.