The University of Texas at Austin- What Starts Here Changes the World
Services Navigation

Example 7 - Sending E-mail

Previous examples illustrated how to read form data and write it to a file. Another action that you might want your PHP script to perform is send an e-mail message with the form information. This is actually very straightforward using PHP's mail function.

PHP's built in mail function relies on the server's mail system to send the message. The syntax of the mail function is:


where to is the e-mail address of where you want the message sent, subject is the subject line of the message, and message is the actual body of of the message. For example:

mail("", $_POST[subject], $_POST[message]);

sends the contents of the $_POST[message] variable to with a subject of $_POST[subject].

<TITLE> Example 7 </TITLE>


// the code below checks the $subject field from a Web form
// if the $subject is not blank the message is sent
// else (the subject field is blank) an error is sent back to the user

if ($_POST[subject] <> "") {

    mail("", $_POST[subject], $_POST[message]);

// don't forget to have your php script print something
// if you just send mail the users will see an empty page

    print("Your message has been sent\n");

} else {
     print("The subject field was blank.  You must complete the subject field");



The mail function can easily be added to the previous examples so form input is saved to a file and e-mail notification is sent.

Test Example 7 

View Source Code

PHP Manual:


  Updated 2006 August 11
  Comments to