CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 

First page Previous page 1 2 Next page Last page  View All


pichels
Novice

Mar 31, 2009, 12:57 PM

Post #1 of 29 (4763 views)
Find file w/ date suffix and email attachment? Can't Post

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

Post #2 of 29 (4762 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #3 of 29 (4761 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #4 of 29 (4758 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #5 of 29 (4755 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #6 of 29 (4752 views)
Re: [FishMonger] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #7 of 29 (4752 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #8 of 29 (4750 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post


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

Post #9 of 29 (4750 views)
Re: [FishMonger] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #10 of 29 (4746 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

Ok, now uncomment the use strict line and add.

use warnings;


pichels
Novice

Mar 31, 2009, 2:03 PM

Post #11 of 29 (4743 views)
Re: [FishMonger] Find file w/ date suffix and email attachment? [In reply to] Can't Post

Ok - done.

-P


pichels
Novice

Mar 31, 2009, 2:05 PM

Post #12 of 29 (4742 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #13 of 29 (4737 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post


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

Post #14 of 29 (4735 views)
Re: [FishMonger] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #15 of 29 (4733 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #16 of 29 (4727 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #17 of 29 (4724 views)
Re: [KevinR] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #18 of 29 (4721 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #19 of 29 (4719 views)
Re: [KevinR] Find file w/ date suffix and email attachment? [In reply to] Can't Post

Hi,

Ok - that makes sense!

I'll change my code again.

Thanks.

-P


pichels
Novice

Apr 1, 2009, 7:12 AM

Post #20 of 29 (4712 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #21 of 29 (4710 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

Do you want to print the filename or its contents?


pichels
Novice

Apr 1, 2009, 7:21 AM

Post #22 of 29 (4707 views)
Re: [FishMonger] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #23 of 29 (4704 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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

Post #24 of 29 (4701 views)
Post deleted by pichels [In reply to]

 


FishMonger
Veteran / Moderator

Apr 1, 2009, 8:09 AM

Post #25 of 29 (4693 views)
Re: [pichels] Find file w/ date suffix and email attachment? [In reply to] Can't Post

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',
);



First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives