Attach file through sendmail

Is it possible to use sendmail to attach files?
Basically,  I need to create a CGI script that when executed, sends a certain file from the local server to a certain user.
Anyone know how to do that?
SG072297Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
You'd want to choose a boundary that doesn't appear anywhere in the content.
I randomly chose something that won't appear in a base64 encoding,
and which was particular to this question id.
0
 
bhattuCommented:
sendmail <address-of-the-recipient> < <filename> should work.

just replace the address-of-the-recipient with the address to which u want the message to be sent to. and replace filename with the file u want to send. hope that helps.

BTW, have u configured ur local server to be able to send mails?
0
 
SG072297Author Commented:
I tried that. Example:
sendmail user@server.com /sg/file.txt

I get:
/sg/file.txt... Cannot mail directly to files
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
rickyrCommented:
Hi....
See my Q in the UNIX networking area called...
Solaris 2.6 isp and mail thru ppp
I think we could be traveling along the same lines here.
regards
0
 
ozoCommented:
sendmail user@server.com < /sg/file.txt
0
 
bhattuCommented:
hi ozo/SG,

this is what i have proposed.

sendmail user < filename

SG, have u tried this out? in ur comment u have missed out the
less-then '<' sign.

- bhattu.
0
 
ahoffmannCommented:
No, sendmail cannot be used to "attach files".

You just can send files using sendmail, which exactly means that sendmail uses the method specified in its configuration file (/etc/sendmail.cf) to deliver the mail data (read from stdin).

Keep in mind that sendmail sends the data as is, but the SMTP protocol expects and accepts only ASCII data.

If you want to attach a file (in any data format) to a message and deliver both via sendmail, you may use

      (cat message_file; uuencode data_file data_file) | sendmail user@server.com

But it's more common to use some of the various mail deliver programs (mail, deliver, etc.) to do this.

      
0
 
SG072297Author Commented:
Ok thanks, that works, but I have a problem. Say I'm trying to send an exe file or a doc file. When I get the email, it's mostly gibbrish.  How do I get the file to be encoded using standard base64 encoding. Like when I attach a file in Netscape, and I receive it, it's normal. How do I get that from the command line in Unix?
0
 
ozoCommented:
mmencode file.exe | sendmail

perl -mMIME::Lite -e '$m=new MIME::Lite Encoding=>"base64", Path=>"file.exe"; print $m->as_string;' | sendmail
0
 
SG072297Author Commented:
I tried the first line "mmencode file.exe | sendmail user@server.com"
and I got the email all weird.
I have no idea what you mean by the second line(the loooong one). Could you please explain?
0
 
SG072297Author Commented:
Let me just rephrase the question.
I need to make a CGI script that works like this. You fill out a form with your email address, and it sends you an email with an attached MS Word .doc file. How do I do the emailing part and with what?
0
 
ozoCommented:
What language are you writing your CGI in?
Since you've been asking about shell commands, and since it seems you don't have Perl, I'll give an sh script using mmencode, which it seems you do have:

(echo 'Content-Transfer-Encoding: base64'; echo 'Content-Type: application/octet-stream; name="file.exe"'; echo 'MIME-Version: 1.0'; echo; mmencode file.exe) | /usr/lib/sendmail user@server.com

0
 
bhattuCommented:
even uuencode should work? ozo, what's the difference between uuencode and mmencode? i have man pages for uuencode installed on my sun m/c but, do not have any information about mmencode. my guess is that: mmencode is for encoding a file in mime.
0
 
ozoCommented:
Both work, but it may be nice to match whichever decoder your user@server.com is more used to using.

mmencode is a common unix utility that encodes in the base64 format defined by MIME,
which differs from the uuencode format in using [AA-Za-z0-9+/]
instead of [` -_]
and also doing padding and line packing differently
0
 
SG072297Author Commented:
Thanks, it worked. But as soon as I added some text, it got all messed up. How do I setup the headers so I can put some text, then the file?

(I added 50 extra points)
0
 
ahoffmannCommented:
What do you mean by "setup the headers"?
Specifying Subject:, Cc:, etc. ?

Use somthing like:

  (cat message_file; uuencode data_file data_file) | mail user@server.com

or using ozo's suggestion:

   echo 'Content-Transfer-Encoding: base64'; echo 'Content-Type: application/octet-stream; name="file.exe"'; echo 'MIME-Version: 1.0'; echo; cat message_file; mmencode file.exe) | mail user@server.com

depending on your  mail  program you may specify Subject:, Cc:, etc. on the command line and/or in  message_file, please refer you man-pages.

0
 
ozoCommented:
#!/usr/bin/perl
use MIME::Lite;
$m=new MIME::Lite
        To      =>'user@server.com',
        Subject =>'A message with 2 parts...',
        Type    =>'multipart/mixed';
attach $m
        Type     =>'TEXT',
        Data     =>"Here's the exe file you wanted\n";
attach $m
        Type     =>'BINARY',
        Encoding =>'base64',
        Path     =>'file.exe';
$m->send;
0
 
ozoCommented:
#!/bin/sh
boundary='_----------=_10053648';
(echo 'Content-Type: multipart/mixed; boundary="'$boundary'"'
echo
echo 'This is a multi-part message in MIME format.'
echo
echo --$boundary
echo 'Content-Type: text/plain'
echo
echo "Here's the exe file you wanted"
echo
echo --$boundary
echo 'Content-Disposition: inline; filename="file.exe"'
echo 'Content-Transfer-Encoding: base64'
echo 'Content-Type: application/octet-stream; name="file.exe"'
echo
mmencode file.exe
echo
echo --$boundary--
echo)  |/usr/lib/sendmail user@server.com
0
 
SG072297Author Commented:
Thanks a lot OZO. That's exactly what I was looking for.
I just have one last question, how come is the boudary is "_----------=_10053648"?
I tried it with a whoel bunch of random boudaries like "32092--328" and "***" and it still worked. So how is the boundary chosen or why did you choose that one?
(Oh yeah, and you can enter an answer so I can give you the points)
0
 
ozoCommented:
You haven't graded this yet, was there something else you needed to know?
If you're not yet happy with the answer, or if you want to see a different approach, you can reject the answer and reopen the question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.