cbtshare has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I am using the sendmail module in a perl script, but having a slight issue.I want to variablise the hostname but when and input that in the FROM section , but I'm getting an error

my $hostname=`hostname`; sub email { my %mail = ( To => 'andr@domain.com', From => "$hostname\@domain.com", Subject => " $hash_ref->{three} ", Message => "$_[0]" ); sendmail(%mail) or die $Mail::Sendmail::error; }
Error
Bad or missing From address: 'DataNode2 @domain.com'

If I try it without escaping the @ sign I get Possible unintended interpolation of @domain in string at aide.pl line 24. Global symbol "@domain" requires explicit package name at aide.pl line 24.

can I get assistance with this please? thank you

Replies are listed 'Best First'.
Re: Perl Script Email error
by hippo (Archbishop) on Jun 14, 2016 at 22:27 UTC

    Your $hostname has a trailing newline - you should chomp it first:

    my $hostname=`hostname`; chomp $hostname; # ... etc.
      Or, use Sys::Hostname and its hostname function to prevent shelling out and getting the newline in the hostname.
      use Sys::Hostname; my $hostname = hostname(); ...

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      I thank you :)