Perl 发送邮件格式
不确定我哪里做错了,但当我将$hostname变量放在“$from”变量中时,sendmail不会在主题行中打印任何内容,否则它可以正常工作 我想把“from”作为noreply@hostname. 代码如下:Perl 发送邮件格式,perl,sendmail,Perl,Sendmail,不确定我哪里做错了,但当我将$hostname变量放在“$from”变量中时,sendmail不会在主题行中打印任何内容,否则它可以正常工作 我想把“from”作为noreply@hostname. 代码如下: my $f="test.txt"; my $h=`hostname`; if ( -s "$f" ) { my $to='user@example.com'; my $from= "noreply\@$h"; my $subject='Test Error'; op
my $f="test.txt";
my $h=`hostname`;
if ( -s "$f" ) {
my $to='user@example.com';
my $from= "noreply\@$h";
my $subject='Test Error';
open(MAIL, "|/usr/sbin/sendmail -t");
## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "Test Body\n";
close(MAIL);
}
您的
$h
末尾有一个尾随的换行符
添加一个可修复此问题的选项:
my $h=`hostname`;
chomp $h;
此外,最好使用词法变量而不是全局文件句柄,以及以下三种参数形式:
谢谢,假设我需要把文件中的内容放在哪里,把代码放在哪里,放
open my $mail, '|-', '/usr/sbin/sendmail -t';
## Mail Header
print $mail "To: $to\n";
print $mail "From: $from\n";
print $mail "Subject: $subject\n\n";
## Mail Body
print $mail "Test Body\n";
close $mail;