Solved

CGI email send form

Posted on 2000-04-13
7
391 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
is there anything additional that you were looking for in your answer??

let me know.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now