Autoresponders With PHP

First off, check out the URL below. You'll learn how to make that today. http://www.jumpx.com utorials/3/signup.html Fill out your e-mail address on the page you see. (I promise it's not being saved anywhere.) Then, wait a minute or two and check your mail. You should get a message from Gumby (null@jumpx.com) containing a sample autoresponder message. Today, we're going to learn three easy things: redirection, mail sending, and form submission. When we finish with that, you will know how to put those components together and create an autoresponder. Because if you think about it, that's all an autoresponder does. Somebody enters in their e-mail address, are sent an e-mail message, and then are redirected to a new page. Of course there are more complex autoresponders, like Gary Ambrose's Opt-In Lightning, or Wes Baylock's Mail Master Pro which handle multiple follow-ups and record the e-mail addresses of those who have signed up for the responder. But today we're just going to focus on how to make a very basic, very simple autoresponder. Hopefully, you've seen what form objects in HTML look like. Here's some code you can use for an example:
Enter Your E-Mail Address: Copy and paste this code into a file called "signup.html" and upload it to your web server. You'll see a text box waiting for your visitor to enter his or her e-mail address so they can be sent that autoresponse message. Of course, the form won't work just yet because, if you look at the first line of that HTML code I gave you, you'll see that the form submits to a script called "some-script.php". And we haven't made that just yet. Look on the second line of "signup.html", at the last half of the line. You should be familiar with HTML tags, but if you're not, an HTML tag consists of two parts: the parent tag and the attributes. The parent tag is simply the tag's designation. For example, if you had a slice of HTML code that looked like this: Then the parent tag would be "font". The rest of what's enclosed in the tag tells the browser what to do with it. For example, in this tag the attributes are that the font should be Verdana with size 1. Why am I telling you all this? Because it relates to the HTML code you see in signup.html. Now, when you look at this: The code tells the receiving browser that this is an "input" tag, meaning that it's a form field. The name of this item is "email" and its size is 30, meaning this text box should be 30 characters in width. When the form is submitted, it takes all the values of all the fields inside that form and throws it at its destination. In this case, our destination is "some-script.php". If you're lost, this will all make a whole lot more sense once you try this next step. Make a file called "some-script.php" and paste this line of code into it: Upload the script in the same folder as signup.html, and go to "signup.html". Type your e-mail address in and click the submit button. You should see a new page containing just your e-mail address and nothing else. Is this starting to make sense? You told the PHP script to dump the contents of the variable called "email" to the screen, and you just submitted a form with a text box called "email". If you want to try one more exercise like this, change the name of the text box to, say, "goober" in signup.html and change the $email in some-script.php to $goober. Upload both, go to signup.html, and type anything into the text box. You'll get the same result. This is how you'll pass data from forms (like text fields, drop down menus, radio buttons and the like) along into the PHP scripts you create. We've just covered how to submit form elements into PHP. Now let's focus on sending mail. PHP has a really simple function that uses whatever mail sending program is installed on your server to send messages to the outside world. If you have a crappy web server, this step might not work and you'll have to use a different web host if you want to try this. But if you're on a good web host that has PHP installed *correctly*, this shouldn't be a problem. Up until now we haven't used functions in PHP too much, aside from simple things like include() and header(). Today's your lucky day, because functions work in a very similar way to HTML tags. You have the parent tag, and the attributes (or parameters). The mail() function basically works like this: mail("recipient","subject","body","headers"); Let's start off by sending a simple e-mail message to yourself. We won't need any special headers this time around, so this will be quick and painless. Copy this one line of code into "mailtest.php": Replace "billg@microsoft.com" with your actual e-mail address, but be *sure* to keep quotes around it. Save it, and upload mailtest.php to your web server and run it in the browser. You should see a blank page. Wait a few minutes and check your mail. You should see a mysterious mail message in your box with the subject "Hello" and the message "Hi. This is the body of my message." If you're using a free e-mail service or a weird ISP, the message won't come through because a lot of mail servers these days require that certain headers are present in the message. Let's do that now. What's below isn't important enough to explain thoroughly, but it's just header information that is interpreted by the mail server. This data tells us that we're sending a plain text e-mail, that the message came from your e-mail address (and gives your name), and tells us that the e-mail "client" we used was PHP. $headers = "Content-Type: text/plain; charset=us-ascii From: $myname <$mymail> Reply-To: <$mymail> Return-Path: <$mymail> X-Mailer: PHP"; This is the code you should have by this point, complete with the header information and the variables which tell the script what your name and e-mail address are: Reply-To: <$mymail> Return-Path: <$mymail> X-Mailer: PHP"; mail($email,"Hello","Hi. This is the body of my message.",$headers); ?> Notice how we've simplified things a bit by using variables in the mail() function. That way we don't have to retype things. This method also looks better (in my opinion anyway) and is easier to tweak once you're ready to actually customize it for yourself. Try this out again. Believe it or not, but you just made your first autoresponder! Before we move on let's make this look even cleaner: Reply-To: <$mymail> Return-Path: <$mymail> X-Mailer: PHP"; if ($email != "") { mail($email,$subject,$body,$headers); } ?> All I did here was just make things look nicer, but notice how I removed the line that set $email to "billg@microsoft.com." This is because the value of $email will be passed to the script from that form we made earlier. This also sends the e-mail message ONLY if the value of $email is not blank. So if someone just hit the submit button without entering an address, the script won't try to send the e-mail message. Everything should be ready for you to try out now. Re-upload "some-script.php" and go to signup.html. Enter your e-mail address in the field, hit submit and wait for that mail message to arrive. There's only one step left to making this autoresponder complete. And that's sending the user somewhere so they aren't given a blank page. Find this line in your script: if ($email != "") { mail($email,$subject,$body,$headers); } And paste this directly underneath it: header("Location:http://www.jumpx.com"); die(); Try the autoresponder out. You'll see that once the autoresponse message is sent, you're directed to www.jumpx.com. Now, go ahead and change it to whatever URL you want to use. Or, make use it with a variable so the end result is like this: Reply-To: <$mymail> Return-Path: <$mymail> X-Mailer: PHP"; if ($email != "") { mail($email,$subject,$body,$headers); } header("Location:$myredirect"); die(); ?> Don't forget to change the values above. "http://www.my-domain-name.com hankyou.html" needs to point to the URL where thankyou.html is stored. You're done. Don't forget to send John feedback for me. If you're really curious as to how to do something in PHP, I might just write an article on it.