![]() Development of own sheets of dispatch There is a set of ways of application of such mechanisms, as sheets of dispatch: from messages to users about obnovlenijakh your site before informing interested people on the changes which have been carried out by you during job above some project. However support of such complex mechanism as a mailing list, manually, without corresponding program decisions, can represent enough labour-intensive process. At the same time, development of the scripts allowing you it is easy to administer a sheet of dispatch of any complexity, and to visitors without your participation to subscribe or otpisyvat`sja from those or other news, is not a unsoluble problem even for the unsophisticated person in Internet-programming. The most simple decision of the given problem , in my opinion, is development of corresponding scripts in language PHP as support of the given language is included into the complete set of services practically any company giving services of a hosting. With help PHP at you the opportunity to develop full-function applications will appear, access to which can be carried out from any point of globe by means of Internet. Certainly, exists set of the applications, capable to give to you all necessary functions for management of sheets of dispatches, however all of them (short of necessity of payment of their purchase) possess the following lack: you cannot adjust job of the application to 100 % how you would like. You will be always dissatisfied with something. And development of own PHP-scripts will provide to you a guarantee of that at any moment it will be possible to modify the existing mechanism of a sheet of dispatch. Within the framework of given clause{article} I suggest to consider the decision of a problem in the organizations of a mailing list. For simplicity here it will not be used any SUBD, and its{her} place will be borrowed{occupied} with flat files. It is done{made} only with the purpose of simplification as conversation on use, in particular MySQL, can borrow{occupy} a lot of time, and not for all there is necessary a job with the big lists of dispatches. For the some people will be the list from 300-500 addresses with what the technology of flat files consults perfectly and without delay of job enough. While purchase of a hosting with support MySQL costs{stands} more dearly{expensively}, than a hosting with PHP, but without an opportunity of job with databases. Further in this clause{article} I shall try to state to you main principles of creation and a manipulation lists of dispatches with use of the mechanisms given PHP. For those who is not familiar enough with this powerful programming language of Web-server applications, some words that such PHP. It is interpretive language for creation of active Web-pages. The program on PHP, similarly to the text on JavaScript, VBScript or ASP, is inserted into a HTML-file (or in a separate file with corresponding expansion). The beginning and the end of the program are marked by special brackets. The text outside of these brackets PHP does not interpret: he is passed a Web-browser " as is ". Syntax PHP is based on syntax of languages Si, Java and perl and is rather in detail described in a management{manual} which is included into the complete set of delivery (it also it is possible to take on http: // www.php.net site). We deliver letters So, we shall be defined{determined}, that all scripts will be in root directory, and files with the data - in the catalogue data/. Each of sheets of dispatch will be stored{kept} in a separate file. Besides the log-file, also as a simple test file will be organized. The list of sheets of dispatch will be stored{kept} in a file lists.txt in the catalogue data in which each line the name of the list and a name of a file in which are stored{kept} e-mail addresses of subscribers will contain. Fields will be divided{shared} by a symbol "|". The given file will be used for formation of the dropping out list in the form of administration for manipulation with that or other list of addresses. I the supporter of providing of access to most often to used mechanisms in the most simple way. For this reason I suggest to place on starting page the form for dispatch of messages to participants of a mailing list, and providing of access to other mechanisms of administration to organize with the help of links. So, I suggest you to look at the text of the file resulted below index.php. In figure 1 the result of the reference{manipulation} to it through a browser is submitted. <html> <head> <title> Administration of mailing lists </title> </head> <body> <center> <H2> Administration of mailing lists </H2> </center> <form method=post action = "sendemail.php"> <table> <tr align = "left"> <td width = "200" align = "left"> <b> The address of the sender: </b> </td> <td align = "left" width = "50"> <input type=text name = "From" size = "40" value = ""> </td> </tr> <tr> <td align = "left"> <b> A subject: </b> </td> <td align = "left"> <input type=text name = "Subject" size = "40"> </td> </tr> <tr> <td align = "left"> <b> Choose the list: </b> </td> <td align = "left"> <select name = "List" size=1> <? $groups = file ("data/lists.txt"); for ($index=0; $index <count ($groups); $index ++) { $grouplist = split ("|", chop ($groups [$index])); ?> <option value = " <? print $grouplist [1]?> " <? if ($index == 0) {print "selected";} ?> > <? print $grouplist [0] ?> </select> </td> </tr> <td align = "left"> <b> The text of the message: </b> </td> <td align = "left"> <textarea cols=50 rows=10 name = "Body"> </textarea> </td> </tr> </table> <input type = "submit" name = "Submit" value = "To send"> </form> <a href = "newlist.php"> Creation of a new sheet of a mailing list. </a> <br> <a href = "addnames.php"> To add addresses in the list </a>. <br> <a href = "picklist.php"> Updating / removal of </a addresses. <br> <a href = "data/log.txt"> Viewing of magazine </a>. <br> <a href = "autoresponder.php"> Viewing / editing of an answering machine </a>. </body> </html> Most likely, you have paid attention, that the above mentioned text in the most part represents a usual HTML-code. Exception is made with the lines limited to brackets. The text between them also is commands of language PHP. Let's consider hardly more in detail, they carry out which problem . Basically, all is simple enough. In a variable $groups contents of a file lists.txt, taking place in the catalogue data/are located. Then in a cycle there is a division every line on fields for what expression is used $grouplist = split ("|", chop ($groups [$index])); The set is formed of the received names of lists and corresponding files tegov option an element of input select (the result is seen in figure 1). Function chop is used for removal{distance} of repeating blanks and a symbol of a new line. For simplification of job you can register directly in a file the text in an element of input of the address of the sender used by default. It you will relieve yourselves of necessity to enter each time the same address. So, after pressing button " Send " management together with all entered data is passed a script with a name sendemail.php. The code which is carrying out dispatch of messages, is simple enough and submitted below: <html> <head> <title> Sending of mail... </title> </head> <body> <? $addresses = file (" data / $ List "); for ($index=0; $index <count ($addresses); $index ++) { mail (" $addresses [$index] ", "$Subject", "$Body", " From: $FromnReply-To: $From "); } $myfile = fopen ("data/log.txt", "a"); fputs ($myfile, $Subject. "t" .date (" dS of F Y h:i:s A ") ". t ". $List. "n"); fclose ($myfile); ?> Your message is sent. <br> <br> <a href = "index.php"> On main </a>. </body> </html> The given script uses some rather useful functions. The first - mail (), one of the most powerful and at the same time simple functions in use, included PHP. At job of the Web-server under the control of operational system UNIX at performance of function mail () it is by default used sendmail, however adjustment PHP can be changed, and for sending mail any other mechanism specified in corresponding section of a file php.ini can be used. Function mail () is described as follows: mail (string to, string subject, string message, string [additional_headers]); As value for parameter additional_headers you can specify that information which count necessary, including such parameters, as X-Mailer, Reply-To, CC:, Bcc, Mime-Version, also you can generate own heading. For reception of the list e-mail addresses from a corresponding file function file () is used. The name of a file is passed as parameter by a call of a script of data processing of the form. Then dispatch of the message in a cycle to all addresses is carried out. We add addresses in a mailing list The following question which I would like to consider in given clause{article}, - the mechanism of addition of new addresses in this or that mailing list. For maintenance of such opportunity it is necessary for us to create a PHP-file with the form of input of addresses, and also a script processing this information. First I shall result the text of a file addnames.php which result of job is submitted in figure 2. <html> <head> <title> Addition of the address in a mailing list </title> </head> <body> <center> <H2> Addition of the address in a mailing list </H2> </center> <form method=post action = "sevemail.php"> <table> <tr> <td align = "left"> <b> Choose the list: </b> </td> <td align = "left"> <select name = "List" size=1> <? $groups = file ("data/lists.txt"); for ($index=0; $index <count ($groups); $index ++) { $grouplist = split ("|", chop ($groups [$index])); ?> <option value = " <? print $grouplist [1]?> " <? if ($index == 0) {print "selected";} ?> > <? print $grouplist [0] ?> </option> <? } ?> </select> </td> </tr> <tr> <td align = "left"> <b> Added e-mail: </b> </td> <td align = "left"> <input type=text name = "Email" size = "40"> </td </tr> </table> <input type = "submit" name = "Submit" value = " To add the address "> </form> <a href = "newlist.php"> Creation of a new sheet of a mailing list. </a> <br> <a href = "picklist.php"> Updating / removal of </a addresses. <br> <a href = "data/log.txt"> Viewing of magazine </a>. <br> <a href = "autoresponder.php"> Viewing / editing of an answering machine </a>. </body> </html> Unique difference of the given file from index.php is absence of fields of input, characteristic only for dispatch of letters, and also a name of a script to which management after pressing the button " is passed To add the address ". The text of a script savemail.php below is resulted. <html> <head> <title> we Write down a file... </title> </head> <body> <br> <br> <? if (file_exists (" data / $ List ")) { $myfile = file (" data / $ List "); $fh = fopen (" data / $ List ", "w"); for ($index=0; $index <count ($myfile); $index ++) { if ($Email! = chop ($myfile [$index])) {fputs ($fh, $myfile [$index]);} } fputs ($fh, $Email. "n"); fclose ($myfile); } else { $myfile = fopen (" data / $ List ", "w"); fputs ($myfile, $Email. "n"); fclose ($myfile); } ?> <br> <? echo $Email?> it is written down in <? echo $List?> <br> <br> <a href = "index.php"> On main </a>. </body> </html> Let's consider problems{tasks} which performance the given script should provide. First, check of existence of a file with the list of addresses. It is done{made} with the help of function file_exists. If the file with the specified name does not exist, it means, that the address added by us - the first in the list. In this case simply we create a new file and it is written down in him e-mail-adres. Otherwise after reading already generated list in a file it is necessary to add transferred{handed} of the form e-mail the address, however before it is necessary to see, whether he is entered earlier since any of addresses should not be duplicated. It, in particular, can be carried out in a cycle on all elements of a file. In this case the file with the list will be open on reading, and in him all addresses which are not conterminous with again written down will be written down. Pay attention, that function of opening of a file fopen as the second parameter uses value "w". It means, that the file opens for recording in him of the data. Value "r" would mean opening a file on reading, "a" - for addition of the information to the existing data. The format of a call of function fopen looks as follows: fopen (filename, mode) Pay attention to the following nuance: if you are going to to open a file simultaneously for reading and recording, you should provide an opportunity of incorrect end of job of a script. After opening a file it is necessary to read all his contents in a file and all updatings to carry out with the data in memory. Only after processing all data it is possible to open again a file, already on recording, and to place in him the modified information. The mechanism of addition considered by us e-mail addresses in many respects neidealen. For example, instead of one address it is possible to provide input at once several. For this purpose it is enough to change only the description of the form in a file addnames.php, and also it is a little podkorrektirovat` a code of processing transmitted in savemail.php the data. Besides it is possible to carry out check of a correctness of input e-mail addresses on some pattern (for example, presence of a symbol inside a line). I shall not go deep into details since it can borrow{occupy} a lot of time as my purpose at present is demonstration of opportunities of language PHP, and also that a problem of creation of such powerful mechanism as the control system of lists of dispatches, is not too complex . However I suggest to proceed{pass} to the following stage - to creation of new mailing lists. As well as in the previous case, for this purpose it is necessary to create two files, first of which will contain the form of input of the initial data, and the second will process these data. Let's name these files newlist.php and makenewlist.php accordingly. newlist.php: <html> <head> <title> Creation of a new mailing list </title> </head> <body> <center> <H2> Creation of the new list </H2> </center> <form method=post action = "make-newlist.php"> <table> <tr> <td align = "left"> <b> The name of the list: </b> </td> <td align = "left"> <input type=text name = "Listname" size = "40"> </td> </tr> <tr> <td align = "left"> <b> The description (one word): </b> </td> <td align = "left"> <input type=text name = "Filename" size = "40"> </td> </tr> </table> <input type = "submit" name = "Submit" value = "To write down"> </form> <br> <a href = "addnames.php"> To add addresses in the list </a>. <br> <a href = "picklist.php"> Updating / removal of </a addresses. <br> <a href = "data/log.txt"> Viewing of magazine </a>. <br> <a href = "autoresponder.php"> Viewing / editing of an answering machine </a>. </body> </html> makenewlist.php: <html> <head> <title> Creation of a file... </title> </head> <body> <? $Filename = $Filename. ".lst "; $myfile = fopen ("data/lists.txt", "a"); fputs ($myfile, $Listname. "|". $ Filename. "n"); fclose ($myfile); ?> The mailing list <is created? echo $Listname?>. <br> <br> <a href = "index.php"> On main </a>. </body> </html> It is easy to notice, that the script makenew-list.php carries out only one function - addition of the name of the list and a name of a corresponding file in lists.txt. Editing of addresses For editing e-mail addresses of a mailing list as simple and, nevertheless, the effective decision it is possible to use direct editing of the text of a corresponding file. First of all we should have form in which we shall choose an edited mailing list. The text of a corresponding file is below resulted. Apparently, he not so that differs from the previous files with forms and is resulted here extremely with the purpose of demonstration of simplicity of development of such type of forms (for those readers who for the present is not the professional in php-programming). <html> <head> <title> Editing of </title </head addresses <body> <center> <H2> Editing of </H2 </center addresses <form method=post action = "editnames.php"> <table> <td align = "left"> <b> Choose the list: </b> </td> <td align = "left"> <select name = "List" size=1> <? $groups = file ("data/lists.txt"); for ($index=0; $index <count ($groups); $index ++) { $grouplist = split ("|", chop ($groups [$index])); ?> <option value = " <? print $grouplist [1]?> " <? if ($index == 0) {print "selected";} ?> > <? print $grouplist [0] ?> </option> <? } ?> </select> </td> </tr> </table> <input type = "submit" name = "Submit" value = "To edit"> </form> <a href = "newlist.php"> Creation of a new sheet of a mailing list. </a> <br> <a href = "addnames.php"> To add addresses in the list </a>. <br> <a href = "data/log.txt"> Viewing of magazine </a>. <br> <a href = "autoresponder.php"> Viewing / editing of an answering machine </a>. </body> </html> Now, when the first form is created, it is necessary to write a script deducing{removing} on the screen the maintenance{contents} of a mailing list. For this purpose we read contents of the file corresponding to the chosen sheet, and then we place his contents in an element of input such as textarea: <html> <head> <title> Editing of </title </head <body addresses <form method=post action = "writenamefile.php"> <br> We edit <? echo $List?>. <br> <br> Edit existing addresses or exclude from the list, having removed all the line long. <b> Empty lines are not supposed! </b> <br> <br> <textarea cols=50 rows=20 name = "Body"> <? if (file_exists (" data / $ List ")) {readfile (" data / $ List ");} ?> </textarea> <br> <br> <input type = "hidden" name = "List" value = " <? echo $List?> "> <input type = "submit" name = "submit" value = "To save"> </FORM> <br> <a href = "index.php"> On main </a>. </body> </html> Pay attention to how we pass in a script writenamefile.php a name of a file with the list of addresses: <input type = "hidden" name = "List" value = " <? echo $List?> "> I want to mention function which we used for transfer of contents of a file directly in the form also: readfile (). In the given file there are no checks (in particular, on a correctness of addresses). By development of own scripts you could realize such mechanisms with use, for example, JavaScript. The result of job of a script editnames.php is submitted in figure 3. The following step will be development of the script responsible for recording of the edited data in a file. Here all is very simple: we obtain the data from an element of input such as textarea (a variable $Body) and it is written down his contents in a file: <html> <head> <title> we Save changes... </title> </head> <body> <br> <br> Changes are saved in the list <? echo $List?>. <br> <? $myfile = fopen (" data / $ List ", "w"); fputs ($myfile, $Body); fclose ($myfile); ?> <br> <a href = "index.php"> On main </a> </body> </html> Answering machine Within the framework of our today's conversation as answering machines we shall understand the mechanism, sending to the user on email the message after end of some action. For example, this event can be a subscription of the visitor to dispatch. For editing the text of the message sent by a script - avtootchetchikom, the same technology can be used, as at editing the list of addresses. First, we create the form for editing the message: autoresponder.php: <html> <head> <title> Adjustment of an answering machine </title> </head> <body> <form method=post action = "writeautoresponder.php"> <br> The automatic message, sent is below submitted to the users who have subscribed on dispatch for managers. <br> <br> <textarea cols=70 rows=20 name = "Body"> <? readfile("data/autoresponder.txt ");?> </textarea> <br> <br> <input type = "submit" name = "submit" value = " To save changes "> </FORM> <br> </body> </html> Second, we write the script saving in the corresponding file (autoresponder.txt) changes made by the manager. writeautoresponder.php <html> <head> <title> we Save changes... </title> </head> <body> <br> <br> <b> The following message used by an answering machine is saved: </b> <br> <? $myfile = fopen ("data/autoresponder.txt", "w"); fputs ($myfile, $Body); fclose ($myfile); ?> <br> <pre> <? echo $Body?> </pre> <br> <br> <a href = "index.php"> On main </a> Now it is necessary to provide an opportunity of a subscription of visitors to a mailing list. For this purpose we shall create a HTML-file approximately with the following maintenance{contents}: <b> To receive news of the server to managers on e-mail: </b> <br> <form method = "post" action = "thanks.php"> <input type = "text" name = "Email" size = "20"> <input type = "hidden" name = "List" value = "admins.txt"> <input type = "submit" Value = "To subscribe"> </form> The result of the reference{manipulation} to this file from a window of a browser is submitted on fig. 4. The last for today will be a script of a premise{room} new e-mail-adresa in a corresponding sheet of dispatch. In our case the file of a sheet has a name "admins.txt" (pay attention to latent parameter List). The code of a script thanks.php is resulted below: <html> <head> <title> Thanks! </title> </head> <body> <? $Body = readfile ("data/autoresponder.txt"); mail ("$Email", " the Subscription to dispatch ", "$Body", " From: MenReply-To: me@myaddress.com "); $myfile = file ("data/admins.lst"); $fh = fopen ("data/admins.lst", "w"); for ($index=0; $index <count ($myfile); $index ++) { if ($Email! = chop ($myfile [$index])) {fputs ($fh, $myfile [$index]);} } fputs ($fh, $Email. "n"); fclose ($fh); ?> Thanks for that have subscribed for our dispatch! <br> <br> </body> </html> That's all. Now, I hope, at you the opportunity to develop own mailing lists has appeared. The conclusion Considered by us the variant of the organization of lists of dispatches is higher is not ideal. Always there is a necessity something to correct. In particular, at input of the information it would be possible to organize checks of the data with help JavaScript'ov. Besides use as storehouses given not flat files, and databases MySQL can make these scripts more productive, is especial if you assume to create dispatches for a plenty of clients. As it is done{made} simply - instead of commands of recording / reading in a file SQL-searches to a DB are used. I hope, that could show to you power and simplicity of language PHP as even above mentioned scripts, at small upgrade, can be used for completely various problems{tasks}. | |