Link to home
Start Free TrialLog in
Avatar of niico
niico

asked on

CGI email send form

I have seen CGI scripts that let you send email from a web page, but they are mainly aimed at UNIX sendmail.

The webpage is running on NT4 and I need to send email from a page using CGI and blat

Bear in mind I know very little about perl or CGI, so go slow please.

If the script can be applied to UNIX as well (with minor changes) - then even better
Avatar of maneshr
maneshr

here are 2 programs that allows you to send mail with Blat.

the first one - blat1.pl - can be used directly and is independent by itself.

the second one - blat2.pl - is more modular and the entire call to blat is within a sub-routine. in order to use the sub routine you will have to call it from another PERL script (call_blat2.pl)

======================blat1.pl

############################################
# This program uses blat.exe to send a to a list of addresses #
# And returns a confirmation HTML page                        #
# Program variable explanation ################################

# $recipients = list of email address to send message;
# $ccaddress = carbon copy address;
# $knownsender = a 'From' address;
# $fromsender = from sender known to STMP server;
# $subject = subject line of message;
# $message = name of message file to send;
# $blatpath = full path name to execute blat;
# $server = server name if different than installed one;

# Define variables ###########################################

$recipients = "name1\@company1.com, name2\@company2.com, name3\@company3.com";
$ccaddress = "foo\@bar.com";
$knownsender = "foo\@bar.com";
$fromsender = "foo\@bar.com";
$subject = "sending testblat.txt";
$message = "testblat.txt ";
$blatpath = "blat.exe ";

# print an html-complient execution confirmation page ########

print "Content-Type: text/html\n\n"; # add header for HTML document
print "<html>\n";
print "<head><title> $input{'type'} Search Results </title></head>\n";
print "<body BACKGROUND=\"images/tile.gif\">\n";        
print "<hr>\n";

print "An E-mail message is about to be sent by blat.exe\:\n";
print "<PRE><P>\n";
print "To\:      $recipients \n";
print "Subject\: $subject \n";
print "File\:    $message \n";
print "<P>\n";
print "</PRE><P>\n";

# Get message ready     ###########################

$commandline = $blatpath;
$commandline .= $message;
$commandline .= "-s \"$subject\" " if $subject;
$commandline .= "-t \"$recipients\" " if $recipients;
$commandline .= "-f $fromsender " if $fromsender;
$commandline .= "-c $ccaddress " if $ccaddress;

print "\$commandline \= $commandline";
print "<P>\n";
print "<P>\n";
print "<P>\n";

# Send mail using blat and the system command ############

system($commandline);
print "<P>\n";
print "<P>\n";
print "Blat has been executed.  <P>\n";


# Finish printing html-complient execution confirmation page####

print "<P>\n";
print "<P>\n";
print "<P>\n";
print "</body>\n";
print "</HTML>\n";

=========blat2.pl
#
#            Sending mail with Blat (Win 95/NT)
#
# Can send file or text or file+text (if both $file and $body ne "")
#
# Call as:
# ($errcode,$output)=
# &mail_blat($blat_prog, $server, $from, $to,
# $subject, $file, $body, $temp_files_path);
#
# Returns error-code(correct in Blat 1.5 only) and Blat-output
#
sub mail_blat {

local ($blat_prog, $SMTP_host, $from, $to, $subj,
       $file, $body, $temp_files_path) = @_;
local ($temp_file, $out, $filebody);

if (!$body){
   return 4,"Both body/file sources are empty in " .
   __FILE__." at line ".__LINE__."\n" if !$file;

   $temp_file=$file;
}
else {  

   # create temp-file's name
   BEGIN{ srand $$.time }
   do {$temp_file  = "$temp_files_path/".
  int (rand(1000000)).".file"} until (!-e $temp_file);

   # file+body request
   if ($file) {
  open (IN,"$file") ||
     return 1,"Can not read file $file in " .
     __FILE__." at line ".__LINE__."\n";
  $filebody = join "",<IN>;
  close IN;
   }
   # write message's body to temp-file
   open (TEMP, ">$temp_file") ||
     return 2,"Can not write temp-file $file in " .
     __FILE__." at line ".__LINE__."\n";
   print TEMP "$filebody\n$body";
   close TEMP;
} # end of else

$out=`$blat_prog $temp_file -s \"$subj\" -f $from -t $to -server
$SMTP_host`;

# remove temp-file
unlink $temp_file if ($temp_file ne $file);

# workaround syntax error that returns 0
$? = 3 if(!$? && $out=~/\nsyntax:\n/);

# return error-code and Blat-output
return $?,$out;
}

