Home arrow PHP arrow Page 4 - Getting Intimate With PHP's Mail() Function

Getting Intimate With PHP's Mail() Function

Interested in sending advanced email using PHP's mail() function, or even by piping output directly to sendmail? If so, make sure you take a look at Steve's comprehensive tutorial on sending email with PHP.

Author Info:
By: Steve Knoblock
Rating: 4 stars4 stars4 stars4 stars4 stars / 70
January 02, 2003
  1. · Getting Intimate With PHP's Mail() Function
  2. · The Mail() Function Explained
  3. · Talking to Sendmail
  4. · Getting Mail From a Form
  5. · Conclusion

print this article

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.


/* 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";



/* 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">
<td bgcolor="#C0C0C0">
<p>Thank you for requesting a personalized quote for our products and services.


/* 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");

/* 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. */


} else {
// start else clause


<div align="center">
<form action="mail.php3" method="post">
<tr><td colspan="2">
<p>Please enter your information for a personal quote.
<td><input type="text" name="frmName" size="24">
<td><input type="text" name="frmPhone" size="24">
<td><input type="text" name="frmFax" size="24">
<td> <input type="text" name="frmEmail" size="24"><br>
<td><input type="text" name="frmAddress" size="24">
<td>Price Range:
<td><input type="text" name="frmPriceRange" size="24">
<td><input type="text" name="frmFurther" size="24">
<!-- 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">


} // 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.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials