开发者

Help sending email usings the linux mail command in a perl CGI script

How can I send an html em开发者_开发百科ail using the linux command line inside of a perl CGI script. I tried:

system(echo $message | mail -s $subject $email);


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;


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