?
Solved

Add an 'attachment' for an Email script

Posted on 2005-03-03
24
Medium Priority
?
242 Views
Last Modified: 2010-07-27
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;
0
Comment
Question by:tmccar10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
  • 3
  • +1
24 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 13451543
use MIME::Lite;
0
 

Author Comment

by:tmccar10
ID: 13452131
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.


0
 
LVL 48

Expert Comment

by:Tintin
ID: 13452968
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";
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 13455793
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.
0
 

Author Comment

by:tmccar10
ID: 13457520
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 13457705
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 13457763
Another alternative to MIME::Lite may be MIME::Entity
0
 

Author Comment

by:tmccar10
ID: 13457959
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

0
 
LVL 84

Expert Comment

by:ozo
ID: 13458108
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 '.';
 
0
 
LVL 84

Expert Comment

by:ozo
ID: 13458207
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"
0
 

Author Comment

by:tmccar10
ID: 13459871
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!
0
 
LVL 20

Expert Comment

by:jmcg
ID: 13464549
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 13464826
$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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 13464883
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.
0
 

Author Comment

by:tmccar10
ID: 13465737
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
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 160 total points
ID: 13472255
Can't believe that people are still running 5.003 on production systems.  5.003 is absolutely ancient now.
0
 

Author Comment

by:tmccar10
ID: 13472810
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


0
 

Author Comment

by:tmccar10
ID: 13475975
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 13476002
 Path =>  '/export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/pslplan/partadv/pdf',
  Filename => 'mgrchart.pdf',
0
 

Author Comment

by:tmccar10
ID: 13476766
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
0
 
LVL 84

Accepted Solution

by:
ozo earned 240 total points
ID: 13479744
You probably had a $msg->attach without a Path or Data
or a MIME::Lite->new without a Path or Data or Type =>'multipart/mixed'

my $msg = MIME::Lite->new (
        From    => $email,
        To      => $email,
        Cc      => $email,
        Subject => 'Your subject',
        Type    =>'multipart/mixed',
);
$msg->attach
 (
  Type => 'application/pdf ',
  Path =>  '/export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/pslplan/partadv/pdfs',
  Filename => 'mgrchart.pdf',
);
print $msg->as_string;

or

my $msg = MIME::Lite->new (
        From    => $email,
        To      => $email,
        Cc      => $email,
        Subject => 'Your subject',
  Type => 'application/pdf ',
  Path =>  '/export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/pslplan/partadv/pdfs',
  Filename => 'mgrchart.pdf',
);

or

my $msg = MIME::Lite->new (
        From    => $email,
        To      => $email,
        Cc      => $email,
        Subject => 'Your subject',
        Data => "Here's the PDF file you wanted"
);
$msg->attach
 (
  Type => 'application/pdf ',
  Path =>  '/export/ford/eccws7/u/mso/www.86.80/fcsd/psl/namer/pslplan/partadv/pdfs',
  Filename => 'mgrchart.pdf',
);
0
 

Author Comment

by:tmccar10
ID: 13481389
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

0
 
LVL 48

Expert Comment

by:Tintin
ID: 13482392
What email client are you using to read the email?
0
 

Author Comment

by:tmccar10
ID: 13482919
Thanks -

I'm using Outlook.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question