Your application just detected a critical condition, you can write a message to an application log or even the syserr_log but how do you notify someone right now that the application needs attention? The obvious answer to send an E-mail or SMS message to someone or maybe several someones BUT OpenVOS does not have any built in way to do this, or does it?

The gnu_library includes the Perl command and a large number of Perl modules (pm files, not to be confused with program modules). One of those modules is SMTP.pm. Which according to the  documentation “implements a client interface to the SMTP and ESMTP protocol, enabling a perl5 application to talk to SMTP servers”.

I have written the following Perl script using that module. Invoking the script without any arguments will give you the usage message

perl smtp.pl

Usage:
   perl smtp.pl -hub MAILHUB -from SENDER@DOMAIN -subject SUBJECT
                -message MESSAGE [-message MESSAGE]*
                -to RECIPIENT@DOMAIN [-to RECIPIENT@DOMAIN]*
                [-verbose]

Required arguments are the SMTP server aka mailhub (-h), the sender, as an E-mail address (-f), a subject (-s) and at least 1 message line (-m) and recipient (-t). If the text of the subject or any message lines contain spaces the text must be enclosed in quotes. The -v is an optional debugging aid and indicates that all arguments and communication with the SMTP server should be displayed.

For example, if I want to send an E-mail to the account tied to my phone I could execute

perl smtp.pl -h mailhub.stratus.com -f noah.davids@stratus.com -s 'test 1' -m 'n
+etwork is crashing call in' -m NOW!! -to XXXXXX@YYYYY.com

As you can see, you can abbreviate the arguments to just their first letters, and sorry but you do not get to see my phone’s E-mail account. The mail looked like

From: Noah Davids
test 1
Oct 28, 2010 12:42 PM

network is crashing call in
NOW!

Sending a text message is exactly the same as sending an E-mail but the “to” address is phonenumber@phonecompany. Phone number is obviously the phone number of the phone. The phonecompany domain will vary depending on the phone company.

For example

perl smtp.pl -h mailhub.stratus.com -f noah.davids@stratus.com -s 'test 2' -m 'n
+etwork is crashing call in' -m NOW!! -to 5555555555@phonecompany

You will note that the lines are joined, and you don’t get to see my phone number or service provider either.

Subject:test 2
 network is crashing call in NOW!

I found the entries that make up the following list here and here.. I have tested Sprint and Verizon, you are on your own with the others.

Alltel          message.alltel.com
AT&T            txt.att.net
AT&T MMS        MMS.att.net
Cingular        cingularme.com
Metro PCS       MyMetroPcs.com
Nextel          messaging.nextel.com
Powertel        ptel.net
Sprint          messaging.sprintpcs.com
SunCom          tms.suncom.com
T-Mobile        tmomail.net
US Cellular     email.uscc.net
Verizon         vtext.com
Virgin Mobile   vmobl.com

If you do not have the gnu_library installed there is a SMTP client on the ftp.stratus.com server, you can download a bundle by clicking here. The bundle includes source code and also program modules for all hardware types and TCP stacks.

send_smtp_mail.pm -form

-------------------------------- send_smtp_mail ------------------------------
 -smtp_server:
 -to:
 -cc:
 -bcc:
 -from:
 -reply_to:
 -return_receipt_to:
 -subject:
 message_1:
 message_2:
 message_3:
 message_4:
 message_5:
 message_6:
 -send_file:
 -smtp_port:         smtp
 -retry_times:
 -user_name:
 -password:
 -control:
 -domain:
 -importance:        normal              -send_file_mode:    text
 -authorization:     none                -force_:          no

Here is an example execution.

send_smtp_mail.pm -s mailhub.stratus.com -fr  noah.davids@stratus.com -sub 'test
+ 3' -msg 'network is crashing call in'  -msg 'NOW !!' -to XXXXXX@YYYYY.com
Mail sent to XXXXXX@YYYYY.com

