13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating a Sample Application: Smart Form Mail<br />

109<br />

Listing 4.1<br />

Continued<br />

//invoke mail() function to send mail<br />

mail($toaddress, $subject, $mailcontent, $fromaddress);<br />

?><br />

<br />

<br />

Bob's Auto Parts - Feedback Submitted<br />

<br />

<br />

Feedback submitted<br />

Your feedback has been sent.<br />

<br />

<br />

Generally, you should check that users have filled out all the required form fields using,<br />

for example, isset().We have omitted this function call from the script <strong>and</strong> other<br />

examples for the sake of brevity.<br />

In this script, you can see that we have concatenated the form fields together <strong>and</strong><br />

used <strong>PHP</strong>’s mail() function to email them to feedback@example.com.This is a sample<br />

email address. If you want to test the code in this chapter, substitute your own email<br />

address here. Because we haven’t yet used mail(), we need to discuss how it works.<br />

Unsurprisingly, this function sends email.The prototype for mail() looks like this:<br />

bool mail(string to, string subject, string message,<br />

string [additional_headers [, string additional_parameters]]);<br />

The first three parameters are compulsory <strong>and</strong> represent the address to send email to, the<br />

subject line, <strong>and</strong> the message contents, respectively.The fourth parameter can be used to<br />

send any additional valid email headers.Valid email headers are described in the document<br />

RFC822, which is available online if you want more details. (RFCs, or Requests<br />

for Comment, are the source of many Internet st<strong>and</strong>ards; we discuss them in Chapter 20,<br />

“Using Network <strong>and</strong> Protocol Functions.”) Here, the fourth parameter adds a From:<br />

address for the mail.You can also use it to add Reply-To: <strong>and</strong> Cc: fields, among others.<br />

If you want more than one additional header, just separate them by using newlines <strong>and</strong><br />

carriage returns (\n\r) within the string, as follows:<br />

$additional_headers=”From: webserver@example.com\r\n “<br />

.’Reply-To: bob@example.com";<br />

The optional fifth parameter can be used to pass a parameter to whatever program you<br />

have configured to send mail.<br />

To use the mail() function, set up your <strong>PHP</strong> installation to point at your mailsending<br />

program. If the script doesn’t work for you in its current form, an installation<br />

issue might be at fault, check Appendix A,“Installing <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong>.”<br />

Throughout this chapter, you enhance this basic script by making use of <strong>PHP</strong>’s string<br />

h<strong>and</strong>ling <strong>and</strong> regular expression functions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!