Link to home
Create AccountLog in
Avatar of Thom McCarthy
Thom McCarthyFlag for United States of America

asked on

Add an 'attachment' for an Email script

I got a nice little script from tintin a while ago.

I wanted to add provisions to include an attachment. I've spent the last hour-and-half trying to figure what to do based on all the  examples I've looked at on line.

I'd really appreciate any help

Thanks-

*************************************************
Code's below
*************************************************
#!/usr/local/bin/perl

$at="\@ford.com";

open MAIL,"|/usr/lib/sendmail -t -oi" or die "Can not run sendmail $!\n";

print  MAIL<<EOF;

To:tmccar10$at
CC:tmccar10\@ford.com
From: tmccar10\@ford.com

Subject: Weekly Graphs

Attached are the graphs reflecting Ciscso Report


EOF

close MAIL;
Avatar of ozo
ozo
Flag of United States of America image

use MIME::Lite;
Avatar of Thom McCarthy

ASKER

Looks like I have a problem with the module, or maybe I just did it wrong.

Can you take a look and let me know?

Is there another way to do this, is this is the problem?

Thanks

*******************************
#!/usr/local/bin/perl

use MIME::Lite;

$at="\@ford.com";

open MAIL,"|/usr/lib/sendmail -t -oi" or die "Can not run sendmail $!\n";

print  MAIL<<EOF;

To:tmccar10$at
CC:tmccar10\@ford.com
From: tmccar10\@ford.com
.
.
.
$msg->attach (
   Type => 'application/pdf ',
   Path =>  /export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/partadv/pdf,
   Filename => mgrchart.pdf,
   Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";



EOF

close MAIL;

###############################

> graphnote.cgi
Bare word found where operator expected at /usr/local/lib/perl5/site_perl/MIME/"
Bareword "co" not allowed while "strict subs" in use at /usr/local/lib/perl5/si.
Bare word found where operator expected at /usr/local/lib/perl5/site_perl/MIME/"
Bareword "co" not allowed while "strict subs" in use at /usr/local/lib/perl5/si.
syntax error at /usr/local/lib/perl5/site_perl/MIME/Lite.pm line 372, near "m{\"
BEGIN failed--compilation aborted at graphnote.cgi line 3.


Avatar of Tintin
Tintin

There's loads of examples with MIME::Lite

What you need to do is:

#!/usr/local/bin/perl
use strict;
use MIME::Lite;

my $email = 'tmccar10@ford.com';

my $msg = MIME::Lite->new (
        From    => $email,
        To      => $email,
        Cc      => $email,
        Subject => 'Your subject',
);

$msg->attach (
  Type => 'application/pdf ',
  Path =>  /export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/partadv/pdf,
  Filename => mgrchart.pdf,
);

$msg->send or die "Can not send email $!\n";
Although your usage of MIME::Lite was not the correct way to send an attachment, it should not have caused the errors you were seeing.
It looks like there was also a problem with the way /usr/local/lib/perl5/site_perl/MIME/Lite.pm  was installed.
Could you show the complete error messags?  The ones you posted seem to have been cut off at 80 characters.
Thanks

Looks like ozo is right.

After running tintin's script the same message popped.

Looks like a problem with the copy of MIME we have.

I commented out the 'USE strict', since it was mentioned in the error message. (below)

*********
Bare word found where operator expected at /usr/local/lib/perl5/site_perl/MIME/"
Bareword "co" not allowed while "strict subs" in use at /usr/local/lib/perl5/si.
syntax error at /usr/local/lib/perl5/site_perl/MIME/Lite.pm line 372, near "m{\"
BEGIN failed--compilation aborted at graphnote1.cgi line 3.
*********
I'm not sure where to go fropm here.

Thanks
If you could see the rest of the error messages to see the line numbers that may give more of a clue what's wrong.  It may be as simple as a misplaced quotation mark.
Or you might just want to try re-installing MIME::Lite
I don't see any "m{\" near line 372 in $MIME::Lite::VERSION 3.01
I see "m{\G" on lines 493, 495, 497, 497
Another alternative to MIME::Lite may be MIME::Entity
Thanks ozo,

If you're seeing 4 lines starting with "Bare word found where operator..." and ending with "...compilation aborted at graphnote.cgi line 3.", your seeing everything that I see.

I don't imagine that I can simply replace "Lite" with "Entity", couldn't be that straight forward.

I don't have control over PERL here, I guess if that's what's wrong I've gotta got through the server folks. I work at a major Ford operation...this will be interesting.  

Thanks again

If you're always generating mails with the same format, the only non-trival part looks like the base64 encoding, which you could learn how to do from the MIME::Lite source, or ask one of us to write it for you.
But even if you don't hve control over /usr/local/lib/perl5/site_perl/MIME
you could still install MIME/Lite.pm on your own directory and
use lib '.';
 
Is your terminal output truncating lines at 80 characters?
Could you either tell your terminal to wrap, or redirect the error messages with either
graphnote.cgi 2> errlog
(in  sh, ksh, bash, ...)
or
graphnote.cgi >& errlog
(in csh, tcsh, ...)
they should look more like
Bare word found where operator expected at /usr/local/lib/perl5/site_perl/MIME/Lite.pm line 123, near "$foo bar"
Thanks ozo-

That's what I like about EE--especially the PERL people--you folks know EVERYTHING. I, on the other hand, face PERL code about three times a year...not the best learning model

Anyway, here the COMPLETE error message:

Bare word found where operator expected at /usr/local/lib/perl5/site_perl/MIME/L
ite.pm line 372, near "m{\G\s*\Z}gco"
Bareword "co" not allowed while "strict subs" in use at /usr/local/lib/perl5/sit
e_perl/MIME/Lite.pm line 372.
Bare word found where operator expected at /usr/local/lib/perl5/site_perl/MIME/L
ite.pm line 374, near "m{\G$SEP$PHRASE\s*<\s*($ADDR)\s*>}gco"
Bareword "co" not allowed while "strict subs" in use at /usr/local/lib/perl5/sit
e_perl/MIME/Lite.pm line 374.
syntax error at /usr/local/lib/perl5/site_perl/MIME/Lite.pm line 372, near "m{\G
\s*\Z}gco"
BEGIN failed--compilation aborted at graphnote1.cgi line 3.



