Combining Code, Newbie Issues.

N

Naji

OK, I have never used Perl before, and FAQ's have confused me quite a
bit. I am not understanding some of the Perl syntax. But anyhow, I
think I have the code, I just need to combine it. At work, we need a
Perl script that automatically sends an e-mail with an attachment every
morning. Using a macro with a scheduled running of Outlook is crashing
too much, so using Perl was brought up as a good alternative. Here is
what our current code is:



&CheckMail($ReturnCode);

sub CheckMail {

my $ReturnCode = shift;
use Mail::Sender;

$msgfrom = qw(PPS-Server) ;
$msgsubject = qq(Production Scheduler);

if ( $ReturnCode == 0 ) {
$msg = "The Production Scheduler has completed successfully " ;
} elsif ( $ReturnCode == 1 ) {
$msg = "The Production Scheduler has completed with errors" ;
} else {
$msg = "The Production Scheduler has completed with warnings" ;
}
$sender = new Mail::Sender {smtp => "EX1",
from => "$msgfrom"};

@ppsmail = qw (
(e-mail address removed)
);

chdir 'D:/PPSPROD/LOG' or die "EEEEEEEEE =====> Can\'t open
D:/PPSPROD/LOG \n";

foreach $msgto (@ppsmail) {
$sender->MailFile({to => "$msgto",
subject => "$msgsubject",
msg => "$msg",
file => "schsub.log"});
}

}



And then, thanks to this forum, I have syntax for checking whether the
file is worth sending...wether it's been updated that day. That code is
as follows:


$currentTime = time();
($device, $inode, $mode, $numOfHardLinks, $userId, $groupId,
$deviceIdentifier, $size, $lastAccessed, $lastModified,
$inodeLastChanged) = stat($attachment);


if($lastModified + 43200 >= $currentTime) { # 43200 is the number of
seconds in 12 hours.
# Attachment was modified in the last 12 hours
 
J

J. Gleixner

script said:
OK, I have never used Perl before, and FAQ's have confused me quite a
bit. I am not understanding some of the Perl syntax. But anyhow, I
think I have the code, I just need to combine it. At work, we need a
Perl script that automatically sends an e-mail with an attachment every
morning. Using a macro with a scheduled running of Outlook is crashing
too much, so using Perl was brought up as a good alternative. Here is
what our current code is:

Always add the following, to help you with possible errors:

use strict;
use warnings;

# 'use' is typically at the top of the file, putting it in a
# sub doesn't do anything special.
use Mail::Sender;
&CheckMail($ReturnCode);

Don't use & when calling a subroutine.

CheckMail($ReturnCode) if worth_sending();
sub CheckMail { [code snipped]
}



And then, thanks to this forum, I have syntax for checking whether the
file is worth sending...wether it's been updated that day. That code is
as follows:


$currentTime = time();
($device, $inode, $mode, $numOfHardLinks, $userId, $groupId,
$deviceIdentifier, $size, $lastAccessed, $lastModified,
$inodeLastChanged) = stat($attachment);


if($lastModified + 43200 >= $currentTime) { # 43200 is the number of
seconds in 12 hours.
# Attachment was modified in the last 12 hours

# assuming $attachment is defined somewhere.
sub worth_sending
{
# This could be done in one line.. but based on your code..
my $currentTime = time();
my $lastModified = ( stat( $attachment ) )[9];
return 1 if $lastModified + 43200 >= $currentTime;
}
 
U

usenet

Naji said:
Here is what our current code is:
[snip code]

You are going to a lot of trouble there, my friend. What if you pared
that down to something like this (substitute your own filename, e-mail
addresses and SMTP server):

#!/usr/bin/perl
use strict; use warnings;

my $attachment = "/path/to/some/file.log";

if ( (stat $attachment)[9] + 43200 >= time) {
use Mail::Sender;
my @ppsmail = qw/[email protected] (e-mail address removed)/;
(new Mail::Sender)->MailFile({
'smtp' => 'mail.mySMTPserver.com',
'from' => 'PPS-Server',
'to' => \@ppsmail,
'subject' => 'Production Scheduler',
'msg' => 'Here is a file for you.',
'file' => $attachment
}) || die "Cannot send message: $!\n";
print "Message Sent.\n";
} else {
print "Nothing to do.\n";
}
__END__
 
U

usenet

[snipped code]

I just noticed from your original post that your message includes a
status (based on return code). I'm assuming that (because you don't
include the actual numeric value of the code in the message) that
"warnings" will always be rc==2 (so that the return code can only be 0,
1, or 2). If this assumption is correct, you can save some typing with
a nifty trick like this:

my $msg = "The Production Scheduler has completed " .
("Successfully", "With Errors", "With
Warnings")[$returncode];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top