Reading of mail through the Web-interface

Recently post systems with the Web-interface became rather popular. It is necessary to notice, it is rather convenient, especial for users who leave in the Internet from different computers.


Library Mail is necessary for a spelling of such program:: POP3Client


Purpose  of library obviously - is visible from the name. She contains methods of access to mail box POP3. The elementary set of a code for check of a mail box on presence of messages looks so:

use Mail:: POP3Client;

$login = "mailuser";

$password = "parol";

$mailserver = "mail.server";

$pop = new Mail:: POP3Client (USER => $login,

PASSWORD => $password,

HOST => $mailserver);

print " In your mail box ", $pop-> Count (), " messages. <br> n ";



All voobhhem, is simple:

1.         To create connection;

2.         To count quantity{amount} of letters.


The first item{point} we sell creation of object $pop, the second - a call of method Count () this object. Method Count () still has one feature - if he returns-1 connection has not been created, otherwise, it was not possible to incorporate to the server. If we in the above-stated code shall replace last line on

if ($pop-> Count () ==-1) {

print " it was not possible to incorporate to the server! <br> n ";

} else {

print " In your mail box ", $pop-> Count (), " messages. <br> n ";

}



, That will look our script a little bit more decently (after all, processing of a supernumerary situation:). If this code has earned, it is necessary to develop success. So, methods of class POP3Client:

new (USER => ' user ', PASSWORD => ' password ', HOST => ' host ', PORT => 110, DEBUG => 0, AUTH_MODE => ' BEST ', TIMEOUT => 60)



He/she is the designer of a class. Developers of library I strongly recommend to use ob``eknym style of a spelling. Arguments of a method the following:

?         USER = a login name

?         PASSWORD = the password

?         HOST = pochtov? j the server

?         PORT = port

?         DEBUG = if this parameter is equal 1 in STDERR the traffic of a socket is deduced

?         AUTH_MODE = allowable values:'BEST ', ' PASS' and ' APOP '

?         TIMEOUT = number by default - 60 sek.


Head (MESSAGE_NUMBER)



Returns heading of the letter with number{room} MESSAGE_NUMBER. Result = a line or a file, depending on a context. It is supported not by all POP3 servers.

Body (MESSAGE_NUMBER)



Returns a body of the letter with number{room} MESSAGE_NUMBER. Result = a file of lines or a file, depending on a context.

Delete (MESSAGE_NUMBER)



Marks the message on removal{distance}. Leaves after performance of QUIT command. Before removal{distance} it is possible to cancel method Reset.

Close



To close connection.

Count



Returns quantity{amount} of messages in a mail box. There is no purpose to substitute this clause{article} the description of library, therefore shall be limited only to these descriptions. In library Mail:: POP3Client approximately in 2 times it is more than methods, and to read his  description in the primary source it is a little bit more useful, as for the beginning it is enough job and the set forth above methods.


Now it would be quite good to see{overlook} the list of messages on the server, from whom, a subject. For this purpose we shall count messages and in a cycle we shall deduce{remove} their headings:

....

print " In your mail box ", $pop-> Count (), " messages <br> n ";

for ($i = 1; $i <= $pop-> Count (); $i ++) {

foreach ($pop-> Head ($i)) {

/ ^ (From|Subject):s +/i ** print $ _, " <br> n ";

}

}

*ºb¬UHHFeh connection

$pop-Close ();



If has earned also this a design, all means is done{made} correctly, and it is possible to pass to a spelling of the module of viewing of messages. To see{overlook} the message ¹KAKOJJ_TO it is possible, having pulled out his  method Body (¹). As the method will return us a file an ordinary cycle foreach we shall see{overlook} the letter.


The text of procedure of viewing of a body of the letter:

sub get_body {

$numb_mess = shift;

foreach ($pop-> Body ($numb_mess)) {

print $ _, " <br> n ";

}

}



Well, and is farther - business of taste how to choose the message. I suggest to make a reference on a subject of the letter for viewing it. Full listing of this "masterpiece" is located below:

use Mail:: POP3Client;

print "Content-type:text/htmlnn";

$login = "mailuser";

$password = "parol";

$mailserver = "mail.server";

* We taste to read the transferred{handed} parameters

* This way works only with method GET!!!

$temp = $ ENV {' QUERY_STRING '};

* Analysis of a line of parameters

if ($temp ne ") {

@pairs=split (/and/, $temp);

foreach $item (@pairs) {

($key, $content) =split (/=/, $item, 2); * we Cut on the name of a key and value.

* Names of parameters and values it is placed in khehsh

$data {$key} = $content;

}

}

* We open connection

$pop = new Mail:: POP3Client (USER => $login,

PASSWORD => $password,

HOST => $mailserver);

* If the parameter view with value we look through has been transferred{handed}

* The message with number{room} view

if ($data {' view '} ne ") {

*get_body ($data {' view '});

} else {* there were no parameters

print " In your mail box ", $pop-> Count (), " messages. <br> n ";

for ($i = 1; $i <= $pop-> Count (); $i ++) {

foreach ($pop-> Head ($i)) {

/ ^ (From|Subject):s +/i ** print " <a href = " $ SCRIPT_NAME? view = $ i ">", $ _, " </a> <br> n ";

}

}

*ºb¬UHHFeh connection

}

$pop-Close ();

*EU«µSsTUb viewing of the message

sub get_body {

$numb_mess = shift;

foreach ($pop-> Body ($numb_mess)) {

print $ _, " <br> n ";

}


}



Actually and all. Actually I do not advise to spread this pearl in the Internet. It  it is possible juzat` on localhost-S. Why? Because in a body of the program there are values of logins / passwords, and it to suppose does not cost. It is better to make the form with pair fields, and to pass their method POST to this script. To read a line of parameters by transfer by method POST it is possible kostrukciej:

if ($ENV {' REQUEST_METHOD '} eq ' POST ') {

read (STDIN, $temp, $ENV {' CONTENT_LENGTH '});

}



I.e. to read it is necessary from a standard stream of input. Analysis of a line precisely same, as well as in method GET. For the further job with a mail box the pair the login / password can be saved in the latent field or in cookie with the period of obsolescence, for example, half an hour. Further, first of all after a seal of line Content-type:text/htmlnn, we shall read this kuki:

$cookie = $ENV {' HTTP_COOKIE '};



Further it  we assort, as it is necessary for us. I offer the following realization:

if ($data {' login '} and $data {' password '}) {


    * We shall remember a password / login in kuki as a line:

* login:: password, time of obsolescence = 1 hour.

print " Set-Cookie: login = $ form {' login '}:: $form {' password '}; expires = + 1H; ";

print "Content-type:text/htmlnn";

* Attempt to create connection with POP3 the server

$pop = new Mail:: POP3Client (USER => $data {' login '},

PASSWORD => $data {' password '},

HOST => $mailserver);

} else {

print "Content-type:text/htmlnn";

$cookie = $ENV {' HTTP_COOKIE '};

    * We assort a line received from cookie

if ($cookie = ~ "login") {

($nm, $vals) = split (/=/, $cookie);

($login, $passwd) = split (/::/, $ vals);

$pop = new Mail:: POP3Client (USER => $login,

PASSWORD => $password,

HOST => $mailserver);

    * Further under the text....

}



Basically and all - small unpretentious mailreader it is ready. But there is one essential lack: "Cyrillic" texts frequently are stored{kept} in the letter as mime encoded. Therefore that them to read, it is necessary to use corresponding libraries or to write the converters, but it already other subject...