Home / code / cgi

Sending emails with Perl CGI

To send emails with Perl CGI you first have to install email sever on your server. You can install EMWAC Internet Mail Services and Blat. (Windows)
You can download Blat from http://gepasi.dbs.aber.ac.uk/softw/blat.html

When sending data with email you first have to read and prepare the data.

read(STDI, $data, $ENV{'CONTENT_LENGTH'});
@pair = split(/&/, $data);

foreach $pair (@pair)
{
  ($name, $value)=split(/=/, $pair);
  $value =~ s/\+/ /g;
  $value =~ s/%([a-fA-F0-9] [a-FA-F0-9])/pack("C",hex($1))/eg;
  $form{$name} = $value;
}
In the next step, the data must be assigned to variables. Also the path and the filename of Blat has to be assigned to a variable. Make sure you avoid the meaning of @ with a black slash (\).

$sender="$form{email}";
$receiver="someone\@mydomain.be";
$content="$form{comment}";
$subject="Comment for $form{name}";
$pad="c:/Winnt/system32/blat.exe";
In the third step a text-file is opened (tempfile.txt) and is assigned to the filehandle (DATA). The actual content of the email is written in this file.
open (TEMP, "<tempfile.txt");
print TEMP $content;
close (TEMP);
In the fourth step, the complete command for the call and execute of Blat is assigned to a variable ($program).
$program="$pad tempfile.txt -t \"$receiver\" -s \"subject\" -i \"$sender\"";
In the fifth step a sub-process is started and closed. The mail program Blat is started and send through the Mail program. This is done by the piping symbol |. Piping is used to transport information from 1 process to another.
open (MAIL, "|$program") || die "Cannot open $program: $!\n";
close (MAIL);
The sixth and last step is a confirm page.
print "content-type: text/html\n\n";

print <<end;
<HTML>
<HEAD><TITLE>Thanks</TITLE></HEAD>
<BODY>
<H1>The following data has been send:</H1>
<TABLE width=80% border=1>
end

print "<TR><TD>E-mail sender</TD><TD>$sender</TD></TR>";
print "<TR><TD>E-mail receiver</TD><TD>$receiver</TD></TR>";
print "<TR><TD>Subject</TD><TD>$subject</TD></TR>";
print "<TR><TD>Comment</TD><TD>$content</TD></TR>";

print <<end;
</TABLE>
</BODY>
</HTML>
end

 

So everything together is:
read(STDI, $data, $ENV{'CONTENT_LENGTH'});
@pair = split(/&/, $data);

foreach $pair (@pair)
{
  ($name, $value)=split(/=/, $pair);
  $value =~ s/\+/ /g;
  $value =~ s/%([a-fA-F0-9] [a-FA-F0-9])/pack("C",hex($1))/eg;
  $form{$name} = $value;
}

$sender="$form{email}";
$receiver="someone\@mydomain.be";
$content="$form{comment}";
$subject="Comment for $form{name}";
$pad="c:/Winnt/system32/blat.exe";

open (TEMP, "<tempfile.txt");
print TEMP $content;
close (TEMP);

open (MAIL, "|$program") || die "Cannot open $program: $!\n";
close (MAIL);

print "content-type: text/html\n\n";

print <<end;
<HTML>
<HEAD><TITLE>Thanks</TITLE></HEAD>
<BODY>
<H1>The following data has been send:</H1>
<TABLE width=80% border=1>
end

print "<TR><TD>E-mail sender</TD><TD>$sender</TD></TR>";
print "<TR><TD>E-mail receiver</TD><TD>$receiver</TD></TR>";
print "<TR><TD>Subject</TD><TD>$subject</TD></TR>";
print "<TR><TD>Comment</TD><TD>$content</TD></TR>";

print <<end;
</TABLE>
</BODY>
</HTML>
end

 

TOP

Latest script:

 

Books: