Send email in Perl script

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Send email in Perl script

Postby PStechPaul » 23. January 2011 04:33

This has been a nightmare :shock: , but I have finally had success :D with this and other problems with using the cgi-bin folder. I'll try to break it down. Here is a simple Perl script that sends mail:

Code: Select all

use warnings;
use strict;

my $sendmail ='C:/xampp/sendmail/sendmail.exe';
my $sender = '';    //Must match sender in config file
my $recipient = ''
my $subject = "My Subject";
my $body = "Comments about My Subject";

open ( MAIL, "| $sendmail -t" ) or die $!;
print MAIL "From: $sender\n";
print MAIL "To: $recipient\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$body\n";
print MAIL "\n.\n";
close ( MAIL );

print "<html><p>Sent Mail</p></html>";

The important thing here is that the shebang must be as shown, but only (I think) in the cgi-bin folder. But to use this method, you must also edit the "sendmail.ini" text file in the C:\xampp\sendmail folder:

Code: Select all
# Example for a user configuration file

# Set default values for all following accounts.
logfile "C:\xampp\sendmail\sendmail.log"

# Mercury
account Mercury
host localhost
from postmaster@localhost
auth off

# Verizon
account Verizon
#from must match sender in email script
auth on
password ********

# Set a default account
account default : Verizon

Unfortunately, I have found that this works properly from the command line, but using a browser there is an error "premature end of headers". It also pops up a command window when the mail program runs. In either case, however, the mail is sent. I think the command line invokes the perl from the ActivePerl installation. The sendmail application, however, is 'C:/xampp/sendmail/sendmail.exe'.

A quick update: I think I've got it licked, finally. There were a combination of problems that had to be understood and resolved, but now it seems to be OK both on the command line and using the IE8 browser. Here is the basic setup for the Perl script mail:

Code: Select all
my $mailprog="C:/xampp/sendmail/sendmail";
my $recipient= '' ;        # use single quotes
open(MAIL, "| $mailprog -oi -t -odq")
    || HTMLdie("Couldn't send the mail (couldn't run $mailprog).") ;
print MAIL "To: $recipient\n";
print MAIL "From: $in{'Email'}\n",
           "Subject: Form data from $in{'Full_Name'}\n\n",
           "The following data was entered at $ENV{'HTTP_REFERER'}:\n\n" ;

And the PHP script that I call from this to purify the raw HTML is as follows:

Code: Select all

//  HTMLfilter.php - PES - January 20, 2011
//  This converts raw HTML to purified (safe) HTML
//  Called from
//  Read Raw.htm, Write to Pure.htm

require_once 'C:/xampp/htdocs/library/';

$config = HTMLPurifier_Config::createDefault();

// configuration goes here:
$config->set('Core.Encoding', 'UTF-8'); // replace with your encoding
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional'); // replace with your doctype

$purifier = new HTMLPurifier($config);

// untrusted input HTML read from "Raw.htm"
$fHTMLrawfile = "Raw.htm";
$fHTMLraw = fopen($fHTMLrawfile, 'r') or die("can't open file");
$html = fread($fHTMLraw, filesize($fHTMLrawfile));

$pure_html = $purifier->purify($html);

// write purified HTML to "Pure.htm"
$fHTMLpurefile = "Pure.htm";
$fHTMLpure = fopen($fHTMLpurefile, 'w') or die("can't open file");
fwrite($fHTMLpure, $html);

echo '<pre>' . htmlspecialchars($pure_html) . '</pre>';

// vim: et sw=4 sts=4

It is called from the Perl script as follows:

Code: Select all
my $HTMLrawfile = "Raw.htm";
my $HTMLpurefile = "Pure.htm";

# Write to Raw.htm
open (fHTMLraw, '>', $HTMLrawfile)
    or HTMLdie ("File error: $!");
print fHTMLraw $E_Title;
close fHTMLraw;

#<iframe src="TestPHP.php" frameborder=0 scrolling="no" framespacing=0 marginheight=0 marginwidth=0 width=400 height=40> </iframe>
# NOTE: Content type must be sent to stdin once only
print <<EOF;
Content-type: text/html

<iframe src="HTMLfilter.php"> </iframe>
my $fHTMLpurefile;
if (-e $HTMLpurefile){
  open (fHTMLpure, '<', $HTMLpurefile);
  $E_Title = <fHTMLpure>;
  close fHTMLpure; }
else {
  $E_Title = 'Title HTML error'; }

Here are a few links that describe other ways to use email: ... rm-mailer/ ... nd-mail%3F
Posts: 7
Joined: 22. January 2011 02:38

Re: Send email in Perl script

Postby JonB » 23. January 2011 16:31


this is the shebang line used in my two working Perl forums - I run them in cgi-bin, and they work correctly.

#!/xampp/perl/bin/perl.exe --

Good Luck
User avatar
AF Moderator
Posts: 3210
Joined: 12. April 2010 16:41
Location: Land of the Blazing Sun
Operating System: Windows XP/7 - Fedora 15 1.7.7

Re: Send email in Perl script

Postby PStechPaul » 25. January 2011 22:08

I was able to get my scripts working with the original shebang that also runs on my remote server:
Code: Select all
#!/usr/bin/perl -T

I did so by creating a C:\xampp\usr folder and moving the perl and php folders there. But it seems to work only from the command line and not from the browser using http://localhost. I think it's due to the configuration of the Apache server, which is not used from the command line.

I'm not very familiar with the server config files, but I think it's in C:\xampp\apache\conf:
Code: Select all
ServerRoot "C:/xampp/apache"
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all

# XAMPP specific settings
Include "conf/extra/httpd-xampp.conf"

# Perl settings
Include "conf/extra/perl.conf"

and in httpd-xamp.conf:
Code: Select all
<IfModule env_module>
    SetEnv MIBDIRS "C:/xampp/php/extras/mibs"
    SetEnv MYSQL_HOME "C:\\xampp\\mysql\\bin"
    SetEnv OPENSSL_CONF "C:/xampp/apache/bin/openssl.cnf"
    SetEnv PHP_PEAR_SYSCONF_DIR "C:\\xampp\\php"
    SetEnv PHPRC "C:\\xampp\\php"
    SetEnv TMP "C:\\xampp\\tmp"
    UnsetEnv PERL5LIB

and in perl.conf:
Code: Select all
LoadFile "C:/xampp/perl/bin/perl510.dll"
LoadModule perl_module modules/
LoadModule apreq_module modules/

PerlSwitches -T
PerlPostConfigRequire "C:/xampp/apache/conf/extra/"
Posts: 7
Joined: 22. January 2011 02:38

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 72 guests