Solved

Attach file through sendmail

Posted on 1998-05-19
20
594 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
sendmail user@server.com < /sg/file.txt
0
 

Expert Comment

by:bhattu
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
#!/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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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 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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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

9 Experts available now in Live!

Get 1:1 Help Now