PHP and E-mail
Main | Review of Operators | Mathematical Functions | Practice 5 | Solution 5 | HTML Forms | PHP Forms | Practice 6 | Solution 6 | PHP and E-mail | Practice 7 | Solution 7 PHP has a very useful built-in function called mail(), which sends email. This function takes several arguments and the easiest way to understand it is to look at an example of it in use. mail($receiver, $subject, $message, "From: $sender");
PHP's mail() function can also return a variable that can be true or false, with regard to whether the mail was sent. This can be a useful feature which you can use to echo information to your user, as to the status of their email. The following shows how one could take advantage of this feature: $sent = mail($receiver, $subject, $message, "From: $sender"); if ($sent) { echo "your message was sent"; } else { echo "your message was not sent"; } In the above $sent will only be true if the mail was sent. If the value of $sent is true the program will tell the user that the mail has been sent. The else statement covers the case where the value of $sent is false, and tells the user that the mail was not sent. It should now be possible for you to write a web-based email application, that uses HTML's ability to get text from users (as illustrated by Practice 5), and PHP's mail() function. You could also use these tools to create online forms, which would allow you to collect information from users, that could then be e-mailed to whomever you wish.
jfulton [at] member.fsf.org
|