1;

=========callblat2.pl

require "blat2.pl";

($errcode,$output)= &mail_blat("blat.exe", "smtp.mailserver.com", "me\@xyz.com", "you\@abc.net", "testing", "/temp/test.txt", "Hi", "/temp");

print "$output\n";


Hope that helps
Avatar of niico

ASKER

OK Ive implemented the first script and I get the following message in the browser (and no email is sent):



An E-mail message is about to be sent by blat.exe:

To:      ndonnelly@cwcom.net, jack@rabbit.com
Subject: sending testblat.txt
File:    testblat.txt  


$commandline = blat.exe testblat.txt -s "sending testblat.txt" -t "ndonnelly@cwcom.net, jack@rabbit.com"





Blat has been executed.
Avatar of niico

ASKER

ok Ive fixed those problems and got the email to send.

What I need to do now though is send information from a webform using blat

Thanks in advance
so from what you are saying, you want a HTML form that will ask for the message & the recipients of the message and then send the message to those recipients, right??

if so, here is the html file...

===blat.html
<form method=post action=blat1.pl>
<B>Enter the receipient(s) Email id</B>
<input type=text name=rec>

<P>
<B>Enter your message here<BR>
<textarea name="message" rows=7 cols="40"></textarea>
<P>
<input type=submit value="Email Now!!">
</FORM>

Also you will have to make some changes in blat1.pl to accomodate for these changes. here is the modified blat1.pl

i have added comments in the places where i have added/modified code.

==========blat1.pl
############################################
# This program uses blat.exe to send a to a list of addresses #
# And returns a confirmation HTML page                        #
# Program variable explanation ################################

# $recipients = list of email address to send message;
# $ccaddress = carbon copy address;
# $knownsender = a 'From' address;
# $fromsender = from sender known to STMP server;
# $subject = subject line of message;
# $message = name of message file to send;
# $blatpath = full path name to execute blat;
# $server = server name if different than installed one;

## PERL module to read the HTML form variables into PERL
use CGI;
$query=new CGI;

# Define variables ###########################################
## Read the HTML form variable into PERL
$recipients = $query->param('rec');
$message=$query->param('message');

$ccaddress = "foo\@bar.com";
$knownsender = "foo\@bar.com";
$fromsender = "foo\@bar.com";
$subject = "sending testblat.txt";
$message_file = "testblat.txt ";
$blatpath = "blat.exe ";

# print an html-complient execution confirmation page ########

print "Content-Type: text/html\n\n"; # add header for HTML document
print "<html>\n";
print "<head><title> $input{'type'} Search Results </title></head>\n";
print "<body BACKGROUND=\"images/tile.gif\">\n";        
print "<hr>\n";

## Check if the recipients field is empty. if empty throw an error!!
if ($recipients=~ /^\s+$/ || $recipients=~ /^$/){
  print "Please enter at least one recipient!!<br>\n";
  exit;
}

if ($message=~ /^\s+$/ || $message=~ /^$/){
  print "Please enter some message!!<br>\n";
  exit;
}

## Now write the user entered message to the text file.
## We will be sending this text file across to blat!!
open(MESS,">$message_file") || die $!;
print MESS $message;
close(MESS);

print "An E-mail message is about to be sent by blat.exe\:\n";
print "<PRE><P>\n";
print "To\:      $recipients \n";
print "Subject\: $subject \n";
print "File\:    $message_file \n";
print "<P>\n";
print "</PRE><P>\n";


# Get message ready     ###########################

$commandline = $blatpath;
$commandline .= $message_file;
$commandline .= "-s \"$subject\" " if $subject;
$commandline .= "-t \"$recipients\" " if $recipients;
$commandline .= "-f $fromsender " if $fromsender;
$commandline .= "-c $ccaddress " if $ccaddress;

print "\$commandline \= $commandline";
print "<P>\n";
print "<P>\n";
print "<P>\n";

# Send mail using blat and the system command ############

system($commandline);
print "<P>\n";
print "<P>\n";
print "Blat has been executed.  <P>\n";


# Finish printing html-complient execution confirmation page####

print "<P>\n";
print "<P>\n";
print "<P>\n";
print "</body>\n";
print "</HTML>\n";
Avatar of niico

ASKER

not quite

just like a web feedback form - user fills in a load of details and when they click send all of the info is sent to a fixed email address
ASKER CERTIFIED SOLUTION
Avatar of maneshr
maneshr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is there anything additional that you were looking for in your answer??

let me know.