I am currently trying to create an Intra-net at home and i wanted to be able to send emails from within a web page, i have tried a few basic Perl scripts but cant seem to get them working on my server at home, i know that the server works fine and perl is installed ok but just cant seem to get a simple email script to work. could anyone please post a basic perl script to accomplish this?
TIA
TurboSquid
I use this code, seems to work pretty well, assuming you have sendmail installed (most webhosts do). Just put it in a file mail.pm and edit the first few declarations to suit your site. Your host can tell you what the path to sendmail is.
Code:
package mail;
use strict;
my $mailer = '/usr/sbin/sendmail -oi -t -oem';
my $from = '"my_website_name" <address@example.com>';
my $generatedby = 'my_website_name';
#MAIL HELPER FUNCTIONS
sub _mail_start {
my ($sender, @recipients) = @_;
my $command = $mailer;
$command .= qq{ -f "$sender"};
my $result;
eval {
local $SIG{__DIE__};
$result = open SENDMAIL, "| $command";
};
if ($@) {
die $@ unless $@ =~ /\$ENV{PATH}/;
delete $ENV{PATH};
$result = open SENDMAIL, "| $command";
}
die "Can't open mailprog [$command]\n" unless $result;
}
sub _mail_end {
close SENDMAIL or die "close sendmail pipe failed, mailer=[$mailer]";
}
#MAIN SEND MAIL FUNCTION
sub send_mail {
my ($to, $subject, $body) = @_;
_mail_start($to);
my $addr = $main::cgi->remote_addr();
$addr =~ /^([\d\.]+)$/ or die "bad remote addr [$addr]";
$addr = $1;
print SENDMAIL <<ENDOFMAIL;
X-HTTP-Client: [$addr]
X-Generated-By: $generatedby
To: $to
Reply-to: $from
From: $from
Subject: $subject
$body
ENDOFMAIL
_mail_end();
}
To use this function put the following in your main code:
Code:
# at the top, with all the other use statements
use mail;
# when you are ready to send the email
mail::send_mail($to, $subject, $body);
where $to, $subject and $body are already set by your code or simply constant expressions.
HTH.