Solved

Attach file through sendmail

Posted on 1998-05-19
20
596 Views
Last Modified: 2013-12-26
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?
0
Comment
Question by:SG072297
  • 8
  • 6
  • 3
  • +2
20 Comments
 

Expert Comment

by:bhattu
ID: 1293570
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
 

Author Comment

by:SG072297
ID: 1293571
I tried that. Example:
sendmail user@server.com /sg/file.txt

I get:
/sg/file.txt... Cannot mail directly to files
0
 
LVL 3

Expert Comment

by:rickyr
ID: 1293572
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
 
LVL 84

Expert Comment

by:ozo
ID: 1293573
sendmail user@server.com < /sg/file.txt
0
 

Expert Comment

by:bhattu
ID: 1293574
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1293575
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
 

Author Comment

by:SG072297
ID: 1293576
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
 
LVL 84

Expert Comment

by:ozo
ID: 1293577
mmencode file.exe | sendmail

perl -mMIME::Lite -e '$m=new MIME::Lite Encoding=>"base64", Path=>"file.exe"; print $m->as_string;' | sendmail
0
 

Author Comment

by:SG072297
ID: 1293578
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
 

Author Comment

by:SG072297
ID: 1293579
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 1293580
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
 

Expert Comment

by:bhattu
ID: 1293581
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
 
LVL 84

Expert Comment

by:ozo
ID: 1293582
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
 

Author Comment

by:SG072297
ID: 1293583
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1293584
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
 
LVL 84

Expert Comment

by:ozo
ID: 1293585
#!/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
 
LVL 84

Expert Comment

by:ozo
ID: 1293586
#!/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
 

Author Comment

by:SG072297
ID: 1293587
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
 
LVL 84

Accepted Solution

by:
ozo earned 220 total points
ID: 1293588
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
 
LVL 84

Expert Comment

by:ozo
ID: 1293589
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

863 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

26 Experts available now in Live!

Get 1:1 Help Now