Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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,
0
Tolgar
Asked:
Tolgar
  • 3
  • 2
1 Solution
 
Carl BohmanCommented:
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.
0
 
Carl BohmanCommented:
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.
0
 
Carl BohmanCommented:
sub CustomSendMail {
    my ($to, $from, $subj, $msg) = @_;

    foreach $user (@users) {
        my %mail = (
                    To      => $to,
                    From    => $from,
                    Subject => $subj,
                    Message => $msg,
                   );
        if ( ! sendmail(%mail) ) {
            print "$0: ERROR $Mail::Sendmail::error\n";
        }
    }
} # CustomSendMail() 

CustomSendMail("a1@domain.com", "a2@domain.com", "Your subject goes here", "Your message goes here");

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
TolgarAuthor Commented:
@bounsy:

I have an additional question in here:

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

Thanks,
0
 
ozoCommented:
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".
0
 
TolgarAuthor Commented:
Thanks Ozo,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now