Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

how to send email in Perl with a given sub function?

Hi,
This is  the sub function that I am going to use in my Perl code.

sub SendMail {
    my ($subj, $msg, @users) = @_;

    foreach $user (@users) {
        my %mail = (
                    To      => "$user\@domain.com",
                    From    => "${user}\@domain.com",
                    Subject => "$0 errors detected",
                    Message => $msg,
                   );
        if ( ! sendmail(%mail) ) {
            print "$0: ERROR $Mail::Sendmail::error\n";
        }
    }
} # SendMail()

Open in new window


I am not supposed to make any changes to this function. Can you please tell me how I will call this function to send an email in the following code?

my $nb_sec = 60;
timeout $nb_secs => sub {
        system("java -jar myfile.java");
  } ;
  if ($@){

    Send e-mail from a1@domain.com to a2@domain.com with a subject of "Logging Timed out" and with a message of "System call interrupted due to time out"

     }
        if ($? == -1) {

           Send email from a1@domain.com to a2@domain.com with a subject line of "Logging Failed" and with a message of "failed to execute: $!\n"

        } elsif ($? & 127) {
             my $signal = ($? & 127);
            my $woCoredump = ($? & 128) ? 'with' : 'without';
            
         Send email from a1@domain.com to a2@domain.com with a subject line of Logging Failed and a message line of "Child died with signal $signal, $woCodedump coredump\n"

        }
    }

Open in new window



Thanks,
Avatar of Carl Bohman
Carl Bohman
Flag of United States of America image

Your code sends from a1@domain.com to a1@domain.com or from a2@domain.com to a2@domain.com.  You can't send to one user from another user.  The two have to match.
You also have a fixed subject that you can't fully overwrite.

The only way to get what you want would be to modify the existing function or create a modified copy with a new name and call that one.
ASKER CERTIFIED SOLUTION
Avatar of Carl Bohman
Carl Bohman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar
Tolgar

ASKER

@bounsy:

I have an additional question in here:

What does $! mean in the body of my initial post?

Thanks,
Avatar of ozo
perldoc perlvar
...
       $OS_ERROR
       $ERRNO
       $!      If used numerically, yields the current value of the C "errno"
               variable, or in other words, if a system or library call fails,
               it sets this variable.  This means that the value of $! is
               meaningful only immediately after a failure:

                   if (open(FH, $filename)) {
                       # Here $! is meaningless.
                       ...
                   } else {
                       # ONLY here is $! meaningful.
                       ...
                       # Already here $! might be meaningless.
                   }
                   # Since here we might have either success or failure,
                   # here $! is meaningless.

               In the above meaningless stands for anything: zero, non-zero,
               "undef".  A successful system or library call does not set the
               variable to zero.

               If used as a string, yields the corresponding system error
               string.  You can assign a number to $! to set errno if, for
               instance, you want "$!" to return the string for error n, or
               you want to set the exit value for the die() operator.
               (Mnemonic: What just went bang?)

               Also see "Error Indicators".
Avatar of Tolgar

ASKER

Thanks Ozo,