T-XU.COM      
Home |
  Home>Computer Programming>CGI>
Autoresponders With PHP
By Robert Plank        [Hits: 14989]



First off, check out the URL below. You'll learn how to makethat today. http://www.jumpx.com utorials/3/signup.html

Fill out your e-mail address on the page you see. (I promiseit's not being saved anywhere.) Then, wait a minute or two andcheck 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, mailsending, and form submission.

When we finish with that, you will know how to put thosecomponents together and create an autoresponder. Because if youthink about it, that's all an autoresponder does. Somebodyenters in their e-mail address, are sent an e-mail message, andthen are redirected to a new page.

Of course there are more complex autoresponders, like GaryAmbrose's Opt-In Lightning, or Wes Baylock's Mail Master Prowhich handle multiple follow-ups and record the e-mail addressesof those who have signed up for the responder. But today we'rejust going to focus on how to make a very basic, very simpleautoresponder.

Hopefully, you've seen what form objects in HTML look like.Here's some code you can use for an example:

Enter Your E-MailAddress:



Copy and paste this code into a file called "signup.html" andupload it to your web server. You'll see a text box waiting foryour visitor to enter his or her e-mail address so they can besent that autoresponse message.

Of course, the form won't work just yet because, if you look atthe first line of that HTML code I gave you, you'll see that theform submits to a script called "some-script.php". And wehaven't made that just yet.

Look on the second line of "signup.html", at the last half ofthe line. You should be familiar with HTML tags, but if you'renot, an HTML tag consists of two parts: the parent tag and theattributes.

The parent tag is simply the tag's designation. For example, ifyou had a slice of HTML code that looked like this:



Then the parent tag would be "font". The rest of what's enclosedin the tag tells the browser what to do with it. For example, inthis tag the attributes are that the font should be Verdana withsize 1.

Why am I telling you all this? Because it relates to the HTMLcode 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 30characters in width.

When the form is submitted, it takes all the values of all thefields inside that form and throws it at its destination. Inthis case, our destination is "some-script.php".

If you're lost, this will all make a whole lot more sense onceyou try this next step.

Make a file called "some-script.php" and paste this line of codeinto 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 submitbutton.

You should see a new page containing just your e-mail addressand nothing else.

Is this starting to make sense? You told the PHP script to dumpthe contents of the variable called "email" to the screen, andyou just submitted a form with a text box called "email".

If you want to try one more exercise like this, change the nameof the text box to, say, "goober" in signup.html and change the$email in some-script.php to $goober. Upload both, go tosignup.html, and type anything into the text box. You'll get thesame result.

This is how you'll pass data from forms (like text fields, dropdown menus, radio buttons and the like) along into the PHPscripts you create.

We've just covered how to submit form elements into PHP. Nowlet's focus on sending mail.

PHP has a really simple function that uses whatever mail sendingprogram is installed on your server to send messages to theoutside world. If you have a crappy web server, this step mightnot work and you'll have to use a different web host if you wantto 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, asidefrom simple things like include() and header(). Today's yourlucky day, because functions work in a very similar way to HTMLtags. You have the parent tag, and the attributes (orparameters).

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 willbe 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 uploadmailtest.php to your web server and run it in the browser. Youshould see a blank page. Wait a few minutes and check your mail.You should see a mysterious mail message in your box with thesubject "Hello" and the message "Hi. This is the body of mymessage."

If you're using a free e-mail service or a weird ISP, themessage won't come through because a lot of mail servers thesedays require that certain headers are present in the message.

Let's do that now.

What's below isn't important enough to explain thoroughly, butit's just header information that is interpreted by the mailserver. This data tells us that we're sending a plain texte-mail, that the message came from your e-mail address (andgives your name), and tells us that the e-mail "client" we usedwas 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 withthe header information and the variables which tell the scriptwhat your name and e-mail address are:


$email = "billg@microsoft.com";

$myname = "Your Name Here"; $mymail = "your@email.here";

$headers = "Content-Type: text/plain; charset=us-ascii From:$myname <$mymail> Reply-To: <$mymail> Return-Path: <$mymail>X-Mailer: PHP";

mail($email,"Hello","Hi. This is the body of mymessage.",$headers);

?>

Notice how we've simplified things a bit by using variables inthe mail() function. That way we don't have to retype things.This method also looks better (in my opinion anyway) and iseasier to tweak once you're ready to actually customize it foryourself.

Try this out again. Believe it or not, but you just made yourfirst autoresponder! Before we move on let's make this look evencleaner:


$myname = "Your Name Here"; $mymail = "your@email.here";

$subject = "Hello"; $body = "Hi. This is the body of my message.Notice how I can continue typing right on the next line!";

$headers = "Content-Type: text/plain; charset=us-ascii From:$myname <$mymail> 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 Iremoved the line that set $email to "billg@microsoft.com." Thisis because the value of $email will be passed to the script fromthat form we made earlier.

This also sends the e-mail message ONLY if the value of $emailis not blank. So if someone just hit the submit button withoutentering an address, the script won't try to send the e-mailmessage.

Everything should be ready for you to try out now. Re-upload"some-script.php" and go to signup.html. Enter your e-mailaddress in the field, hit submit and wait for that mail messageto arrive.

There's only one step left to making this autorespondercomplete. And that's sending the user somewhere so they aren'tgiven 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 autoresponsemessage is sent, you're directed to www.jumpx.com. Now, go aheadand change it to whatever URL you want to use. Or, make use itwith a variable so the end result is like this:


$myredirect = "http://www.my-domain-name.com hankyou.html";

$myname = "Your Name Here"; $mymail = "your@email.here";

$subject = "Hello"; $body = "Hi. This is the body of my message.Notice how I can continue typing right on the next line!";

$headers = "Content-Type: text/plain; charset=us-ascii From:$myname <$mymail> 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 tothe URL where thankyou.html is stored.

You're done. Don't forget to send John feedback for me. Ifyou're really curious as to how to do something in PHP, I mightjust write an article on it.
  Top Articles
*CGI Security Issues
*Clever Profit Growth Software
*Why Aren't You Using CGI
*How to Stop Digital Thieves wi
*Open Source Scripts
*5 CGI Scripts You Must Use to
*Quick Intro to PHP Development
*PHP and Cookies; a good mix!
*PHP On-The-Fly!
*this is a test
*Screen scraping your way into
*ASP, CGI and PHP Scripts and R
  Related Articles
*Track your visitors, using PHP
*Password Protection and File I
*Better Writing: What Works and
*PHP On-The-Fly!
*Quick Intro to PHP Development
*PHP and Cookies; a good mix!
*How to Stop Digital Thieves wi
*CGI Security Issues
*Screen scraping your way into
*Mastering Regular Expressions
*CGI: What the Heck Is That?
*ASP, CGI and PHP Scripts and R


Prev: Home Based Business Tax Deductions   Next: The 10-Step Resume Critique



Home | Site Map | Bookmark this site | T-XU RSS
Copyright 2007 T-XU.com - All Rights Reserved Worldwide.