简体   繁体   English

在perl CGI脚本中使用linux mail命令帮助发送电子邮件

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

How can I send an html email using the linux command line inside of a perl CGI script. 如何在perl CGI脚本中使用linux命令行发送html电子邮件。 I tried: 我试过了:

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

Perl is not shell. Perl不是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. 您在此处执行的操作是使用“裸词” echo调用一些Perl子例程,并将$message二进制值或-ed与名为mail的某些子变量的输出一起传递,并传递$subject命名文件的大小( -s运算符),并且我们只能在完全忽略它甚至无法编译之后才能做到这一点,因为$email和它之前的表达式之间没有运算符。

In Perl, you need quotes for your system commands. 在Perl中,您需要为系统命令加上引号。 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: 但是因为$message可以包含任何数量的字符,这使得很难将其原样传递给外壳,所以最好打开管道并向其打印:

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 . 看一下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. 该模块实现了SMTP和ESMTP协议的客户端接口,使perl5应用程序可以与SMTP服务器通信。

... ...

This example sends a small message to the postmaster at the SMTP server known as mailhost: 本示例向SMTP服务器上称为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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM