PHP learning experience

My personal learning experience on php. Basic php scripting language for beginners

Saturday, May 13, 2006

Use PHP Code to Send Email

Yesterday, I was assigned an task of creating a contest registration page so that when people register, I should get their information and at the same time, the person who register should get an welcome email from me. How can I accomplish that by php? I know it is a very easy job for a php expert. For me, I am just like all you guys here - a beginner! I start to recall the functions and did some research on the web and finally, I get it done! Please look at the following code and explanations for this email sending php code:

First, I think, if I want to send them a welcome email, which is store in my server called "welcome-mail,html", I should first, tell php find, open and then read the file. So I start by writing the following code:

// I assigned the filename to a variable to tell php where my file is stored
$file = 'welcome-mail.html';
// I am telling php to open the file by using the build-in function in php - "fopen"
$fh = fopen($file, 'r') or die('Could not open file!');
// Tell php to read the file it opend in the last step and then assign a variable $data to content of the file
$data = fread($fh, filesize($file)) or die('Could not read file!');
//After reading the content, close the file by using function "fclose"
fclose($fh);

Then, I need to retrive the information about my registered user:
$fname = trim($_POST['fname']); //Get the first name of register
$lname = trim($_POST['lname']); //Get the last name of register
$mail = trim($_POST['email']); //Get the email of register
$site = trim($_POST['website']); //Get the web address of register
$subject="Thank you for your registration"; // the subject that the participator see
$subject_main="We get another one to join, Rock!"; // the subject I see
$name=$fname.' '.$lname;
$mailmessage="\r\n Name: ".$name."\r\n Website: ".$site."\r\n Email Address: ".$mail;
$mailmessage=$data ; //assign the content of the file to a new variable - "$mailmessage"

Then I am telling php to send the following to different email address, which is to say, send an email to me that tells me there is another person who wants to join the contest and at the same time, send an welcome mail to that new participator.

// Additional headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'To: '.$fname.' '.$lname.' <'.$mail.'>' . "\r\n";
$headers .= 'From: Jeff Cai' . "\r\n";
mail('mymail.com', $subject_main, $message, $headers);
mail($mail, $subject, $mailmessage, $headers);
echo "
Thank you for your registration! You will receive an email from us shortly.


We will provide you tips on how you can make your chances of winning even better and keep you updated with our contest standings. ";

To sum up, here is the whole php code:
$file = 'matt-email.html';
$fh = fopen($file, 'r') or die('Could not open file!');
$data = fread($fh, filesize($file)) or die('Could not read file!');
fclose($fh);
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$mail = trim($_POST['email']);
$site = trim($_POST['website']);
$subject="Thank you for your registration";
$subject_main="We get another one to join";
$name=$fname.' '.$lname;
$mailmessage="\r\n Name: ".$name."\r\n Website: ".$site."\r\n Email Address: ".$mail;
$mailmessage=$data ;


// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: '.$fname.' '.$lname.' <'.$mail.'>' . "\r\n";
$headers .= 'From: Jeff Cai' . "\r\n";

// send an html mail...
mail('mymail.com', $subject_main, $message, $headers);
mail($mail, $subject, $mailmessage, $headers);
echo "
Thank you for your registration! You will receive an email from us shortly.


We will provide you tips on how you can make your chances of winning even better and keep you updated with our contest standings.";
?>

Sunday, May 07, 2006

PHP Learning - How to Create Password Protected Page

Most often, visitors are required to enter username and password to view a page. You can create user auth either with HTTP and Apache or specify in PHP code. Today, let's take a look how to with HTTP and Apache.

You use instructions to Apache, called directives, to set up your authentication application. Directives allow you to specify the files that are password-protected and the valid user names and IDs. The simplest way to use Apache directives for authentication is to create a file called .htaccess

For example, I want to protect my folder called "user". Thus I will create a file called .htaccess. in that file, I specifies the following lines:

AuthUserFile c:\secret\.htpass

AuthGroupFile /dev/null

AuthName Accounting Department

AuthType Basic

Require valid-user


then in the Windows command prompt, i can type

c:\Apache\bin\htpasswd -c c:\secret\.htpass jeff


to create a username for Jeff (in order to do this, you need to go to your httpd.txt file and change "AllowOverride None" to "AllowOverride Authconfig")


Another way to create a page that requires user name and password is through using Http Auth in PHP. Unlike Http auth with Apache, Http auth with PHP have username and password stored in database, in my example, i will use Mysql (since it is my favorite database)

First, I want to create a database, called "Jefftest"

Create database Jefftest;

Then create a table in the database

CREATE TABLE User (

user_name CHAR(10) NOT NULL,

password CHAR(255) NOT NULL,

create_date DATE NOT NULL,

PRIMARY KEY(user_name) );


Then you can access the database from your PHP script with the MySQL functions that are built into PHP. In here, i won't tell you how to access Mysql by using php built-in function (I might in the future) since it is fairly complex to php beginners. But at least you got the idea of how this function is achieved by using HTTP with the apache server.