Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Online Phone Directory 1

Status
Not open for further replies.

abbottboi

IS-IT--Management
Nov 14, 2005
94
CA
Hi,

I need to build a small database for about 40 names with emails and phone numbers to be added to my web site.

It would be great if i could update it when i need to and users could click a link to download the latest excel version of it. (.csv file whatever)

This is a directory of staff so this would be great since currently i have to keep updating an excel sheet and emailing everyone the updated version.

Any help would be great as i'm not a programer at all.

thanks
 
First get yourself a Database server such as MYSQL or even Access, although I would not recommend it, and build the DB with the tables and fields you need.

Once you have your basic structure we can help you with how to interact with it using PHP.






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

For 40 people a full featured database is a bit of an overkill to my mind. A flat file/excel sheet will do just as well.

i've written a quick script to get you going. you need to download phpmailer and put it in a directory called phpmailer in the same directory as this file.


you will also need three other files:

1. an html file for the email that you will send to users with the new address book
2. a text file for the non-html users
3. a csv file with all fields quoted with a double quote mark.

you must provide the filenames (and relative paths) of each file to the script by changing the parameter block.

you change the csv file as you wish and upload new variants whenever you want. the users will always have access to the current version either on the screen or by clicking on the download link.

to force the script to send an eamil to everyone, just create a file called changes.txt and put it in the same folder as the script. The file can/should be completely empty. Next time someone accesses the page, the script will send the updated file to the people listed in the address book. It will then delete the changes.txt file if the permissions are set correctly. if they are not you will have to delete it yourself (or change the permissions).

the script itself is below. just copy and paste it. post back if you have problems.

Code:
<?
session_start();
set_magic_quotes_runtime(0);
$sep = PATH_SEPARATOR;
ini_set("include_path", ini_get("include_path") . $sep . "./phpmailer");
$action = (isset($_POST['action']) ? $_POST['action'] : 
			(isset ($_GET['action']) ? $_GET['action'] : 
				(isset($_SESSION['action']) ? $_SESSION['action'] : "view")));
$_SESSION['action'] = $action;


#************************************************************************
//PARAMETERS
$filename =			"addresses.csv" ;		//name of address file
$myemailaddress = 	"email@domain.com" ; 	//email address FROM which mails are to be sent
$myname=			"John Smith";			//you real name
$emailfield =		2 ;						//the column in the csv file in which email addresses are stored (NB col1 = 0)
$htmlmessage = 		"filename.html";		//filename of the html you want to use for the email.
$textmessage =		"textmessage.txt";		//filename of the text you want for non html readers						
$subject =			"New Address list attached";	//email subject line
$emailfield =		2 ;						//the column in the csv file in which email addresses are stored (NB col1 = 0)
$namefield=			0;						//the column in which the person's name is stored in the csv file (nb col1=0)
#************************************************************************

function emailcsvfiletousers() {

	require("class.phpmailer.php");
	
	global $emailfield, $filename, $myemailaddress, $myname, $htmlmessage, $textmessage, $subject, $namefield;
	if (!file_exists($filename)):
		return false;
	endif;
	$bcc = "";

	//create the mail object.  this assumes you have a mail server on the localhost
	$mail = new PHPMailer();
	$mail->Host     = "localhost";
	$mail->Mailer   = "smtp";
	$mail->From     = $myemailaddress;
	$mail->FromName = $myname;
	$mail->AddAddress($myemailaddress,$myname); 
	$mail->WordWrap = 50; 
	$mail->AddAttachment("addresses.csv"); 	
	$mail->IsHTML(true);
	$mail->Subject  =  $subject;
	
	
	//get the mail contents
	$mail->Body     =  @file_get_contents($htmlmessage);
	$mail->AltBody  =  @file_get_contents($textmessage);

	//this picks up the addressees from the csv fil
	$fh = @fopen($filename, "rbt");
	if ($fh !== false):
		while (($data = fgetcsv($fh, 2048, ",")) !== FALSE):
			$email = $data[$emailfield];
			if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)):
				$mail->addBCC($email, $data[$namefield]);
			endif;
		endwhile;
		fclose($fh);
	endif;

	if(!$mail->Send()):
		 echo "Mailer Error: " . $mail->ErrorInfo;
		 return false;
	else:
		return true;
	endif;
}


//check to see whether there is a change flag 
//and if there is get a list of email addresses and 
//send the file to the users

if (file_exists("change.txt")):
	if (emailcsvfiletousers()):
		@unlink ("change.txt");
	else:
		@unlink ("change.txt");
		//you might want to change this
	endif;
endif;
$contents="";


