Home: Perl Programming Help: Intermediate:
Find file w/ date suffix and email attachment?



pichels
Novice

Mar 31, 2009, 12:57 PM


Views: 23206
Find file w/ date suffix and email attachment?

Hi,

Hope I am in the correct forum - please redirect me if not.

I am new to perl - took a class and have been hacking perl code together to get a program to take a date stamped file and send it out via MIME::Lite - make sense?

I've attached my code - or should I paste in here with my post?

I apologize - it is a real mess and I'm trying to figure out the latest errors.

I was able to get the date-based file from a dir and print it.
But, Next I need to take that value and pass it to my mailer and send off as an attachemnet!
file

Please advise - thanks!

-P


pichels
Novice

Mar 31, 2009, 1:00 PM


Views: 23205
Re: [pichels] Find file w/ date suffix and email attachment?

Forgot to add my error after running script:

C:\Documents and Settings\pichels.WMNA\My Documents\Perl>Wingetprtdate.pl
Scalar found where operator expected at C:\Documents and Settings\pichels.WMNA\M
y Documents\Perl\Wingetprtdate.pl line 103, near "Filename => '$drmfile"
(Might be a runaway multi-line '' string starting on line 102)
(Do you need to predeclare Filename?)
String found where operator expected at C:\Documents and Settings\pichels.WMNA\M
y Documents\Perl\Wingetprtdate.pl line 104, near "Disposition => '"
(Might be a runaway multi-line '' string starting on line 103)
(Missing semicolon on previous line?)
syntax error at C:\Documents and Settings\pichels.WMNA\My Documents\Perl\Wingetp
rtdate.pl line 103, near "Filename => '$drmfile"
Bad name after attachment' at C:\Documents and Settings\pichels.WMNA\My Document
s\Perl\Wingetprtdate.pl line 104.


FishMonger
Veteran / Moderator

Mar 31, 2009, 1:20 PM


Views: 23204
Re: [pichels] Find file w/ date suffix and email attachment?

You need to post or attach your code.


(This post was edited by FishMonger on Mar 31, 2009, 1:21 PM)


pichels
Novice

Mar 31, 2009, 1:28 PM


Views: 23201
Re: [pichels] Find file w/ date suffix and email attachment?

Hmmmm....

I added as an attachment?

Here it is...

#!/usr/bin/perl

#use strict;


use MIME::Lite;

use Date::Calc qw(

);


my ($Y,$M,$D) = ('','',''); # Original year,month,day
my ($y,$m,$d) = ('','',''); # Resulting year,month,day

# Year
($y,$m,$d) = year();

#print format_date($y, $m, $d);
my ($tdate) = format_date($y, $m, $d);


# ----------------------------------------
# subs
# ----------------------------------------

# Year
# Usage: ($y) = year([$y]);
sub year
{
my ($y,$m,$d) = (scalar(@_) == 1) ? (shift, 1, 1) : Today();
$m = 1;
$d = 1;
return ($y, $m, $d);
}


# Formatted date (YYYYMMDD - possible zero padding)
# Usage: $string = format_date([$y, $m, $d]);
sub format_date
{
my ($y,$m,$d) = Today();
return sprintf("%04d%02d%02d", $y, $m, $d);
}



#print "\n";


$mydir = ".";
opendir(DIR, "$mydir");
@drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
closedir(DIR);




foreach $file (@drmfile) {


#print "$file\n";




#my %params;

#$params{first_name} = 'TSM';
#$params{last_name} = 'Reports';


#my %options;
#$options{INCLUDE_PATH} = '/path/to/templates';

my $msg = MIME::Lite::TT::HTML->new(
From => 'tsm_reports@company.com',
To => 'tsmadmin@company.com',
Subject => 'TSM DR Plan',
#Template => {
# text => 'test.txt.tt',
# html => 'test.html.tt',
# },
#TmplOptions => \%options,
#TmplParams => \%params,
);


# Set our content type properly
$msg->attr("content-type" => "multipart/mixed");

# Attach a PDF to the message
$msg->attach( Type => 'application/txt',
Path => 'C:\recoveryplans\',
Filename => '$drmfile',
Disposition => 'attachment'
);


$msg->send('smtp', '10.97.24.104', Timeout => 60 );


}



