[英]Sendmail Format
Not sure where I am doing wrong, but when I am putting $hostname variable within "$from" variable, sendmail is not printing anything within subject line, else it works fine. 不确定我在哪里做错了,但是当我将$ hostname变量放在“$ from”变量中时,sendmail不会在主题行中打印任何内容,否则它可以正常工作。
I want to put "from" as noreply@hostname. 我想把“from”作为noreply @ hostname。
Here is the code : 这是代码:
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);
}
Your $h
has a trailing newline on the end of it. 您的
$h
在其末尾有一个尾随换行符。
Add a chomp
to fix it: 添加一个
chomp
来修复它:
my $h=`hostname`;
chomp $h;
Also, it's a good idea to use lexical variables instead of global filehandles, and the three-argument form of open
: 此外,使用词法变量而不是全局文件句柄以及
open
的三参数形式是个好主意:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.