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
Solved

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

Posted on 2011-03-24
6
384 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
binary to char / hexadecimal 5 120
hard perl script 16 158
Perl File::Find alternative 1 78
html form to write data to csv 24 134
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

861 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