Thanks!

-P


FishMonger
Veteran / Moderator

Mar 31, 2009, 1:45 PM


Views: 23198
Re: [pichels] Find file w/ date suffix and email attachment?

There are a number of issues, but lets start with one.

change line 94:
Path => 'C:\recoveryplans\',

to:
Path => 'C:\\recoveryplans\\',

or to this:
Path => 'C:/recoveryplans/',


Do you see why that should be changed?


pichels
Novice

Mar 31, 2009, 1:50 PM


Views: 23195
Re: [FishMonger] Find file w/ date suffix and email attachment?

Hi Fishmonger!

Ok - I am running on Perl on Win32 so I guess that would explain why I should list the dir structure the way you mentioned.

Why? No - I am not sure? why?

Thx.

-P


FishMonger
Veteran / Moderator

Mar 31, 2009, 1:53 PM


Views: 23195
Re: [pichels] Find file w/ date suffix and email attachment?

Lets clean up your date calculation.

Delete lines 8 through 45 and replace them with this:

use POSIX qw(strftime);

my $tdate = strftime("%Y%m%d", localtime);


FishMonger
Veteran / Moderator

Mar 31, 2009, 1:56 PM


Views: 23193
Re: [pichels] Find file w/ date suffix and email attachment?


In Reply To
Hi Fishmonger!

Ok - I am running on Perl on Win32 so I guess that would explain why I should list the dir structure the way you mentioned.

Why? No - I am not sure? why?

Thx.

-P


The backslash is the escape character, so if you need a literal \ backslash, you need to escape it with another backslash.


pichels
Novice

Mar 31, 2009, 1:58 PM


Views: 23193
Re: [FishMonger] Find file w/ date suffix and email attachment?

Ok - got it.

Much cleaner...phew.


#!/usr/bin/perl

#use strict;


use MIME::Lite;

use POSIX qw(strftime);

my $tdate = strftime("%Y%m%d", localtime);


print "\n";


$mydir = ".";
opendir(DIR, "$mydir");
@drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
closedir(DIR);




foreach $file (@drmfile) {


#print "$file\n";




#my %params;

#$params{first_name} = 'TSM';
#$params{last_name} = 'Reports';


#my %options;
#$options{INCLUDE_PATH} = '/path/to/templates';

my $msg = MIME::Lite::TT::HTML->new(
From => 'tsm_reports@company.com',
To => 'tsmadmin@company.com',
Subject => 'TSM DR Plan',
#Template => {
# text => 'test.txt.tt',
# html => 'test.html.tt',
# },
#TmplOptions => \%options,
#TmplParams => \%params,
);


# Set our content type properly
$msg->attr("content-type" => "multipart/mixed");

# Attach a PDF to the message
$msg->attach( Type => 'application/txt',
Path => 'C:\\recoveryplans\\',
Filename => '$drmfile',
Disposition => 'attachment'
);


$msg->send('smtp', '10.97.24.104', Timeout => 60 );


}


FishMonger
Veteran / Moderator

Mar 31, 2009, 2:01 PM


Views: 23189
Re: [pichels] Find file w/ date suffix and email attachment?

Ok, now uncomment the use strict line and add.

use warnings;


pichels
Novice

Mar 31, 2009, 2:03 PM


Views: 23186
Re: [FishMonger] Find file w/ date suffix and email attachment?

Ok - done.

-P


pichels
Novice

Mar 31, 2009, 2:05 PM


Views: 23185
Re: [pichels] Find file w/ date suffix and email attachment?

My errors now...
<smile>

