简体   繁体   中英

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.

I want to put "from" as 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.

Add a chomp to fix it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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