Solved

attaching a file to email

Posted on 1998-07-12
13
808 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
0
Comment
Question by:kscope
  • 5
  • 5
  • 3
13 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 200 total points
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
By the way, Perl also has a built in uuencode function
$uuencoded = pack 'u',$string;
0
 

Author Comment

by:kscope
Comment Utility
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
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
 

Author Comment

by:kscope
Comment Utility
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
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
Comment Utility
 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
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
 

Author Comment

by:kscope
Comment Utility
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
 

Author Comment

by:kscope
Comment Utility
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
 

Author Comment

by:kscope
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now