#!/usr/local/bin/perl -w # FILE: webMailer.pl # AUTHOR: Diana Coppenbarger # DATE: April 3, 2003 (Created: April 3, 2003) # PURPOSE: Send e-mail from the web page # $Id: webMailer.pl 60647 2019-08-05 16:03:29Z dcop $ LLE/SDG # Look in current lib directory first (this is helpful if we're # running in the development directory and want to pick-up # development source), then in the installed lib directory. use lib qw( . /htpub/perlib /oudvmt/perllib ); use DButils; use strict; use Getopt::Std; use CGI::Pretty qw/:all/; use LWP::Simple; sub usage { print STDERR "Version: 1.15 - Send e-mail from the web page.\n"; print STDERR "Usage: webMailer [] [];\n"; print STDERR "-\@: this message\n"; exit(1); } main(); sub main { usage() if (@ARGV && $ARGV[0] eq '-@'); #usage() unless ( getopts('') ); my $type = param('type'); my $name = param('to'); my $cc = param('cc'); my $subj = param('subject'); my $msg = param('message'); my $from = param('from'); # if($msg ne ""){ # print STDERR "webMailer: msg = $msg\n"; # } my $templateHost = $ENV{'HTTP_HOST'}; $templateHost = "www.lle.rochester.edu"; # Change any AND in $name to & my $pos; if(($pos = index($name," AND ")) > -1){ substr($name,$pos+1,3) = "&"; } print header, get("http://$templateHost/assets/templates/header.php?title=Send%20E-Mail&menu=none&relative=false&styles[]=/assets/css/webMailer.css"), h2("   Send a Message"),br,br; # Check for valid "to" entry. Must be in our database $ENV{DATABASE}='lleweb'; my @names = split ',', $name; my $ptest; my $dbname; my $enames = ''; for(my $i = 0; $i <= $#names; $i++){ $ptest = $names[$i]; if($ptest ne "5853136636\@vtext.com"){ if(!($ptest =~ /^[A-Za-z0-9_ \-\/\&\x27\x2e]+$/)){ print center(font({-color => "red"}, h3("Invalid 'mail to' address"))), get("http://$templateHost/assets/templates/footer.php"); print STDERR "webMailer: Invalid 'mail to' address '$names[$i]'\n"; exit; } # Check the database $dbname = findEmail($names[$i]); if($dbname eq "unknown"){ # wasn't a lookup name. Check to see if it's already an email address if(checkEmail($names[$i]) == 0){ print center(font({-color => "red"}, h3("Invalid 'mail to' address"))), get("http://$templateHost/assets/templates/footer.php"); print STDERR "webMailer: Invalid 'mail to' address '$names[$i]'\n"; exit; } } else{ $ptest = $dbname; } } # if it gets here, it's a good name if(index($ptest,"\@") < 0){ $ptest .= "\@lle.rochester.edu"; } $enames .= "$ptest "; } if(!$enames){ print center(font({-color => "red"}, h3("Invalid 'mail to' address"))), get("http://$templateHost/assets/templates/footer.php"); print STDERR "webMailer: Unable to find e-mail address(es)\n"; exit; } chop $enames; if (!$type || !$msg || !$from || index($from,",") >= 0){ if ($type){ if(!$from){ print center(h3("Please supply a return e-mail address")),br; } elsif(index($from,",") >= 0 ){ print center(h3("Only one return address can be specified")),br; } } # Create the window printMailForm($name); exit(0); } my $status = "Successfully sent message"; # Go through and find the list of e-mails based on the list of names # Make sure the "from" is in the right email format. my $fename = ''; if(index($from,"\@") >= 0 && index($from, ".") >= 0){ # good format. $fename = $from; } elsif(index($from," ") < 0){ # no space. assume LLE address. if(checkEmail($from) == 0){ print STDERR "webMailer: Invalid return address: $from\n"; print center(font({-color => "red"}, h3("Invalid return address. Please supply a return address in the form of user\@domain."))); printMailForm($name); exit(0); } $fename = "$from\@lle.rochester.edu"; } else{ # Check the database in case they used their real name $fename = findEmail($from); if($fename eq "unknown"){ print STDERR "webMailer: Invalid return address: $from\n"; print center(font({-color => "red"}, h3("Invalid return address. Please supply a return address in the form of user\@domain."))); printMailForm($name); exit(0); } } # get rid of the last comma and space. print STDERR "webMailer: Sending message to '$enames'\n"; # Check the CC field also my $ccnames = ''; @names = split ',', $cc; for(my $i = 0; $i < $#names; $i++){ if(index($names[$i],"\@") >= 0 && index($names[$i], ".") >= 0){ # already in the right form for an email. $ccnames .= "$names[$i], "; } elsif(index($names[$i]," ") < 0){ # no space assumes LLE email address. my $chk = "$names[$i]\@lle.rochester.edu"; if(checkEmail($chk) != 0){ $ccnames .= "$names[$i], "; } } else{ $dbname = findEmail($names[$i]); if(index($dbname, "unknown") < 0){ $ccnames .= "$dbname, "; } } } local *PH; my $cmd = "| /bin/mailx -s \"$subj\" -r \"$fename\" "; if($ccnames){ # get rid of the last comma and space. chop $ccnames; chop $ccnames; $cmd .= "-c \"$ccnames\" "; } $cmd .= "$enames"; print STDERR "webMailer: $cmd: $msg\n"; if (open PH, $cmd){ print PH "$msg\n\n-----------\nThis message was sent using LLE WebMailer\n"; close PH; } else{ $status = "Unable to send message"; print STDERR "webMailer: Unable to open pipe to mailx\n"; } if (substr($status,0,7) eq "Success"){ print center(font({-color => "green"}, h3("$status"))); } else{ print center(font({-color => "red"}, h3("$status"))); } print center(a({-href => "javascript:window.close()"}, "Close Window")), get("http://$templateHost/assets/templates/footer.php"); } sub printMailForm { my $name = shift; my $templateHost = $ENV{'HTTP_HOST'}; # Javascript for word counter if(index($name,"vtext.com") >= 0){ # Verizon has a limit to its characters } print " "; # Create the window print start_form(-name => "myForm", -method =>'POST', -action => '/cgi-script/webMailer'); if(index($name,"vtext.com") < 0){ print center(hidden(-name => 'to', -value => '$name'),"\n", div({-id => "webMailer"}, div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Mail to:")). div({-id => "rightDiv"}, $name)). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Return Address:")). div({-id => "rightDiv"}, textfield(-name => 'from', -size => 50)). div({-id => "extraDiv"}, i("(One address only)"))). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "CC:")). div({-id => "rightDiv"}, textfield(-name => 'cc', -size => 50)). div({-id => "extraDiv"}, i("(optional)"))). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Subject:")). div({-id => "rightDiv"}, textfield(-name => 'subject', -size => 50))). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Message:"))). div({-id => "horizDiv"}, textarea(-id => "mailMessage", -name => 'message',-rows => 20,-cols => 75, -wrap => 1).br. submit(-name => 'type', -value => 'Submit Message')))); } else{ print center(hidden(-name => 'to', -value => '$name'),"\n", div({-id => "webMailer"}, div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Mail to:")). div({-id => "rightDiv"}, $name)). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Return Address:")). div({-id => "rightDiv"}, textfield(-name => 'from', -size => 50)). div({-id => "extraDiv"}, i("(One address only)"))). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "CC:")). div({-id => "rightDiv"}, textfield(-name => 'cc', -size => 50)). div({-id => "extraDiv"}, i("(optional)"))). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Subject:")). div({-id => "rightDiv"}, textfield(-name => 'subject', -size => 50))). div({-id => "horizDiv"}, div({-id => "leftDiv"}, h3({-style => "text-align: left"}, "Message:"))). div({-id => "horizDiv"}, textarea(-id => "mailMessage", -name => 'message',-rows => 20,-cols => 75, -wrap => 1, -onKeyDown => "textCounter(document.myForm.message,document.myForm.remLen,160)", -onKeyUp => "textCounter(document.myForm.message,document.myForm.remLen,160)").br. textfield(-readonly=>'1', -name => "remLen", -size => "3", -maxlength => "3", -value => "160"). "characters left".br.submit(-name => 'type', -value => 'Submit Message')))); } print end_form,br; print get("http://$templateHost/assets/templates/footer.php"); } sub findEmail { my $name = shift; my $ename = "unknown"; my $pos; openDB(); # Check for an apostrophe my $namecmp = $name; if(($pos = index($namecmp,"'")) != -1){ # there is an apostrophe # Add another substr($namecmp,$pos,0) = "'"; } my (@names) = split ' ', $namecmp; my $lname = pop @names; my $fname = join ' ',@names; my $sql = "SELECT email \ FROM web_email \ WHERE link = '$namecmp' \ UNION \ SELECT email \ FROM phonelist \ WHERE lastname = '$lname' \ AND firstname = '$fname' \ UNION \ SELECT '$namecmp\@' || text_address \ FROM phonelist, cellular_carriers \ WHERE llecoipager = '$namecmp' AND lle_issued_pager = carrier_code"; my ($rows, $data); eval { ($rows, $data) = execSelect(\$sql); }; if($@){ print STDERR "webMailer: $@"; } elsif($rows != 0){ $ename = "$data->[0]->[0]"; } closeDB(); return $ename; } sub checkEmail { my $name = shift; openDB(); # Check for an apostrophe my $namecmp = $name; my $pos; if(($pos = index($namecmp,"'")) != -1){ # there is an apostrophe # Add another substr($namecmp,$pos,0) = "'"; } my $fname = "$namecmp\@lle.rochester.edu"; my $sql = "SELECT email \ FROM web_email \ WHERE link = '$namecmp' \ UNION \ SELECT email \ FROM phonelist \ WHERE email = '$fname' \ UNION \ SELECT llecoipager \ FROM phonelist, cellular_carriers \ WHERE llecoipager = '$namecmp' AND lle_issued_pager = carrier_code"; my ($rows, $data); eval { ($rows, $data) = execSelect(\$sql); }; if($@){ print STDERR "webMailer: $@"; } closeDB(); return $rows; }