-   Programming (
-   -   Send Mail with Perl (

Cool_Hand_Luke 03-12-2005 09:04 AM

Send Mail with Perl
Ok I am running Mandrake 9.1. I wrote a simple program to test sendmail and it does not work. As soon as I reference sendmail I get a 500 server error. Do I need to install a module for perl? I installed already. Please see the program below.


#!/usr/bin/perl -wT
use strict;
use CGI ':standard';
use warnings;

my $test= $ENV{'HTTP_USER_AGENT'};
my $version = $CGI::VERSION;
my $vemail;
my $from="lukeharris_79\";
my $to="lukeharris\";
my $subject="I\'m sending myself a test e-mail!";
my $sendmailpath="/usr/sbin/sendmail";
$vemail= param('vemail');

open (SENDMAIL, "| $sendmailpath -t");
#print SENDMAIL "Subject: $subject\n";
#print SENDMAIL "From: $from\n";
#print SENDMAIL "To: $to\n\n";
#print SENDMAIL "This is a test e-mail.\n\n";
#print SENDMAIL "I like to make lists:\n";
#print SENDMAIL "- One\n";
#print SENDMAIL "- Two\n";
#print SENDMAIL "- Three\n\n";
#print SENDMAIL "Bye!\n\n";
#print SENDMAIL "-Luke Harris";
#close (SENDMAIL);

print "Content-type: text/html\n\n";
print "<html><head></head><body>";
print "<H1>HELLO WORLD!</H1>\n";
print "your viewing this page with $test <br>" ;
print "your email is $vemail <br>";
print "your using version $version<br>";
#print "server admins email is $email<br>";
print "subject is $subject<br>";
print "to is $to<br>";
print "path is $sendmailpath<br>";
print "from is $from<br>";

print "</body></html>";
The program works until I uncomment the sendmail section. I tried uncommentting just the open and the close and sill nothing. It can be noted that the program receives $vemail from a test form and that works.

Oh yeah this is the results of "whereis sendmail" not sure what one to use as my path.


[root@localhost root]# whereis sendmail
sendmail: /usr/sbin/sendmail.postfix /usr/sbin/sendmail /usr/lib/sendmail /usr/share/man/man1/sendmail.1.bz2

Any help is appreciated.


Cool Hand...

saldkj 03-12-2005 02:44 PM

I've had luck with the Net::SMTP module.

TheLinuxDuck 03-14-2005 09:59 AM

First things first, since the code uses taint checking, (-T), it will need to define a valid (and untainted) ENV{PATH}. In this case, it could be as simple as:

local $ENV{PATH} = "/usr/sbin";
Second, you've got two ".com"s on the end of the $to definition.

Third, your open to sendmail doesn't do any error checking. It's a very good idea to trap any errors that might occur on the open.

After fixing these issues (of course changing the email addresses to match my own), the code worked just fine.

All times are GMT -5. The time now is 12:54 PM.