Chapter 12: Sending Email |
|
|
This is extremely simple because PHP's mail() function hides the code required to locate the mail program and feed all the parts of a message to it. Here's what the textbook description of the mail() function looks like: boolean mail(string recipient, string subject, string body, string additional_headers); 'Boolean' means that the function does its job and returns either TRUE(1) or FALSE(0), meaning that it succeeded or failed. 'String' means that an argument should be a string. The last argument is technically optional. You can generate email from inside a script in lots of ways. It can be in plain text, HTML or both. It can also include attachments. The FormMailer script included with this book does all of that. So there's not much point in covering the technical details here. Just use that script; you don't really need to know how it works. |
|
Here's a little example of sending a plain text message from a script: <?php There are a lot of things you can change in $additional_headers to send various kinds of messages. For instance, if you want to send an HTML-formatted message you'd change the Content-Type to be text/html. Sending an email message from inside a script is a simple way to deliver an automatic response when someone fills out a form on your site. Other changes to the header information let you send messages with more than one kind of content (both text and HTML, for example), attach files, and more. You can wrap all the basic code for sending a message into a function and pass things to it. For instance, you could put the bodies of several messages into separate files and just pass the name of one file to the function as $body when you call your function. Inside your function, your message body would be passed to the mail() function. The same applies to $recipient and $name. If you pass them in and let your function pass it to mail(), your function could send any of your messages to anyone. That would look like this: <?php Look at the second line of the function. Do you see what's been done here? If there is no "to" email address stored in $recipient, we have no idea where to send the message. So I've just told the function to stop right there. You can't snail-mail a letter without an address. Likewise, the Internet can't send email without one. |
| Previous Page Table of Contents Next Page |
Copyright © 2004 Steve Humphrey |