Link to home
Start Free TrialLog in
Avatar of kscope
kscope

asked on

attaching a file to email

i need to attach a file to an email message being generated by a perl script. what i've tried is some code offered up in a usenet group, but it's not working:
#!usr/bin/perl

$uuencoded = 'uuencode product.txt encoded.txt';

  open (MAIL, "|/usr/sbin/sendmail -t");
  print MAIL "From: julia\@avk.com \n";
  print MAIL "To: julia\@avk.com \n";
  print MAIL "Subject: Attachment Test \n";
  print MAIL "\n";
  print MAIL <<"END_OF_MESSAGE";

Hello there - this mail should come with an attachment entitled product.txt
END_OF_MESSAGE

  print MAIL $uuencoded;
  print MAIL "\n";
  close MAIL

the author swears he gets back an email message with the document attached. what i get is an email message with the body message on the first line, then "uuencode product.txt encoded.txt" on the second line. ideas, comments, suggestions?

thanks - jag
ASKER CERTIFIED SOLUTION
Avatar of alamo
alamo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
By the way, Perl also has a built in uuencode function
$uuencoded = pack 'u',$string;
Avatar of kscope
kscope

ASKER

changed the script to use the back ticks and now have a new problem - but hey, that's progress! anyway, now the mail comes back with the content of 'product.htm' printed in the body of the email - i need it to 'attach'

i presume the problem is in the print MAIL line ...but i have no clue what the syntax for 'attach' is.
Your script works for me - I receive the email with an attachment, and when I "save attachment" I get a good copy of the attached file. I don't know if that's because my mail client is smarter than yours, and recognizes the message as having an attachment even though it might not be completely correct.

First off, lets verify what you are sending is the same as what I am sending. As a test, Change the open MAIL line to:
open (MAIL, ">mail.out");
This will write the contents to a file. When I did so I got:

From: alamo@not.alamo.net
To: alamo@not.alamo.net
Subject: Attachment Test


    Hello there - this mail should come with an attachment entitled
product.txt.

begin 755 product.txt
M(R$O=7-R+V)I;B]P97)L"@HC($AI(&]Z;R Z+2D*"B1U=65N8V]D960@/2!@
M=75E;F-O9&4@=&5S='-M+G!L('!R;V1U8W0N='AT8#L*)'1O(#T@)V%L86UO
M0&YO="YA;&%M;RYN970G.PHC;W!E;B H34%)3"P@(GPO;&EB+W-E;F1M86EL
M("UT(BD[( IO<&5N("A-04E,+" B/FUA:6PN;W5T(BD[( IP<FEN="!-04E,
M(")&<F]M.B D=&]<;B([( IP<FEN="!-04E,(")4;SH@)'1O7&XB.R *<')I
M;G0@34%)3" B4W5B:F5C=#H@071T86-H;65N="!497-T(%QN(CL@"G!R:6YT
M($U!24P@(EQN(CL@"G!R:6YT($U!24P@/#PB14Y$7T]&7TU%4U-!1T4B.R *
M"B @("!(96QL;R!T:&5R92 M('1H:7,@;6%I;"!S:&]U;&0@8V]M92!W:71H
M(&%N(&%T=&%C:&UE;G0@96YT:71L960*<')O9'5C="YT>'0N"@I%3D1?3T9?
M34534T%'10H*<')I;G0@34%)3" D=75E;F-O9&5D.R *<')I;G0@34%)3" B
47&XB.R *8VQO<V4@34%)3#L*"@HD
 
end

Let me know if your script outputs the same thing essentially, including the begin and end lines. In the meantime I'll do some more investigation into what else is needed to mark it as an attachment.
Avatar of kscope

ASKER

alamo - here's what i got:

Hello there - this mail should come with an attachment entitled product.txt
begin 644 encoded.txt
=:&5L;&\@+2!I)VT@82!T97-T(&%T=&%C:&UE;G04
`
end

---that's certainly different!
Not so much different.... that decoded as "hello - i'm a test attachment" after I got rid of the backtick before end (my uudecode didn't like it). I included the mail headers, I think you just posted the message contents.

I checked and the pine mail reader doesn't recognize it as an attachment, whereas my Windows-based mail reader Pegasus does. I am doing some searching to see if there are some simple headers which can solve this, or whether full-blown MIME headers are needed.

There has to be a module available to do this, though if it's only a matter of a couple of headers  this way is simpler.
 print MAIL <<"END_OF_MESSAGE";
From: julia\@avk.com
To: julia\@avk.com
Subject: Attachment Test
Content-Type: multipart/mixed; boundary="$uniqueboundarystring";

This is a multi-part message in MIME format.

--$uniqueboundarystring
Content-Type: text/plain

   Hello there - this mail should come with an attachment entitled product.txt

--$uniqueboundarystring
Content-Disposition: attachment; filename="encoded.txt"

$uuencoded

--$uniqueboundarystring--
END_OF_MESSAGE

That looks very good, ozo. What I wonder though is whether some email packages expect more headers. Like everything else on the net the 'standards" are somewhat murky. I just sent a uuencoded attachment from Pegasus which pine couldn't decode, for example.

kscope, if the above doesn't solve your problems, you might want to take s look at the MIME::Lite package from CPAN. You have probably read about it on Usenet. It looks like it's simple to use and formats attachments easily. In terms of compability with email packages I personally would feel better using a module whose author has presumably spent a lot of time looking into what works, and which has lots of people filing bug reports when it doesn't.
I couldn't find a header for uuencoding,  If your mail reader does not support it, you may prefer to use base64 encoding
It's easy enough to write a perl function to translate frn uuencode to base64,
but as alamo suggests, it's easier to take advantage of what's already written.

use MIME::Lite;
$msg = new MIME::Lite
   From     => 'julia@avk.com',
   To:      => 'julia@avk.com',
   Subject  => 'Attachment Test',
   Type    =>'multipart/mixed';
attach $msg
   Type     =>'TEXT',
   Data     =>"Hello there - this mail should come with an attachment entitled encoded.txt\n";
attach $msg
   Encoding => 'base64',
   Path     => 'product.txt',
   Filename => 'encoded.txt';
#Wait a minute, why are you encoding text?
   $msg->send;
I did some testing of the MIME headers from ozo's message and found that on my mail reader I needed a couple of changes it make it work. First, it didn't recognize it was MIME at all until I added the header
MIME-Version: 1.0

It didn't know to uudecode the file unless I added (to that section):
Content-Transfer-Encoding: uuencode

although this seems not to be universal. base64 looks like a better option to me.

And to make the user see nice section descriptions I added
Content-description: (whatever)

to each of the sections.

That's all for now.
Avatar of kscope

ASKER

alamo - here's what i got:

Hello there - this mail should come with an attachment entitled product.txt
begin 644 encoded.txt
=:&5L;&\@+2!I)VT@82!T97-T(&%T=&%C:&UE;G04
`
end

---that's certainly different!
Avatar of kscope

ASKER

alamo - ozo;

ignore the repost of my message at 7:10am - it's early - i hit the wrong button ;-)

i'll run more tests this morning and let ya know what happens.....you've both put in a lot of effort, any way i can give you both points?

thanks again.....
Avatar of kscope

ASKER

thanks guys - our server lost a hard drive so it's taken me a couple days to get back to this project. i went the route of mime::lite and have just about got it working. just missing some library files [lost in the crash] reinstalling perl and libs today. thanks again for your help