Developers Archive for March, 2007

Read your e-mail with PHP!

Read your e-mail with PHP! Monday, March 19th, 2007

To retrieve the e-mails in our inbox we will have to use the POP3 protocol, and the raw PHP socket functions, such as fsockopen. But why should we all write it from scratch, when there are already many great libraries available?

That’s why we’re going to use a POP3 class from PHPClasses.org, available at http://www.phpclasses.org/browse/package/1120.html. Unfortunately, you’ll need to login to download the package, but registration doesn’t take long.

Once you’ve downloaded the class, try the following example:
<?php
require (’pop3.class.inc’);

$pop3 = new POP3;

// Connect to mail server
$do = $pop3->connect (’YOUR-EMAIL-SERVER-HERE);
if ($do == false) {
die($pop3->error);
}

echo ‘Connected to mail server’;

$pop3->close();

?>

In the above example you will have to put in your own mail server. If you don’t know what your own mail server is, try something like mail.YOURDOMAIN.com or mail.YOURISP.com.

If everything goes okay, you will see ‘Connected to mail server’ when running the example. This means that the script was able to connect to your mail server, and the next step is to login to your e-mail inbox.

This can be done with the login() method, and you will have to pass your username (which is usually your e-mail address) and your password, like this:
// Login to your inbox
$do = $pop3->login (’email@address.goes.here.com, ‘password-here’);

if ($do == false) {
die($pop3->error);
}

echo ‘
Logged in’;

Now that we’ve logged into our inbox, we’re almost ready to start downloading new e-mail messages, but first we have to get the ‘office status’, which basically tells our script how many new e-mails there are.

The office status can be retrieved with the get_office_status() method; see the example below:
// Get office status
$status = $pop3->get_office_status();

if ($status == false) {
die($pop3->error);
}

$count = $status[’count_mails’];

echo ‘There are ‘ . $count . ‘ new e-mails waiting for you!’;

As you can see in the above example, the get_office_status() method returns an array, which includes the number of new e-mails, but also a few other details, such as the total size of the new e-mails. This will allow you to create a neat looking progress bar.

Now we can start retrieving the new e-mails with a for-loop and the get_mail() method, like this:
for ($i = 1; $i <= $count; $i++) {
$email = $pop3->get_mail($i);

if ($email == false) {
echo $pop3->error;
continue;
}

echo ‘<pre>’;
print_r ($email);
echo ‘</pre>’;
}

If you run the above example, it will print every new e-mail in your inbox, including the headers of each e-mail.

Drop Down Box with automatic redirect

Drop Down Box with automatic redirect Monday, March 19th, 2007

We can do drop down box with automatic redirect using Java script. The following Java script code will enable visitors to click on a drop down box to select the location on web site in which they would like to navigate. Once they make their selection, they will automatically be taken to the location.

We can place this code where we would like the box to appear:

<Script language=”JavaScript”>
function goto(form) { var index=form.select.selectedIndex
if (form.select.options[index].value != “0″) {
location=form.select.options[index].value;}}
</SCRIPT>
<FORM NAME=”form1″>
<SELECT NAME=”select” ONCHANGE=”goto(this.form)” SIZE=”1″>
<OPTION VALUE=”">——-Choose a Selection——</option>
<OPTION VALUE=”index.html”>Home
<OPTION VALUE=”main_page.html”>Main page
<OPTION VALUE=”contact.htm”>Contact us
</SELECT>
</FORM>

Javascript String Split Function

Javascript String Split Function Friday, March 16th, 2007

A string can be splitted in javascript by using the split() function. The split function takes the delimiter as an arguement. Whenever it sees the delimiter the string is splitted and stored in the array.

sample code:
<script type=”text/javascript”>
var myString = “Javascript string split function”;

var mySplitString = myString.split(” “);

for(i = 0; i < mySplitString.length; i++)
{
 document.write(”<br /> Element ” + i + ” = ” + mySplitString[i]);
}
</script>

In the above example the delimiter used is the space character ” “. Thus the split function splits the given string whenever it finds the space character and stores the result in the array. The result can be viewed by looping through the elements of the array with its length.

Result:
Element 0 = Javascript
Element 1 = string
Element 2 = split
Element 3 = function


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.