C:\Scripts>Get_Plan_Send.pl
Global symbol "$mydir" requires explicit package name at C:\Scripts\Get_Plan_Sen
d.pl line 16.
Global symbol "$mydir" requires explicit package name at C:\Scripts\Get_Plan_Sen
d.pl line 17.
Global symbol "@drmfile" requires explicit package name at C:\Scripts\Get_Plan_S
end.pl line 18.
Global symbol "$file" requires explicit package name at C:\Scripts\Get_Plan_Send
.pl line 24.
Global symbol "@drmfile" requires explicit package name at C:\Scripts\Get_Plan_S
end.pl line 24.
Execution of C:\Scripts\Get_Plan_Send.pl aborted due to compilation errors.


FishMonger
Veteran / Moderator

Mar 31, 2009, 2:18 PM


Views: 23180
Re: [pichels] Find file w/ date suffix and email attachment?


Quote
Global symbol "$mydir" requires explicit package name

That means you need to declare the var with the my keyword.


pichels
Novice

Mar 31, 2009, 2:23 PM


Views: 23178
Re: [FishMonger] Find file w/ date suffix and email attachment?

Ok - I went thru and decalred them all but how can I tell if my program is working ok in Perl without debug or line tracing fo some kind?

#!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite;

use POSIX qw(strftime);

my $tdate = strftime("%Y%m%d", localtime);


print "\n";


my $mydir = ".";
opendir(DIR, "$mydir");
my @drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
closedir(DIR);



my $file;
foreach $file (@drmfile) {


#print "$file\n";




#my %params;

#$params{first_name} = 'TSM';
#$params{last_name} = 'Reports';


#my %options;
#$options{INCLUDE_PATH} = '/path/to/templates';

my $msg = MIME::Lite::TT::HTML->new(
From => 'tsm_reports@company.com',
To => 'tsmadmin@company.com',
Subject => 'TSM DR Plan',
Template => {
text => 'test.txt.tt',
html => 'test.html.tt',
},
#TmplOptions => \%options,
#TmplParams => \%params,
);


# Set our content type properly
$msg->attr("content-type" => "multipart/mixed");

# Attach a PDF to the message
$msg->attach( Type => 'application/txt',
Path => 'C:\\recoveryplans\\',
Filename => '$drmfile',
Disposition => 'attachment'
);


$msg->send('smtp', '10.97.24.104', Timeout => 60 );


}




Here is the file I want to get and pass to my mailer...



C:\Scripts>dir Plan*
Volume in drive C is Preload
Volume Serial Number is DC03-58F8

Directory of C:\Scripts

03/30/2009 06:29 AM 178,533 PLAN.20090330.062953
1 File(s) 178,533 bytes
0 Dir(s) 21,584,506,880 bytes free


pichels
Novice

Mar 31, 2009, 2:31 PM


Views: 23176
Re: [pichels] Find file w/ date suffix and email attachment?

Ok - shortened the code again - the MIME::Lite stuff - cleaner now...


#!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite;

use POSIX qw(strftime);

my $tdate = strftime("%Y%m%d", localtime);


print "\n";


my $mydir = ".";
opendir(DIR, "$mydir");
my @drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
closedir(DIR);



my $file;
foreach $file (@drmfile) {


print "$file\n";


my $msg = MIME::Lite->new(
From => 'me@myhost.com',
To => 'you@yourhost.com',
Cc => 'some@other.com, some@more.com',
Subject => 'A message with 2 parts...',
Type => 'multipart/mixed',
);

$msg->attach(
Type => 'TEXT',
Data => "C:\\recoveryplans\\",
);

$msg->send('smtp', '10.97.24.104', Timeout => 60 );


}


KevinR
Veteran


Mar 31, 2009, 6:16 PM


Views: 23170
Re: [pichels] Find file w/ date suffix and email attachment?

Your attachment looks like it is a directory, not a file:

Data => "C:\\recoveryplans\\",

I don't think you can attach a directory to an email.
-------------------------------------------------


pichels
Novice

Mar 31, 2009, 6:44 PM