Note that the command line arguments are different from the form’s arguments. To display the command line arguments use the -help option

send_smtp_mail.pm -help                                                        

 Command Line Syntax: (All arguments require a value to follow)

-server|s
 -port|p
 -to ;;...
 -cc ;;...
 -bcc ;;...
 -from|fr
 -reply|rp|rply
 -return|rtn
 -importance|i high|normal|low
 -subject|sub
 -msg|m     (multiple allowed)
 -file|f /text|html|binary|text_encl|html_encl    (multiple allowed)
 -auth|a login
 -user|u           (only needed for login auth)
 -password|pass     (only needed for login auth)
 -braces|b yes|no
 -domain|dom
 -retry|ret
 -control|c

to enable VOS form mode, supply the -vos option before ALL other options.

These are commands, how do you integrate them into an application? Well obviously if your application is a Perl script you can use my script as a template to make calls to the SMTP module. By the same token you can integrate the C code from send_smtp_mail modules into your C application. The other alternative is to just call Perl or the send_smtp.mail command directly from your application. Assuming your application is in C the following call will work

system ("perl smtp.pl -h mailhub.stratus.com -f noah.davids@stratus.com -s 'APPL
+ICATION ALERT' -m 'Some failure event has occurred' -t XXXXXX@YYYYY.com");

If you are using PL1 or Cobol you will need to write a C routine and bind that into your application.

Finally, let’s talk about errors.

If there is a problem with the from address the SMTP Perl module will report

perl smtp.pl -h mailhub.stratus.com -f noah.davids -m 'debug test line 1' -m 'de
+bug test line 2' -m 'debug test line 3' -to XXXXXX@YYYYY.com -s 'test 4'
Error setting from address  suggest setting verbose mode

Using the suggested verbose argument you can see what the SMTP server is reporting.

perl smtp.pl -h mailhub.stratus.com -f noah.davids -m 'debug test line 1' -m 'de
+bug test line 2' -m 'debug test line 3' -to XXXXXX@YYYYY.com -s 'test 4' -v
mailhub = mailhub.stratus.com
from = noah.davids
subject = test 4
message [0] = debug test line 1
message [1] = debug test line 2
message [2] = debug test line 3
to [0] = XXXXXX@YYYYY.com

Net::SMTP: Net::SMTP(2.24)
Net::SMTP:   Net::Cmd(2.21)
Net::SMTP:     Exporter(5.566)
Net::SMTP:   IO::Socket::INET(1.26)
Net::SMTP:     IO::Socket(1.27)
Net::SMTP:       IO::Handle(1.21)

Net::SMTP=GLOB(0x402f9c04)>>> MAIL FROM:
Net::SMTP=GLOB(0x402f9c04)<<< 553 5.5.4 ... Domain name required fo
+r sender address noah.davids
Error setting from address  suggest setting verbose mode

The send_smtp_mail.pm will report the SMTP server’s response without any extra prodding.

send_smtp_mail.pm -s mailhub.stratus.com -fr noah.davids -sub  'test 4'  -msg 'b
+ad from address' -msg 'NOW !!' -to XXXXXX@YYYYY.com
send_smtp_mail: Expected 250 reply.  Got: 553 5.5.4 noah.davids... Domain name r
+equired

If you can’t reach the mailhub the Perl script will report

perl smtp.pl -h mailhub.stratus.com -f noah.davids@stratus.com -s 'test 1' -m 'n
+etwork is crashing call in' -m NOW!! -to XXXXXX@YYYYY.com
Cannot reach mailhub <mailhub.stratus.com>

The send_smtp_mail command reports

pm>send_smtp_mail.pm -s mailhub.stratus.com -fr noah.davids@stratus.com -sub 'te
+st 2' -msg 'network is crashing call in' -msg 'NOW !!' -to XXXXXX@YYYYY.com
send_smtp_mail: Can't connect to: mailhub.stratus.com Network trying to be reach
+ed is unreachable.

A bad “to” address will not generate any errors from either command. It may result is an E-mail from the mailhub to the “from” address indicating that the mail could not be delivered.

The smtp.pl Perl script:

# smtp.pl begins here
#
#
# Version 1.00 10-10-31
# noah.davids@stratus.com
#
use Net::SMTP;
use Getopt::Long;
use strict;

my ($mailHub, @To, $From, $Subject, @Message, $Verbose);
my ($Result, $i, $toList);
my ($smtp);

$Result = GetOptions ("hub=s"      => $mailHub,
                      "to=s"       => @To,
                      "from=s"     => $From,
                      "subject=s"  => $Subject,
                      "message=s"  => @Message,
                      "verbose"    => $Verbose);

if (($Result != 1) || !defined($mailHub) || !defined($From) ||
                      !defined($Subject) || !defined(@Message) ||
                      !defined(@To))
   {
   if (defined($mailHub)) { print "mailhub = " . $mailHub . "n"; }
   if (defined($From))    { print "from = " . $From . "n" ; }
   if (defined($Subject)) { print "subject = " . $Subject . "n"; }
   for ($i = 0; $i < @Message; $i++)
        { print "message [" . $i . "] = " . $Message[$i] . "n"; }
   for ($i = 0; $i < @To; $i++)
        { print "to [" . $i . "] = " . $To[$i] . "n"; }
   print "nnUsage:n";
   print "tperl smtp.pl -hub MAILHUB -from SENDER@DOMAIN -subject SUBJECTn";
   print "ttt-message MESSAGE [-message MESSAGE]*n";
   print "ttt-to RECIPIENT@DOMAIN [-to RECIPIENT@DOMAIN]*n";
   print "ttt[-verbose]nn";
   exit;
   }

if (defined($Verbose))
   {
   if (defined($mailHub)) { print "mailhub = " . $mailHub . "n"; }
   if (defined($From))    { print "from = " . $From . "n" ; }
   if (defined($Subject)) { print "subject = " . $Subject . "n"; }
   for ($i = 0; $i < @Message; $i++)
        { print "message [" . $i . "] = " . $Message[$i] . "n"; }
   for ($i = 0; $i < @To; $i++)
        { print "to [" . $i . "] = " . $To[$i] . "n"; }
   }

$smtp = Net::SMTP->new($mailHub) || die "Cannot reach mailhub <" .
                                        $mailHub . ">n";

if (defined($Verbose)) { $smtp->debug(1); }

$smtp->mail($From) || die "Error setting from address <" . $From .
                          "> suggest setting verbose moden";

$toList = $To[0];
$smtp->to($To[0]) || die "Error setting to address <" . $To[0] .
                          "> suggest setting verbose moden";

for ($i = 1; $i < @To; $i++)
    {
    $toList = $toList . ";" . $To[$i];
    $smtp->to($To[$i]) || die "Error setting to address <" . $To[$i] .
                          "> suggest setting verbose moden";

    }

$smtp->data() || die "Error setting data suggest setting verbose moden";

$smtp->datasend("To: " . $toList . "n") ||
       die "Error sending To string <" . $toList .
           "> suggest setting verbose moden";

$smtp->datasend("Subject: " . $Subject . "n") ||
       die "Error sending Subject string <" . $Subject .
           "> suggest setting verbose moden";

$smtp->datasend("n") ||
       die "Error sending newline suggest setting verbose moden";

for ($i = 0; $i < @Message; $i++)
   {
   $smtp->datasend($Message[$i] . "n") || die
       die "Error sending message " . $i . " <" . $Message[$i] .
           "> suggest setting verbose moden";
   }

$smtp->dataend() || die "Error ending data send " .
                        "suggest setting verbose moden";

$smtp->quit || die "Error closing connection suggest setting verbose moden";
#
# smtp.pl ends here