Thanks!
Could you tell us what version of Perl you're running? (perl -v or perl -V should tell you) Is it still 5.003?

The error messages make it appear that your Perl interpreter is so old that it does not recognize the "c" modifier in the substitute operator. This change occurred somewhere between the 5.000 and 5.005 releases, as best I can tell. Of course, Mime:::Lite ought to have a require statement up front to more clearly document that it is dependent on a more recent version of Perl.

The "best" fix would be if you could get your system admins to update Perl to something newer.
$MIME::Lite::VERSION = "3.01" does have a
require 5.004;
If the compilation didn't get aborted by the co, you'd probably see an error from the require.
If you have Mail::Address the sub that uses the /c modifier does not appear to be used at all.
Perhaps you can just remove it.
As I said, you guys know everything!!!

We're running  perl 5.003

I'll ask out tech folks about this.

I'll get back to everybody then.

Is there an alternative if they give me any grief?

Thanks again for all the advice
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I contacted our web server folks.

It can't be the cost!

I hope, maybe that once somebody asks, they update...but I'm not hopeful.

I'll see if I get an answer tomorrow.

Thanks Tintin


My fault, I guess. They update fairly regularly, but you need to change version paths with each new version, the default is to the oldest (5003).
(I'm kicking up the points, you folks have spent alot of time on me)

I've got the right path now, but I get this message, and I can't see anything obvious, unless "export" is somehow a key word, or something

########################################################
Bareword found where operator expected at graphnote1.cgi line 19, near "/export/ford"
        (Missing operator before ford?)
syntax error at graphnote1.cgi line 19, near "/export/ford"
########################################################

**************************
$msg->attach
 (
  Type => 'application/pdf ',
  Path =>  /export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/pslplan/partadv/pdfs,
  Filename => mgrchart.pdf,
);
**************************

Thanks
 Path =>  '/export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/pslplan/partadv/pdf',
  Filename => 'mgrchart.pdf',
That took care of that--I thought I tried the single quotes before I wrote...

I delayed coming back because I got an error message I was trying to clear myself:

"no data in this part
 at graphnote1.cgi line 25"

25. ($msg->send or die "Can not send email $!\n";)

I tried using different pdf files, move them to the same directory and the perl program...I couldn't get past "no data". I looked on the web and could only fine that it was a problem, but not a reason or a way to fix it.

The mail sends, NOT the attachment.


Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Finally!!

Learned a lot this time. As usual, more than I expected.

Adding the Data => seemed to do it.

Still have a problem with opening the file in Adobe.
"...it was sent as an email attachment and wasn't decoded correctly"....more to learn.

If you know a quick answer I'll take it, but I'm letting everyone go, you've done plenty!

Thanks for sticking with me, you've all got great patience.

Thom

What email client are you using to read the email?
Thanks -

I'm using Outlook.