PHP
  Home arrow PHP arrow Page 4 - Getting Intimate With PHP's Mail() Fun...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Getting Intimate With PHP's Mail() Function
By: Steve Knoblock
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 47
    2003-01-02

    Table of Contents:
  • Getting Intimate With PHP's Mail() Function
  • The Mail() Function Explained
  • Talking to Sendmail
  • Getting Mail From a Form
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Getting Intimate With PHP's Mail() Function - Getting Mail From a Form


    (Page 4 of 5 )

    Of course, any of these headers or the message body can be specified using variables. This is a full treatment of the previous script, expanding it into a simple form handler. It introduces variables and divides the code into a small configuration section and the familiar sendmail script.

    The first section of PHP code serves as brief configuration area for setting up the values of key variables. It is helpful to store the path to your mail program in a variable so you don't have to hunt through code when it comes time to change it.

    The second section of PHP codes does the work of sending the mail. In true PHP fashion, it intermingles HTML and PHP code to conditionally prompt the user to fill out the form or mail information collected by the form to the site administrator.

    The PHP code will include the email address of the person submitting the form in the "From:" field, which makes it easy for the person receiving the mail to reply to the submitter - they just have to hit 'Reply' in Eudora or their favorite email application. Following the Internet convention, the "From:" field is formatted by placing the email address in angled brackets, such as:

    From: $frmName <$frmEmail>\n

    Each form input has a variable name. When the form is submitted, PHP automatically creates variables using the names given in the NAME attribute in the form for each input using the values entered by the user. These variables are available from within the PHP script. For each variable a line is added to the sendmail output printing that value in the message. 

    I am using PHP 4.1 with register_globals turned on. You may need to explicitly call form variables through the $_FORM array if you're running PHP 4.1 or above.

    <?php

    /* Configuration -----------------------------------------*/

    /* Mail results to this address */
    /* Set this to the email address you wish to receive mail */
    /* from the form submissions at. */
    $TO = "YourEmailId@YourISPDomain.SomeTopLevelDomain";

    /* Specify system mail program */
    /* Set this to the path to your mail program. Check with */
    /* your server administrator for the proper location. */
    $MP = "/usr/sbin/sendmail -t";

    ?>

    <?php

    /*-------------------------------------------------------*/
    /* Decide if we should display a new form or send the */
    /* form data by email. */
    /* To make this decision, the script can check for the */
    /* existence of 1) the action variable defined by a */
    /* hidden field; 2) a required form field that you know */
    /* will always be set on submission; or you may set the */
    /* action variable to a particular value that can be */
    /* checked to determine the action to take. I chose */
    /* to simply check for the existence of the hidden */
    /* action variable (which is always set as long as we */
    /* give a value in the hidden field). */
    /*-------------------------------------------------------*/

    if ($frmAction)
    {

    /*-------------------------------------------------------*/
    /* A thank you message (or other response) goes here. We */
    /* switch to HTML mode to make it easy to include any */
    /* tags you wish without worrying about quoted */
    /* attributes. */
    /*-------------------------------------------------------*/

    ?>

    <div align="center">
    <table width="350" border="2">
    <tr>
    <td bgcolor="#C0C0C0">
    <p>Thank you for requesting a personalized quote for our products and services.
    </p>
    </td>
    </tr>
    </table>
    </div>



    <?php

    /*-------------------------------------------------------*/
    /* The real work gets done here by opening a pipe to */
    /* sendmail, which sends the contents of the submitted */
    /* form by email to the address specified in the */
    /* configuation section (which can acutally be an */
    /* an included initialization file if you want to get */
    /* fancy). For each variable we expect the form to */
    /* to submit, we output as part of the email. */
    /*-------------------------------------------------------*/


    $fd = popen($MP,"w");
    fputs($fd, "To: $TO\n");
    fputs($fd, "From: $frmName <$frmEmail>\n");
    fputs($fd, "Subject: Message from your web site\n");
    fputs($fd, "X-Mailer: PHP3\n");
    fputs($fd, "Name: $frmName\n");
    fputs($fd, "Phone: $frmPhone\n");
    fputs($fd, "Fax: $frmFax\n");
    fputs($fd, "Email: $frmEmail\n");
    fputs($fd, "Address: $frmAddress\n");
    fputs($fd, "Price range: $frmPriceRange\n");
    fputs($fd, "Details: $frmFurther");
    pclose($fd);

    /*-------------------------------------------------------*/
    /* Here the script must exit so we don't display the */
    /* form again once the thank you message has been */
    /* displayed and the mail sent. */
    /*-------------------------------------------------------*/

    exit;

    } else {
    // start else clause

    ?>

    <div align="center">
    <form action="mail.php3" method="post">
    <table>
    <tr><td colspan="2">
    <p>Please enter your information for a personal quote.
    </p>
    </td>
    </tr>
    <tr>
    <td>Name:</td>
    <td><input type="text" name="frmName" size="24">
    </td>
    </tr>
    <tr>
    <td>Phone:</td>
    <td><input type="text" name="frmPhone" size="24">
    </td>
    </tr>
    <tr>
    <td>Fax:
    </td>
    <td><input type="text" name="frmFax" size="24">
    </td>
    </tr>
    <tr>
    <td>Email:
    </td>
    <td> <input type="text" name="frmEmail" size="24"><br>
    </td>
    </tr>
    <tr>
    <td>Address:
    </td>
    <td><input type="text" name="frmAddress" size="24">
    </td>
    </tr>
    <tr>
    <td>Price Range:
    </td>
    <td><input type="text" name="frmPriceRange" size="24">
    </td>
    </tr>
    <tr>
    <td>Details:
    </td>
    <td><input type="text" name="frmFurther" size="24">
    </td>
    </tr>
    <tr>
    <td>
    <!-- To determine whether the script should display the form or mail the data, you can check for existence of a required field or this special action variable. -->
    <input type="hidden" name="frmAction" value="formmail">
    <input type="submit" value="Submit">
    </td>
    <td>&nbsp;
    </td>
    </table>
    </div>
    <br>

    </form>

    <?php
    } // end else clause
    ?>


    This script is an example of how a web script can be used to generate a HTML form which can then be used to send mail. This is possible because the first time that we go through the script, we send the HTML for the form to the client's browser. The script then sits idle on the server until we call it again to accept the form values. The HTML form's action parameter is set to call the same script that generated it. The script looks at the values in its environment to see if it has been called by the same form. The presence of a key value required in the form may be used to signify form submission, or a special "action" value can be specified in a hidden input field in the form.

    Of course, this simple example is just to teach you how to setup a form that mails data back to you. Unless you're making a quick form for a small project, you would not want to go through the process of writing the code to print out each variable. It's easy to spell a variable name incorrectly so the input does not show up in the output.

    What is needed is a general form handling script that accepts any number of inputs without worrying about how many inputs there are or what they are called. Well, that just about takes the script as far as possible without writing a generalized form mail handler.

    More PHP Articles
    More By Steve Knoblock


       · Very interesting article, however I was really hoping it would cover how to deal...
     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT