• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 878
  • Last Modified:

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
0
kscope
Asked:
kscope
  • 5
  • 5
  • 3
1 Solution
 
alamoCommented:
Your line

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

should be

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

While those may look the same, the difference is the second one uses back-ticks instead  of apostrophes. The back-ticks cause the command enclosed in `` to be run, returning the output of the command. In your case uuencode is run to uuencode the specified file and the output is placed into $uuencoded, which is then printed into the pipe sent to sendmail.

Hope this helps!

0
 
ozoCommented:
By the way, Perl also has a built in uuencode function
$uuencoded = pack 'u',$string;
0
 
kscopeAuthor Commented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
alamoCommented:
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.
0
 
kscopeAuthor Commented:
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!
0
 
alamoCommented:
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.
0
 
ozoCommented:
 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

0
 
alamoCommented:
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.
0
 
ozoCommented:
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;
0
 
alamoCommented:
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.
0
 
kscopeAuthor Commented:
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!
0
 
kscopeAuthor Commented:
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.....
0
 
kscopeAuthor Commented:
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
0

Featured Post

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!

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now