Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How to send email when executing PERL on Windows NT

by seanbo (Chaplain)
on Oct 05, 2001 at 22:00 UTC ( [id://117062]=note: print w/replies, xml ) Need Help??


in reply to How to send email when executing PERL on Windows NT

Here is a module I wrote a while back for an inhouse notification tool. It should run fine on NT (I developed it in Linux, tested in Linux, AIX, Solaris, & VMS, not NT, but should work fine. It uses Mail::Sender.

It may be overkill, but it allows you to store pager numbers and emails in a dictionary file and it looks up the email addys and pager numbers and send out a notification. You could modify it to read from Outlook Contacts as well, if you use that. I had to do some special stuff for our paging software (like add a % to the pager number and put it in the mail subject), but you can modify that stuff (orremove it all together). This is just a small piece of the whole solution, but you should get a good idea what you can do with Mail::Sender. See Pod for examples...

# ****code***** package Mail::Notify; # # Send a notification via smtp to email, pager, or both # based on entries in a dictionary file, distribution.list # # Written by Sean Ryle (c) 2001. use strict; use Mail::Sender; use Carp; use base 'Exporter'; use vars qw( @EXPORT $SMTP $FROM $DICT_FILE %DICTIONARY ); @EXPORT = qw(notify); # Here are some globals $DICT_FILE = '/path/to/dictionary/file'; %DICTIONARY = _read_dict_file($DICT_FILE); $SMTP = "your.smtp.server"; $FROM = "nobody\@test.com"; sub notify { #get params my %params = @_; #get info from parms, die if 'to' is not passed with a value my $SUBJECT = $params{"subject"} || ""; my $MSG = $params{"msg"} || ""; my $F_MSG = $params{"include"} || ""; my $ATT = $params{"attach"} || ""; my $F_TO = $params{"dist"} || ""; my $TO = $params{"to"} || ""; $TO =~ tr/ \t//d; my @tmp = split /,/, $TO; my @REC = (); my @PAGE = (); # add any names from a dist list to search the dictionary for if($F_TO) { push @tmp, split /,/, _read_dist_file($F_TO); } unless(@tmp) { croak "I am not a mind reader. Who should I send the notificati +on to?"; } for(@tmp) { # Note: \w+ allows _only_ letters, numbers, and _. # If we want something else, we need to adjust the regexs. if(/^(\w+)\.email$/) { if(exists $DICTIONARY{$1}{email}) { push @REC, $DICTIONARY{$1}{email}; }else{ warn "$1\'s email address was not found in $DICT_FILE"; } } elsif(/^(\w+)\.page$/) { if(exists $DICTIONARY{$1}{pager}) { push @PAGE, $DICTIONARY{$1}{pager}; }else{ warn "$1\'s pager number was not found in $DICT_FILE"; } } elsif(/^(\w+)(?:\.both)?$/) { # Assume both if nothing if(exists $DICTIONARY{$1}{email}) { push @REC, $DICTIONARY{$1}{email}; }else{ warn "$1\'s email address was not found in $DICT_FILE"; } if(exists $DICTIONARY{$_}{pager}) { push @PAGE, $DICTIONARY{$1}{pager}; }else{ warn "$1\'s pager number was not found in $DICT_FILE"; } } } if($F_MSG) { $MSG .= _read_include_file($F_MSG); } #Create a new Mail::Sender object with the default smtp server #and default FROM email address my $sender = new Mail::Sender {smtp => "$SMTP", from => "$FROM"}; # Send the message to the intended parties via email # include attachments if necessary. if($ATT) { if(@REC) { $sender->MailFile({ to => join(",", @REC), subject => $SUBJECT, msg => $MSG, file => $ATT}); } if(@PAGE) { $sender->MailMsg({ to => 'pager@someplace.com', #you have to +have something setup for the pages subject => join(",", @PAGE), msg => "$MSG\n$ATT was attached."}); } }else{ if(@REC) { $sender->MailMsg({ to => join(",", @REC), subject => $SUBJECT, msg => $MSG}); } # Send the page email to the pager email addy with the # pager list as the subject if(@PAGE) { $sender->MailMsg({ to => 'pager@someplace.com', #you have to have somethi +ng setup for the pages subject => join(",", @PAGE), msg => $MSG}); } } } sub _read_dict_file { my $filename = shift or return; open my $file, "< $filename" or die "$0: can't open dictionary file $filename: $!"; my %dict = (); my $name = ''; local $_; while(<$file>) { chomp; if(/^\s*\[(\w+)\]\s*(?:#.*)?$/) { $name = $1; # Change to a new person } elsif(/^\s*email\s*=\s*(\S+)\s*(?:#.*)?$/) { $dict{$name}{email} = $1; # Save email. } elsif(/^\s*pager\s*=\s*(\(?\d(?:[ \-\d)]*\d)?)\s*(?:#.*)?$/) { $dict{$name}{pager} = "%".$1; # Save pager. #this is u +sed by our pager software $dict{$name}{pager} =~ tr/-)( //d; # Remove dashes, spaces, pare +ns } elsif(/^\s*(?:#.*)?$/) { # ignore; blank or comment line } else { warn "$0: bad line #$. in dictionary file $filename\n"; } } close $file or die "$0: can't close dictionary file $filename: $!"; return %dict; } sub _read_dist_file { my $filename = shift or return; open my $file, "< $filename" or die "$0: can't open distribution file $filename: $!"; local $/; # Slurp mode: read whole file in. # If not defined, it either means there was # a read error or the file was totally empty. defined(my $text = <$file>) or die "$0: can't read from distribution file $filename: $!"; close $file or die "$0: can't close distribution file $filename: $!" +; $text =~ s/\n/,/g; # Convert newlines to commas. $text =~ tr/ \t//d; # Remove spaces and tabs return $text; } sub _read_include_file { my $filename = shift or return; open my $file, "< $filename" or die "$0: can't open file $filename: $!"; my $text = "\n**This text was inserted from the file: $filename**\n" +; local $_; local $/; # Slurp mode: read whole file in. # If not defined, it either means there was # a read error or the file was totally empty. defined($_ = <$file>) or die "$0: can't read from distribution file $filename: $!"; close $file or die "$0: can't close include file $filename: $!"; $text .= $_; return $text; } 1; __END__ =head1 NAME Mail::Notify - Send a notification via smtp to email, pager, or both based on ent ries in a dictionary file, distribution.dict =head1 SYNOPSIS use Mail::Notify; notify( to => 'joe.email, john.page, betty.both', subject => 'Test notification', msg => 'This is a test.', attach => './test.dat', ); =head1 DESCRIPTION This module connects via smtp to a mail server to send emails or pages to a user or distribution list. Files can be atteached to emails. See the C<attach> parameter to C<notify()> below. =head1 FUNCTIONS This module contains and exports a single function: C<notify()>. =head2 notify Creates and sends an email, page, or both to the specified users from the dictionary file (see below). This function returns nothing and uses named parameters. For example: notify( to => $recipients, msg => $body ); The following parameters can be specified: =over 4 =over 4 =item to A single string containing a comma separated list of recipients. Each recipient will be searched for in the dictionary file and can have any one of '.email', '.page', or '.both' (the default is '.both') to specify that they should be sent an email, page, or both (respectively). Any names not found in the dictionary file will be ignored. Example: 'joe.email, john, betty' This parameter is optional if the C<dist> parameter is given. =item dist The path to a file containing a list of names to send to. Similar to using a group in an email system. The names in this file should be exactly the same as you would pass to the C<to> parameter except that they can be separated by newlines as well as commas. This parameter is optional if the C<to> parameter is given. =item subject The optional subject for emails. This is ignored for pages. =item msg The body of the message to page or email. =item include The path to a file that will be appended to the end of the email along with a note that the name of the included file. This parameter is optional. =item attach The path to a file that will be attached to the email. For pages, a line is appended to the text of the page stating the name of the attached file (no files are actually attached to pages). This parameter is optional. =back =back =head1 DICTIONARY FILE This module requires that a file called distribution.dict be created that holds the pager numbers and email addresses for anyone who is to receive notification. The file should contain one or more records of the form: [user] email=name@host.com pager=555-1234 You can specify comments using a # which last up to the end of the line. Blank lines and whitespace are ignored. If this file is not found, cannot be read, or is empty, the module will die with an error. =cut


seanbo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://117062]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found