PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Guts or Glory Poker PHP - A casino-style card game written entirely in PHP


[back]
WB01624_.gif (281 bytes) 12/28/99 "Sending email with PHP" WB01626_.gif (272 bytes)[next]

Using PHP to send mail

Let's get interactive with surfers again. I'd like to create a form mail system in PHP. First we need to create a form in HTML to gather the information. I am going to use one similar to my Perl form mailer script, because it looks clean.

Send your diary entry "future topic requests" to TDavid

    From: enter your email: myemail@myisp.com
Subject: enter the subject of your email

I would like to be notified when you release freeware or commercial php scripts


Enter your message body above

The important thing, remember, when taking input in PHP is the field names for the information you are gathering. "From" will be converted into a variable with the same name by php (it would become $from), the "subject" will be made into $subject and the "contents" of the message will be made into $contents. For submitting the actual form, we will activate the php script by using the form action tag and linking to send_mail.php3:

<form action="send_email.php3" method="POST">

The $to variable will be defined in the send_mail.php3 script to point to my email address (you'll see the php code momentarily), of course. I also prefill the subject with the words "diary entry suggestion" which visitors can change to something else, if they want:

<input type="text" size="22" name="subject" value="diary entry suggestion">

Ok, let's take a look at the code to mail the contents of the above form to me and if the process is successful it will redirect you right back to this page. In order for this code to work you will need to know the path to sendmail on your server.

<?
$to = "webmaster@php-scripts.com";
$from_header = "From: $from";

if($contents != "")
{
   //send mail - $subject & $contents come from surfer input
   mail($to, $subject, $contents, $from_header);
   // redirect back to url visitor came from
   header("Location: $HTTP_REFERER");
}
  else
{
   print("<HTML><BODY>Error, no comments were submitted!");
   print("</BODY></HTML>");
}
?>

Notes: I make sure there are some comments submitted or else show an error message by using the != (not equal) with an if statement. When you want to redirect the browser using the header function (like when using the setcookie function) you must do it before any HTML. With the header and $HTTP_REFERER in the code above I am simply sending people who submit the form back where they came from, which should will be this page that calls the form. I could have easily changed the header line to read:

header("Location: http://www.php-scripts.com/thankyou.html");

And at a page named thankyou.html I could have thanked them for submitting the form.  I could have also added HTML directly to the send_email.php3 script.

Building an opt-in email list?

Bet some of you are wondering about the checkbox which indicates that "I would like to be notified when you release freeware or commercial php scripts" because that is not part of the code above. What does that do?  Well that is my first stab at using PHP to build a mailing list. Go ahead and try to join the list repeatedly and you will see the script won't let you join twice with the same email address. In tomorrow's diary I will show you how I did this, but suffice to say if you check that box you will be added to our php-scripts mail list. Don't worry, I'm not going to spam you with unrelated information, or sell or give away your email address like a lot of other lame sites do. I will also show you how to create an unsubscribe script so that people in your opt-in email list can easily unsubscribe. It isn't very acceptable these days to create mail lists that people can't easily unsubscribe from. If you do stay on my mail list, however, I will notify you when I start releasing freeware and commercial PHP scripts on this site, which, depending on when you are reading this might be already :)

Retrospective: what has been learned so far?

In 10 days using PHP I've created a small, dynamic, and yet useful voting script using PHP, worked with date and time (12/17/99), learned how to generate random numbers (12/27/99), how to open, read, append and close files (12/26/99), how to take visitor input (12/22/99) from site visitors and use php to manipulate that information, and we learned a little bit about security (12/22/99) in PHP as well as sending and using cookies (12/23/99), and today we built a form to send email. Whew! That's a lot for 10 days, but there are still many things under the hood of PHP to explore. Armed with this existing knowledge, however, quite a few different useful applications can be created using PHP. If you are even brand new to programming I hope you have been able to glean at least a little knowledge (your surfer ratings so far indicate you have).

Here is just a small list of things I'd still like to do with PHP that will likely become future diary entries:

searching remote (or local) URLs for information
file uploading with browser
creating and using a mySQL database
sorting a-z, z-a and numerically 1,2,3,4 and 5,4,3,2,1
blending JavaScript and other languages with PHP

Using the form above is your chance to send me ideas for future diary entries, so take a few seconds and shoot me your requests.

Please vote on what you think of this diary lesson :)

How useful was this diary entry? Avg Surfer Rating: 90501.63 (1105)

[back]WB01624_.gif (281 bytes) 12/28/99 "Sending email with PHP" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 01/6/00 05:49