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
#!"\xampp\perl\bin\perl.exe"

use warnings;
use strict;

my $sendmail ='C:/xampp/sendmail/sendmail.exe';
my $sender = 'myusername@verizon.net';    //Must match sender in config file
my $recipient = 'recipient@example.com'
my $subject = "My Subject";
my $body = "Comments about My Subject";
$ENV{PATH}='';

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.
defaults
logfile "C:\xampp\sendmail\sendmail.log"

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

# Verizon
account Verizon
host outgoing.verizon.net
#from must match sender in email script
from myusername@verizon.net
auth on
user myusername@verizon.net
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
#!"\xampp\perl\bin\perl.exe"
my $mailprog="C:/xampp/sendmail/sendmail";
my $recipient= 'paul@peschoen.com' ;        # use single quotes
open(MAIL, "| $mailprog -oi -t -odq")
    || HTMLdie("Couldn't send the mail (couldn't run $mailprog).") ;
$ENV{'HTTP_REFERER'} || ($ENV{'HTTP_REFERER'}= "www.peschoen.com") ;
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" ;
close(MAIL);


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

Code: Select all
#!"C:\xampp\php\php-cgi.exe"
<?php

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

require_once 'C:/xampp/htdocs/library/HTMLPurifier.auto.php';

$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));
fclose($fHTMLraw);

$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);
fclose($fHTMLpure);

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

<html>
<body>
<iframe src="HTMLfilter.php"> </iframe>
</body>
</html>
EOF
sleep(1);
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:
http://www.elated.com/articles/writing- ... rm-mailer/
http://www.perlmonks.org/?node_id=4684
http://perldoc.perl.org/Net/SMTP.html
http://perldoc.perl.org/perlfaq9.html#H ... nd-mail%3F
PStechPaul
 
Posts: 7
Joined: 22. January 2011 02:38

Re: Send email in Perl script

Postby JonB » 23. January 2011 16:31

FWIW -

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
8)
User avatar
JonB
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
</Directory>

# 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
</IfModule>


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

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


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 154 guests