Send email in Perl script
Posted: 23. January 2011 04:33
This has been a nightmare , but I have finally had success 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:
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:
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:
And the PHP script that I call from this to purify the raw HTML is as follows:
It is called from the Perl script as follows:
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
- 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