switch ($action):
	case "download":
		//this allows for the direct download of the address book.
		if (!file_exists("addresses.csv")):
			$contents = "There are no current names in the address list";
			break;
		endif;		
		if (isset($_GET['permission']) && isset($_SESSION['permission'])):
			if ($_GET['permission'] === $_SESSION['permission']):
				unset ($_SESSION['permission']);
				header("Content-Length: " . filesize("addresses.csv") );
			    header("Content-Type: application/force-download");
		    	header('Content-Disposition: attachment; filename="addresses.csv"');
				header("Content-Transfer-Encoding: binary\n");
				readfile("addresses.csv");
				exit;
			endif;
		endif;	//note we do not break out of this as the exit handles the dl.


	case "view":
	default:
		//this shows the name and addresses on the page
		
		if (!file_exists("addresses.csv")):
			$contents = "There are no current names in the address list";
			$flag = true;
			break;
		endif;
		$fh = fopen("addresses.csv", "rbt");
		if ($fh == false):
			$contents = "There are no current names in the address list";
			$flag=true;
			break;
		endif;			
		$header = true;
		$col = "#FFFCD2";
		while (($data = fgetcsv($fh, 2048, ",")) !== FALSE):
			$col = ($col === "#FFFCD2") ? "white" : "#FFFCD2";
			if ($header) $col = "white";
			$contents .= "\r\n<tr style=\"background-color:$col; text-align:left;\">";
			foreach ($data as $val):
				if ($header===true):
					$contents .= "<th>$val</th>";
					
				else:
					$contents .= "<td>$val</td>";
				endif;
			endforeach;
			$header = false;
			$contents .= "</tr>\r\n";
		endwhile;
		$contents = "<table border=\"1\">\r\n $contents \r\n </table>";
		fclose($fh);
	break;
endswitch;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Address Book</title>
</head>

<body>
<? echo $contents; if (!isset($_SESSION['permission'])) $_SESSION['permission'] = uniqid ("prm_",true);?>
<br/>
<? if (empty($flag)) : ?><h2><a href="<?=$_SERVER['PHP_SELF']?>?action=download&permission=<?=$_SESSION['permission']?>">Download</a> the latest version of the address book</h2><? endif;?>

</body>
</html>
 
sorry - use this code instead. i had forgotten to remove some hard coded values and replace them with parameters.

Code:
<?
session_start();
set_magic_quotes_runtime(0);
$sep = PATH_SEPARATOR;
ini_set("include_path", ini_get("include_path") . $sep . "./phpmailer");
$action = (isset($_POST['action']) ? $_POST['action'] : 
			(isset ($_GET['action']) ? $_GET['action'] : 
				(isset($_SESSION['action']) ? $_SESSION['action'] : "view")));
$_SESSION['action'] = $action;


#************************************************************************
//PARAMETERS
$filename =			"addresses.csv" ;		//name of address file
$myemailaddress = 	"email@domain.com" ; 	//email address FROM which mails are to be sent
$myname=			"John Smith";			//you real name
$emailfield =		2 ;						//the column in the csv file in which email addresses are stored (NB col1 = 0)
$htmlmessage = 		"filename.html";		//filename of the html you want to use for the email.
$textmessage =		"textmessage.txt";		//filename of the text you want for non html readers						
$subject =			"New Address list attached";	//email subject line
$emailfield =		2 ;						//the column in the csv file in which email addresses are stored (NB col1 = 0)
$namefield=			0;						//the column in which the person's name is stored in the csv file (nb col1=0)
#************************************************************************

function emailcsvfiletousers() {

	require("class.phpmailer.php");
	
	global $emailfield, $filename, $myemailaddress, $myname, $htmlmessage, $textmessage, $subject, $namefield;
	if (!file_exists($filename)):
		return false;
	endif;
	$bcc = "";

	//create the mail object.  this assumes you have a mail server on the localhost
	$mail = new PHPMailer();
	$mail->Host     = "localhost";
	$mail->Mailer   = "smtp";
	$mail->From     = $myemailaddress;
	$mail->FromName = $myname;
	$mail->AddAddress($myemailaddress,$myname); 
	$mail->WordWrap = 50; 
	$mail->AddAttachment("addresses.csv"); 	
	$mail->IsHTML(true);
	$mail->Subject  =  $subject;
	
	
	//get the mail contents
	$mail->Body     =  @file_get_contents($htmlmessage);
	$mail->AltBody  =  @file_get_contents($textmessage);

	//this picks up the addressees from the csv fil
	$fh = @fopen($filename, "rbt");
	if ($fh !== false):
		while (($data = fgetcsv($fh, 2048, ",")) !== FALSE):
			$email = $data[$emailfield];
			if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)):
				$mail->addBCC($email, $data[$namefield]);
			endif;
		endwhile;
		fclose($fh);
	endif;

	if(!$mail->Send()):
		 echo "Mailer Error: " . $mail->ErrorInfo;
		 return false;
	else:
		return true;
	endif;
}


