Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-24
6
Medium Priority
?
395 Views
Last Modified: 2012-05-11
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
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 35208200
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
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 35208211
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
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 2000 total points
ID: 35208235
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Tolgar
ID: 35234702
@bounsy:

I have an additional question in here:

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

Thanks,
0
 
LVL 84

Expert Comment

by:ozo
ID: 35234925
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
 

Author Comment

by:Tolgar
ID: 35235896
Thanks Ozo,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question