Specifications
Unsurprisingly, this function sends email. The prototype for mail() looks like this:
bool mail(string to, string subject, string message,
string [additional_headers]);
The first three parameters are compulsory and represent the address to send email to, the sub-
ject line, and the message contents, respectively. The fourth parameter can be used to send any
additional valid email headers. Valid email headers are described in the document RFC822,
which is available online if you want more details. (RFCs or Requests For Comment are the
source of many Internet standards—we will discuss them in Chapter 17, “Using Network and
Protocol Functions.”) Here we’ve used the fourth parameter to add a “From:” address for the
mail. You can also use to it add “Reply-To:” and “Cc:” fields, among others. If you want
more than one additional header, just separate them by newlines (\n) within the string, as fol-
lows:
$additional_headers=”From: webserver@bobsdomain.com\n”
.”Reply-To: bob@bobsdomain.com”;
In order to use the email() function, set up your PHP installation to point at your mail-sending
program. If the script doesn’t work for you in its current form, double-check Appendix A,
“Installing PHP 4 and MySQL.”
Through this chapter, we’ll enhance this basic script by making use of PHP’s string handling
and regular expression functions.
Formatting Strings
You ’ll often need to tidy up user strings (typically from an HTML form interface) before you
can use them.
Trimming Strings: chop(), ltrim(), and trim()
The first step in tidying up is to trim any excess whitespace from the string. Although this is
never compulsory, it can be useful if you are going to store the string in a file or database, or if
you’re going to compare it to other strings.
PHP provides three useful functions for this purpose. We’ll use the trim() function to tidy up
our input data as follows:
$name=trim($name);
$email=trim($email);
$feedback=trim($feedback);
The trim() function strips whitespace from the start and end of a string, and returns the result-
ing string. The characters it strips are newlines and carriage returns (\n and \r), horizontal and
vertical tabs (\t and \v), end of string characters (\0), and spaces.
Using PHP
P
ART I
96
06 7842 CH04 3/6/01 3:41 PM Page 96