//check to see whether there is a change flag 
//and if there is get a list of email addresses and 
//send the file to the users

if (file_exists("change.txt")):
	if (emailcsvfiletousers()):
		@unlink ("change.txt");
	else:
		@unlink ("change.txt");
		//you might want to change this
	endif;
endif;
$contents="";


switch ($action):
	case "download":
		//this allows for the direct download of the address book.
		if (!file_exists($filename)):
			$contents = "There are no current names in the address list";
			break;
		endif;		
		if (isset($_GET['permission']) && isset($_SESSION['permission'])):
			if ($_GET['permission'] === $_SESSION['permission']):
				unset ($_SESSION['permission']);
				header("Content-Length: " . filesize($filename) );
			    header("Content-Type: application/force-download");
		    	header("Content-Disposition: attachment; filename=\"$filename\"");
				header("Content-Transfer-Encoding: binary\n");
				readfile($filename);
				exit;
			endif;
		endif;	//note we do not break out of this as the exit handles the dl.


	case "view":
	default:
		//this shows the name and addresses on the page
		
		if (!file_exists($filename)):
			$contents = "There are no current names in the address list";
			$flag = true;
			break;
		endif;
		$fh = fopen($filename, "rbt");
		if ($fh == false):
			$contents = "There are no current names in the address list";
			$flag=true;
			break;
		endif;			
		$header = true;
		$col = "#FFFCD2";
		while (($data = fgetcsv($fh, 2048, ",")) !== FALSE):
			$col = ($col === "#FFFCD2") ? "white" : "#FFFCD2";
			if ($header) $col = "white";
			$contents .= "\r\n<tr style=\"background-color:$col; text-align:left;\">";
			foreach ($data as $val):
				if ($header===true):
					$contents .= "<th>$val</th>";
					
				else:
					$contents .= "<td>$val</td>";
				endif;
			endforeach;
			$header = false;
			$contents .= "</tr>\r\n";
		endwhile;
		$contents = "<table border=\"1\">\r\n $contents \r\n </table>";
		fclose($fh);
	break;
endswitch;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Address Book</title>
</head>

<body>
<? echo $contents; if (!isset($_SESSION['permission'])) $_SESSION['permission'] = uniqid ("prm_",true);?>
<br/>
<? if (empty($flag)) : ?><h2><a href="<?=$_SERVER['PHP_SELF']?>?action=download&permission=<?=$_SESSION['permission']?>">Download</a> the latest version of the address book</h2><? endif;?>

</body>
</html>
 
Thanks a lot.. this sounds exactly like what i wanted.

Do i copy and paste this code into a HTML doc? Should this code be the HTML doc?
What do i need to do to prepare the .txt doc?
Should only the headers be in double quotes in the excel file? with the other rows be the information?

thanks again
 
the code above is the operative document. name it with a ".php" extension. php will generate all the html you need

the two email docs (docs 1 and 2) are as simple as

htmldoc
Code:
<h2>Update to the address book</h2>
<p>Please find attached a new address book with all the latest changes included.</p>
<p>with love from your<br/>
friendly admin</p>

textdoc
Code:
UPDATE TO THE ADDRESS BOOK
Please find attached a new address book with all the latest changes included.
with love from your
friendly admin

i.e. they are the body content of the emails you send.

as with all normal csv files each field must be enquoted and comma separated. if you are using a text editor (as opposed to excel), you should also make sure there is a CRLF at the end of each record. ie.

Code:
"name","email","telnum"
"jpadie","somemailaddress","somtelnum"
 
Great!so i set up the files as you said. (please see attached pic) Now i need to change the programming of the phpmailer. Could you tell me what i need to change exactly?

screen.jpg
 
do you have an smtp server on the pc that is running this app? if not you can install one through windows or use one elsewhere in the network or wherever. you need to plug the details of your server (the ipaddress or domain name) into this line
Code:
$mail->Host     = "localhost";

if your smtp server uses smtp auth, post back and i will give you the code to extend it .

you do not need to change anything in phpmailer but you do need to set the parameters of this script correctly. the script parameters are in the big block at the top.
 
i'm not sure if my web host has one on the pc that's hosting my site but i do have a smtp.server.ca address?

If that's complicated then the whole auto-emailing the clients isn't that necessary. I would really just need the ability to add new information and i could manally email everyone to tell them to go to the webpage and download the new csv file.
 
does your smtp.server.ca require a user name and password?

it's not complicated at all either way. to use without the automailer just do not create a file called changes.txt in the same directory. without this file the automailer never gets called.
 
perfect so i'll skip the automailer for now. What else do i need to do in the php file for this to work?
 
Thanks.. works fine!!! (totally didn't realize my server admin didn't activate PHP)

Thanks man, your a genius!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top