Autoresponders With PHP

Written by Robert Plank

Continued from page 1

body of my message."); ?>

Replace "" 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 inrepparttar 105088 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 withrepparttar 105089 subject "Hello" andrepparttar 105090 message "Hi. This isrepparttar 105091 body of my message."

If you're using a free e-mail service or a weird ISP,repparttar 105092 message won't come through because a lot of mail servers these days require that certain headers are present inrepparttar 105093 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 byrepparttar 105094 mail server. This data tells us that we're sending a plain text e-mail, thatrepparttar 105095 message came from your e-mail address (and gives your name), and tells us thatrepparttar 105096 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 isrepparttar 105097 code you should have by this point, complete withrepparttar 105098 header information andrepparttar 105099 variables which tellrepparttar 105100 script what your name and e-mail address are:

$email = "";

$myname = "Your Name Here"; $mymail = "";

$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 isrepparttar 105101 body of my message.",$headers);


Notice how we've simplified things a bit by using variables inrepparttar 105102 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:

$myname = "Your Name Here"; $mymail = "";

$subject = "Hello"; $body = "Hi. This isrepparttar 105103 body of my message. Notice how I can continue typing right onrepparttar 105104 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 I removedrepparttar 105105 line that set $email to "" This is becauserepparttar 105106 value of $email will be passed torepparttar 105107 script from that form we made earlier.

This also sendsrepparttar 105108 e-mail message ONLY ifrepparttar 105109 value of $email is not blank. So if someone just hitrepparttar 105110 submit button without entering an address,repparttar 105111 script won't try to sendrepparttar 105112 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 inrepparttar 105113 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 sendingrepparttar 105114 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:"); die();

Tryrepparttar 105115 autoresponder out. You'll see that oncerepparttar 105116 autoresponse message is sent, you're directed to Now, go ahead and change it to whatever URL you want to use. Or, make use it with a variable sorepparttar 105117 end result is like this:

$myredirect = " hankyou.html";

$myname = "Your Name Here"; $mymail = "";

$subject = "Hello"; $body = "Hi. This isrepparttar 105118 body of my message. Notice how I can continue typing right onrepparttar 105119 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 changerepparttar 105120 values above. " hankyou.html" needs to point torepparttar 105121 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.

Article by Robert Plank PHP Newsletter:


Password Protection and File Inclusion With PHP

Written by Robert Plank

Continued from page 1

If you want to takerepparttar next step, go back to your code and change "myusername" and "mypassword" to a username and password of your choice. Upload it back to your web server and try again. Now go to that page again and you'll see that you can only be let in usingrepparttar 105085 username and password you chose for yourself.

Now changerepparttar 105086 part that says "My Protected Area" to something else, say "John Calder's Bar and Grill." Upload and try it. You'll see when that password box comes up under "Realm" it'll say "John Calder's Bar and Grill." You can change this to whatever you like.

But what if you want to password protect just a handful of files? Do you have to copy and paste this code onto PHP script after PHP script?

Hell no!

Takerepparttar 105087 code you just modified and takerepparttar 105088 last line out of it. You know,repparttar 105089 one that said "my main text." All you should have in there now is everything in betweenrepparttar 105090 PHP brackets ().

Save this file as "auth.php". You can rename this later, on your own time.

Make a new file called "test.php" or just rename one of your normal HTML to this name. It doesn't matter. Atrepparttar 105091 very top of test.php (the VERY top, meaningrepparttar 105092 first line) copy and paste this line of code:

Upload auth.php and test.php to your web server and run test.php. Make sure both files are placed inrepparttar 105093 same folder. Now, try to go to test.php in your web browser. You'll see that you can't get to test.php withoutrepparttar 105094 right username and password. You can do this to any file with a ".php" extension just by adding that one line of code.

The catch to it is that this line of code has to be atrepparttar 105095 very top ofrepparttar 105096 file. Onrepparttar 105097 very first line. The reason for this is that whenrepparttar 105098 script asks for a person's username and password, these are sent using HTTP headers and *must* come before anything else.

Of course, this doesn't take care of your secret sites or private members' areas, where you have to deal with several logins, but that's what htaccess is for.

While we're onrepparttar 105099 subject of includes, one last thing before we finish up.

Includes are basically a way of absorbing other files into your script. As you saw when we included auth.php,repparttar 105100 script read everything that was in auth.php and used it as ifrepparttar 105101 contents of that file were actually there. This works with not only PHP scripts but also with other files as well.

Make a new file called "header.html". Put anything you want in it, but I just put "This is my header " when I did it.

Make a second file called "footer.html". Again, go again and put anything you want in it, but I just put "This is my footer " in.

Make a third file called "main.php." Copyrepparttar 105102 following into it.

This is my main page

Upload all three intorepparttar 105103 same folder and run main.php. You should seerepparttar 105104 following:

This is my header This is my main page This is my footer

This is just a basic example of how includes can be used. But if you have a web site with several pages andrepparttar 105105 same layout... wouldn't it be easier just to put everything above your main text in header.html and everything below that main text in footer.html? That way if you change your design you only have to edit 2 files instead of 100 or 200?

You'd think.

Article by Robert Plank

Free PHP articles and advice.

    <Back to Page 1 © 2005
Terms of Use