Views: 23167
Re: [KevinR] Find file w/ date suffix and email attachment?

Hi,

I changed the code again.

Data => "C:\\Scripts\\$file",

But, I still can't get it to pickup @drmfile and pass to the Mailer code?

How do I do that?

Thanks.



#!/usr/bin/perl

#use strict;
#use warnings;

use MIME::Lite;

use POSIX qw(strftime);

my $tdate = strftime("%Y%m%d", localtime);


#print $tdate,"\n";


my $mydir = ".";
opendir(DIR, "$mydir");
my @drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
closedir(DIR);

#print @drmfile,"\n";

#my $pfile = $drmfile;
#print $pfile,"\n";

my $file;
foreach $file (@drmfile) {


#print "$file\n";


my $msg = MIME::Lite->new(
From => 'tsm_reports@comapny.com',
To => 'pichels@company.com',
Cc => 'some@other.com, some@more.com',
Subject => 'TSM DR Plan',

Type => 'multipart/mixed',
);

$msg->attach(
Type => 'TEXT',
Data => "C:\\Scripts\\$file",
);

$msg->send('smtp', '10.97.24.104', Timeout => 60 );


#}


KevinR
Veteran


Mar 31, 2009, 7:33 PM


Views: 23164
Re: [pichels] Find file w/ date suffix and email attachment?

Because of this line it looks like you are running the program in the current directory:

my $mydir = ".";

So why are you adding "Scripts" to the path?

Data => "C:\\Scripts\\$file",

Probably should just be the file name:

Data => $file,
-------------------------------------------------


pichels
Novice

Mar 31, 2009, 7:52 PM


Views: 23162
Re: [KevinR] Find file w/ date suffix and email attachment?

Hi,

Ok - that makes sense!

I'll change my code again.

Thanks.

-P


pichels
Novice

Apr 1, 2009, 7:12 AM


Views: 23155
Re: [pichels] Find file w/ date suffix and email attachment?

Hi again,

Looks like I am still not able to print out the file in my current dir.

A filename PLAN.DATE.* in the curent dir is put into an array - correct?

Then, I want print it.


#!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite;
use POSIX qw(strftime);


my $tdate = strftime("%Y%m%d", localtime);
my $file;

#print $tdate,"\n";


my $mydir = ".";
opendir(DIR, "$mydir");
my @drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
#my @drmfile = grep(/\.pl$/,readdir(DIR));
closedir(DIR);

#print $drmfile,"\n";

#my $pfile = @drmfile;


foreach $file (@drmfile) {

print $file,"\n";

}




Please help!


-P


FishMonger
Veteran / Moderator

Apr 1, 2009, 7:14 AM


Views: 23153
Re: [pichels] Find file w/ date suffix and email attachment?

Do you want to print the filename or its contents?


pichels
Novice

Apr 1, 2009, 7:21 AM


Views: 23150
Re: [FishMonger] Find file w/ date suffix and email attachment?

Hi!

Ok - Actually I found out that I was entering today's daye and using an old dated plan file friom two days ago!

I want to attach the file I am grepping/finding in the current dir to an email and send it.

Make sense?

Right now I am only printing the filename in the body of the email and not attaching the "actual" file!


#!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite;
use POSIX qw(strftime);


my $tdate = strftime("%Y%m%d", localtime);
my $file;

#print $tdate,"\n";


my $mydir = ".";
opendir(DIR, "$mydir");
my @drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
#my @drmfile = grep(/\.pl$/,readdir(DIR));
closedir(DIR);


foreach $file (@drmfile) {

#print $file,"\n";

#}

my $msg = MIME::Lite->new(
From => 'tsm_reports@weirminerals.com',
To => 'scott.pichelman@weirminerals.com',
Cc => 'some@other.com, some@more.com',
Subject => 'TSM DR Plan',
Type => 'multipart/mixed',
);

$msg->attach(
Type => 'TEXT',
Data => "$file",
);

$msg->send('smtp', '10.97.24.104', Timeout => 60 );


}



Ideas? <smile>

Thx.

-P


FishMonger
Veteran / Moderator

Apr 1, 2009, 7:34 AM


Views: 23147
Re: [pichels] Find file w/ date suffix and email attachment?

Ok, so what part do you need help with; the actual file attachment?

What type of file are you attaching, pdf, xls, txt?

Please enclose your code with the code tags.


(This post was edited by FishMonger on Apr 1, 2009, 7:34 AM)


pichels
Novice

Apr 1, 2009, 7:38 AM


Views: 23144
Post deleted by pichels

 


FishMonger
Veteran / Moderator

Apr 1, 2009, 8:09 AM


Views: 23136
Re: [pichels] Find file w/ date suffix and email attachment?

When you're composing your post, you will see several buttons, one of them is labeled 'code'. Click on that button to insert the opening code tag, then paste in your code and click on the code button again to insert the closing code tag.

Your posted code will be separated from the comments and in a different font and will retain its indentation, like this:

Code
$msg->attach( 
Type => 'text/plain',
Path => $file,
Filename => $file,
Disposition => 'attachment',
);




pichels
Novice

Apr 1, 2009, 8:16 AM


Views: 5505
Re: [FishMonger] Find file w/ date suffix and email attachment?

Ok - thanks FM.

Here is the code w/ tags:



Code
 
!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite;
use POSIX qw(strftime);


my $tdate = strftime("%Y%m%d", localtime);
my $file;

#print $tdate,"\n";


my $mydir = ".";
opendir(DIR, "$mydir");
my @drmfile = grep(/PLAN.$tdate.*$/,readdir(DIR));
#my @drmfile = grep(/\.pl$/,readdir(DIR));
closedir(DIR);


#foreach $file (@drmfile) {

#print $file,"\n";

#}

#my $msg = MIME::Lite->new(
# From => 'tsm@abc.com',
# To => 'p@abc.com',
# Cc => 'some@other.com, some@more.com',
# Subject => 'TSM DR Plan',
# Type => 'multipart/mixed',
#);

#$msg->attach(
#Type => 'TEXT',
#Path => 'C:\\Scripts\\$file\',
#Filename => '$file',
#Disposition => 'attachment',
#Data => "$file",
#);

#$msg->send('smtp', '10.97.24.104', Timeout => 60 );


#}



FishMonger
Veteran / Moderator

Apr 1, 2009, 8:21 AM


Views: 5502
Re: [pichels] Find file w/ date suffix and email attachment?

My previous post shows how you need to attach the file. Adjust your script accordingly and see what happens.


pichels
Novice

Apr 1, 2009, 8:47 AM


Views: 5499
Re: [FishMonger] Find file w/ date suffix and email attachment?

Excellent! Works now.

Ok - Don't suppose I could trouble you w/ another question?

If I want to add more "text" files in two different dir's but they will have the "same" or consistent filenames - not changed by date - I could change/add more code to the MIME part ?


Code
 
msg->attach(
Type => 'text/plain',
Path => $file,
Filename => $file,
Disposition => 'attachment',
);

msg->attach(
Type => 'text/plain',
Path => 'C:\recoveryplans\',
Filename => 'devcng.out',
Disposition => 'attachment',
);


Right?
Thanks again!

-P



pichels
Novice

Apr 1, 2009, 9:23 AM


Views: 5497
Re: [pichels] Find file w/ date suffix and email attachment?

Ok - figured out.
Actually this seems to work fine...


Code
 
$msg->attach(
Type => 'text/plain',
Path => $file,
Disposition => 'attachment',
);

$msg->attach(
Type => 'text/plain',
Path => 'C:\\Mitel\\CommAddUsers.vbs',
Disposition => 'attachment',
);

$msg->attach(
Type => 'text/plain',
Path => 'C:\\Mitel\\HTTPCheck.txt',
Disposition => 'attachment',
);



Thanks again for your help!

-P