Although most of my job description involves managing our web site, I often have to write memos telling certain sections of the staff about updates on the site. While some of them are only sent out to 2-3 people, some have to be sent out to 70+. Changing a memo 70 times can be an excruciating task. So, to control my sanity, I wrote a combo javascript/perl script that generates memos. The admin page uses javascript to dynamically add recipients to a list, while the perl script retrieves the list of names from the mySQL database and generates the memos. I used a css property to force a page break after each memo after the document is printed. See it in action
The source code is easily modifyible; it could easily be changed to read from a text database, or be changed into a mass-emailer. Finally, the moment you've all been waiting for, the source code:
memo.shtml The Admin Page
<html> <head> <title>Memo Maker</title> </head> <body> <script> var items = new Array(0); function additem() { var temp = document.combowithtext.addoption.options[document.combowit +htext.addoption.selectedIndex].text; var temp1 = document.combowithtext.text.value; var breakcheck=1; for (i=0; i<items.length; i++) { if (items[i] == temp) breakcheck=0; } if (breakcheck) items[items.length] = temp; temp1=items[0]; for (i=1; i<items.length; i++) { temp1=+","+items[i]; } var y=0; for (i=0; i<document.combowithtext.remove.length+y; i++) { if (typeof items[i] != "undefined" && items[i] != "") { document.combowithtext.remove.options[i-y].text=items[i]; } else if (items[i] == "") { y++; } else { document.combowithtext.remove.options[i-y].text=""; } // else y++; } document.combowithtext.text.value=items; } function addall() { removeall(); for (i=0; i<document.combowithtext.addoption.length; i++) { items[i]=document.combowithtext.addoption.options[i].text; document.combowithtext.text.value+=","+items[i]; } for (i=0; i<document.combowithtext.addoption.length; i++) { document.combowithtext.remove.options[i].text=document.combowithtext +.addoption.options[i].text; } document.combowithtext.text.value=items; } function removeitem() { var temp = document.combowithtext.remove.options[document.combowithte +xt.remove.selectedIndex].text; for (i=0; i<items.length; i++) { if (items[i] == temp) { items[i] = ""; } } var y=0; for (i=0; i<document.combowithtext.remove.length+y; i++) { if (typeof items[i] != "undefined" && items[i] != "") { document.combowithtext.remove.options[i-y].text=items[i]; } else if (items[i] == "") { y++; } else { document.combowithtext.remove.options[i-y].text=""; } // else y++; } document.combowithtext.text.value=items; } function removeall() { items.length=0; document.combowithtext.text.value = ""; for (i=0; i<document.combowithtext.addoption.length; i++) { document.combowithtext.remove.options[i].text=""; } } </script> <form name="combowithtext" action="/cgi-bin/memo/make.pl" method="post +"> <table border="1" cellpadding="3" cellspacing="0"> <tr> <td> Subject: <input type="text" name="subject" size="30"> </td> </tr> <tr> <td> <table border="0" cellpadding="4" cellspacing="0"> <tr> <td> To:<br> <select name="addoption" size="6"> <!--#include virtual="/cgi-bin/memo/fill.pl?blank=0"--> </select> </td> <td> <input type="button" value="ADD -->" onClick="additem()"><br> <input type="button" value="ADD ALL" onClick="addall()"><br> <input type="button" value="<-- REMOVE" onClick="removeitem()"><br> <input type="button" value="REMOVE ALL" onClick="removeall()"> </td> <td> <input type="hidden" name="text"> <select name="remove" size="6"> <!--#include virtual="/cgi-bin/memo/fill.pl?blank=1"--> </select> </td> </tr> </table> </td> </tr> <tr> <td> From: <input type="text" name="from" size="30"> </td> </tr> <tr> <td> <textarea name="body" rows="8" cols="80"> The body of the message. <!-- after you download, change thetextarea to textarea this was necessary to get the perlmonks preview page to let me submit +this :) --> </thetextarea> </td> </tr> </table><br> <input type="submit" value="Submit"> </form> </body> </html>
fill.pl Fills the drop down select boxes in memo.shtml
#!/usr/bin/perl -w use strict; use CGI; use DBI; my $query = CGI::new(); my $blank = $query->param("blank"); my $dbh = DBI->connect("DBI:mysql:news", "root", ""); my $sth = $dbh->prepare("select * from newspw"); $sth->execute(); print "Content-type: text/html\n\n"; # filled options if (!$blank) { while (my @data = $sth->fetchrow_array()) { my $username=$data[1]; # format name; optional, only here because I have usernames # stored as email addresses in the form: joe_ryan my ($username1)=$username; my ($username2)=$username; $username1 =~ s/([a-z])([a-z]+)_([a-z])([a-z]+)/$1/; $username2 =~ s/([a-z])([a-z]+)_([a-z])([a-z]+)/$3/; $username1 =~ tr/[a-z]/[A-Z]/; $username2 =~ tr/[a-z]/[A-Z]/; $username =~ s/([a-z])([a-z]+)_([a-z])([a-z]+)/$username1$2 $usernam +e2$4/; print "<option>$username</option>\n"; } } # for the blank options else { while (my @data = $sth->fetchrow_array()) { print "<option></option>\n"; } } $dbh = $dbh->disconnect();
make.pl Builds the memo
#!/usr/bin/perl -w use strict; use CGI; my $query = CGI::new(); my $subject = $query->param("subject"); my $text = $query->param("text"); my $body = $query->param("body"); my $from = $query->param("from"); print "Content-type: text/html\n\n"; print "<html><head><title>Memo</title>\n"; print "<style>span { page-break-after: always; }</style>\n"; print "<body bgcolor=\"#FFFFFF\">"; my @splittext = split (/\,/, $text); for (my $i=0; $i<@splittext; $i++) { my $username=$splittext[$i]; # format name; optional, only here because I have usernames # stored as email addresses in the form: joe_ryan my ($username1)=$username; my ($username2)=$username; $username1 =~ s/([a-z])([a-z]+)_([a-z])([a-z]+)/$1/; $username2 =~ s/([a-z])([a-z]+)_([a-z])([a-z]+)/$3/; $username1 =~ tr/[a-z]/[A-Z]/; $username2 =~ tr/[a-z]/[A-Z]/; $username =~ s/([a-z])([a-z]+)_([a-z])([a-z]+)/$username1$2 $usernam +e2$4/; my $thetime = localtime; my @entry = split(' ', $thetime); my $thedate=$entry[1]." ".$entry[2]." ".$entry[4]; print "<font size=\"+3\">"; print "<b>Subject: $subject</b><br>\n"; print "<b>To: $username</b><br>\n"; print "<b>From: $from</b><br>\n"; print "<b>Date: $thedate</b><br>\n"; print "<br>\n$body\n<br>\n"; print "<br>\n" x 2; print "</font><span></span>"; } print "\n</body></html>";
Edit: Petruchio Sun Sep 2 05:30:22 UTC 2001 - Added READMORE tag.
|
|---|