Solved

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

Posted on 2011-03-24
6
374 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
  • 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 500 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now