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

Hi All,

I am a newbie to perl. I am trying to make a script in perl for sending email on a person's birthday. The name,date,gender,timezone,mailid are in a semicolon seperated text file. I am reading the file line by line and splitting the variables. In my mail sending part, i am getting error.

The problem i am facing is,if I am hardcoding the email address in mailx syntax, I am getting a mail. If I am using the email variable in place of complete email address, I am getting an error. Text file, Error Code and the program output are below. Please help. PS: I have masked the email ids. My Perl version is v5.8.8. OS is Linux x86_64. My Shell is Korn Shell ie /bin/ksh $ echo $KSH_VERSION Version AJM 93t+ 2010-02-02

Text file ========= Sandeep:1002:IST:him:email@provider Vishal:0603:IST:him:email@provider Aaradhna:2703:IST:her:email@provider Mallikarjun Gowda:2412:IST:him:email@provider Code Snippet ============ #!/usr/bin/perl -w use POSIX qw[tzset]; use POSIX qw(strftime); $ENV{'TZ'} = 'Asia/Calcutta'; tzset(); $clear_string=`clear`; print $clear_string; printf("\t\t\tIST TIMEZONE SET\n\n"); $datestring = strftime "%a %b %e %H:%M:%S %Y", localtime; printf("\tDate and time - $datestring\n"); $datecmp = strftime "%d%m", localtime; #This part reads the file line by line my $filename = '/home/srivasta/birthday.txt'; open (my $fh,'<:encoding(UTF-8)',$filename) or die "Could not open file '$filename' $!"; while (my $row = <$fh>) { chomp($row); my($birthdayperson,$birthdate,$timezone,$gender,$email)= +split /:/,$row; if ($birthdate == $datecmp){ printf("\tBirthdayPerson name is $birthdayperson\n"); printf("\tBirthdate is $birthdate\n"); printf("\ttimezone is $timezone\n"); printf("\tGender is $gender\n"); printf("\tEmail is $email\n"); printf("\t$birthdayperson has birthday today"); printf("\n\tRemember to Wish $gender a very Happy Birthday\n"); # Below block of code sends mail to the intended Receipent open(MAILPIPE,'|/bin/mailx -s "Happy Birthday" email@provider') or die + "Can't open pipe $!"; print MAILPIPE "Wishing you a Very happy Birthday. Enjoy the day\n"; close MAILPIPE; } } Sample Output ============= IST TIMEZONE SET Date and time - Thu Dec 24 14:18:46 2015 BirthdayPerson name is Mallikarjun Gowda Birthdate is 2412 timezone is IST Gender is him Email is email@provider Mallikarjun Gowda has birthday today Remember to Wish him a very Happy Birthday $

if I am using $email variable in place of email@provider (hardcoded mailid) i am getting an error as below.

open(MAILPIPE,'|/bin/mailx -s "Happy Birthday" $email') or die "Can't +open pipe $!"; Output ====== IST TIMEZONE SET Date and time - Thu Dec 24 14:32:03 2015 BirthdayPerson name is Mallikarjun Gowda Birthdate is 2412 timezone is IST Gender is him Email is email@provider.com Mallikarjun Gowda has birthday today Remember to Wish him a very Happy Birthday You must specify direct recipients with -s, -c, or -b.

Replies are listed 'Best First'.
Re: How to put email addresses in a variable and send mails via mailx command
by jcb (Parson) on Dec 24, 2015 at 21:19 UTC

    Another option (TIMTOWTDI!) is to use one of the many modules on CPAN for sending mail directly from Perl instead of running a subprocess.

    A recent question asked for advice on which of the many modules to use, one of the answers recommends looking at Task::Kensho::Email.

Re: How to put email addresses in a variable and send mails via mailx command
by Anonymous Monk on Dec 24, 2015 at 09:30 UTC

    Your problem is use of single-quotes which do not interpolate any variables ...

    ... '|/bin/mailx -s "Happy Birthday" $email' ...

    ... so reverse the quotes used: qq{|/bin/mailx ... $mail_addr}.

      ... or use explicit string concatenation, which is messier IMHO, but should work just as well:
          open(MAILPIPE,'|/bin/mailx -s "Happy Birthday" ' . $email) or die "Can't open pipe $!";


      Give a man a fish:  <%-{-{-{-<

        but should work just as well:

        both versions are vulnerable to shell interpolation