We help IT Professionals succeed at work.

attaching a file to email

kscope
kscope asked
on
Medium Priority
944 Views
Last Modified: 2008-03-04
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
Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
By the way, Perl also has a built in uuencode function
$uuencoded = pack 'u',$string;

Author

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.

Commented:
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.

Author

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!

Commented:
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.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
 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

Commented:
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.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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;

Commented:
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.

Author

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!

Author

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.....

Author

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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.