Penn State

Web Conference 2005

Writing PHP for ITS/ASET Web services

Email Submission Form

<- Back - The POST Method|Up |Saving Data on the Server - Next ->

Sending Email From PHP

  1. Next we will build a form that will send an email upon submission. Create a new file with the following content:

    <html>
    <body>

    <?php
    // Set the following variable to your email address:
    $my_email 'xyz123@psu.edu';

    if( isset(
    $_REQUEST['subject']) && isset($_REQUEST['message']) ){
        if( 
    mail$my_email,
              
    "Email form: " htmlspecialchars($_REQUEST['subject']),
              
    htmlspecialchars($_REQUEST['message']),
              
    "From: Email Submission Form <$my_email>\r\n" .
              
    "X-Mailform-Submitted-By-IP: " $_SERVER['REMOTE_ADDR'] . "\r\n" )){
            echo 
    "<p>Email sent successfully.</p>\n";
        }else{
            echo 
    "<p>There was an error attempting to send mail.</p>\n";
        }
    }else{

    ?>
    <form action="mailform.php" method="POST">
    <p>Send me an email.</p>
    <p>Subject: <input type="text" name="subject"></p>
    <p>Body:</p>
    <textarea cols=70 rows=5 name="message">
    </textarea>                          
    <p>
    <input type="reset"><input type="submit">
    </p>
    </form>
    <?php

    }
    ?>

    </body>
    </html>
  2. Before saving, go back and change the value of $my_email, xyz123@psu.edu to be your email address.

  3. Save the file as mailform.php in your space.

  4. Next, view mailform.php in a browser. It should look like this:

    Send me an email.

    Subject:

    Body:

About the form code

<- Back - The POST Method|Up |Saving Data on the Server - Next ->

If you have any questions, feel free to ask me.

Content by: Jeff D'Angelo <jcd@psu.edu> © 2005

See the source for this page

Last update on: Mon Jun 29, 2009, 12:14:51 AM