Solved

CGI email send form

Posted on 2000-04-13
7
397 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:niico
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2713838
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
0
 

Author Comment

by:niico
ID: 2714271
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.
0
 

Author Comment

by:niico
ID: 2714447
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
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 16

Expert Comment

by:maneshr
ID: 2716162
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";
0
 

Author Comment

by:niico
ID: 2716194
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
0
 
LVL 16

Accepted Solution

by:
maneshr earned 200 total points
ID: 2716481
then the html file needs some small changes.

remove ...
<B>Enter the receipient(s) Email id</B>
                    <input type=text name=rec>

and also in the perl script, replace....
$recipients = $query->param('rec');

WITH...

$recipients = "fixed\@recipient.com";

this will show you how to send a message to a fixed recipient. if you want you can add more fields to the HTML form and get them in the PERL script and process them in any way you want to!!

Hope that helps.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2730703
is there anything additional that you were looking for in your answer??

let me know.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question