Reputation: 1249
How can I send an html email using the linux command line inside of a perl CGI script. I tried:
system(echo $message | mail -s $subject $email);
Upvotes: 0
Views: 3762
Reputation: 29844
Perl is not shell. What you are doing here is calling some Perl subroutine with the "bare word" echo
and passing the value of $message
binary or-ed with the output of some sub called mail
which is passed the size of the file named in $subject
(-s
operator)--and we can only get this far after completely ignoring that it wouldn't even compile because there is no operator between $email
and the expression before it.
In Perl, you need quotes for your system commands. But because $message
could have any number of characters that would make it hard to pass as-is to a shell, it's best to open a pipe and print to it:
use English qw<$OS_ERROR>;
open( my $mailh, '|-', "mail -s '$subject' $email" )
or die( "Could not open pipe! $OS_ERROR" )
;
print $mailh $message;
close $mailh;
Upvotes: 3
Reputation: 1567
Take a look at Net::SMTP.
From the documentation:
This module implements a client interface to the SMTP and ESMTP protocol, enabling a perl5 application to talk to SMTP servers.
...
This example sends a small message to the postmaster at the SMTP server known as mailhost:
#!/usr/local/bin/perl -w
use Net::SMTP;
$smtp = Net::SMTP->new('mailhost');
$smtp->mail($ENV{USER});
$smtp->to('postmaster');
$smtp->data();
$smtp->datasend("To: postmaster\n");
$smtp->datasend("\n");
$smtp->datasend("A simple test message\n");
$smtp->dataend();
$smtp->quit;
Upvotes: 0