Attach a binary file to email in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Attach a binary file to email in a script
# 8  
Old 01-05-2010
I think I am getting the gist.
I am able to encode and decode a file on Unix, and it works fine.
However, if I mail the encoded file, save that email message, ftp it back to unix and then try and decode it, I can't. It says Line too long. ( I got rid of the extra email header stuff ).
I think it may have to do with the extra ^M ( \r\n ) that gets put on in the windows email client or system.
I tried to get rid of that also, no joy.

Any further ideas about that ??

Thanks,
Floyd
# 9  
Old 01-05-2010
Quote:
Originally Posted by fwellers
... save that email message, ...
Save in Windows ?

Quote:
... and decode it, I can't. It says Line too long. ( I got rid of the extra email header stuff ).
Not sure how you uudecode it, but if I -
(a) mail an attachment to my own Linux user
(b) view it using "mail" command
(c) save the email, as a text file, in my current directory in Linux
(d) run uudecode on that text file

then I get the original attachment "test.tar.gz" back. Even when the headers and all are seen in the text file.

Shown below -

Code:
$                                                    
$ mail                                               
...
...
& p                                                    
Message  1:                                            
...
...
Content-Type: text/plain; charset=us-ascii               
Status: R                                                

begin-base64 664 test.tar.gz
H4sICFlkQ0sAA3Rlc3QudGFyAO1YfWwcRxWfXe85l8RO/JGQtDbVqTgQS/h6
556/oEj+TOLGsV3bDW7TZHN3u/YevS/d7RW7ikSTiymuY2EBriKBkCIh1D9S
iapVZamhSnFpqOCPAP9EAUEBFy7ERa0aQv4oWd6bmd2b27hFgIpAuneanfnN
/Oa9t2
...
...
vtR0VDWNXPJxf2SG0P862MyylKUsZSlLWcpSlrKUpSxlKUtZylKWspTlf1D+
AenpsMcAKAAA                                                
====

& save myfile
"myfile" [New file] 74/3938
& q
$
$ # check if the email was saved as "myfile" in current directory
$ ls
myfile
$
$ # uudecode myfile now
$ uudecode myfile
$
$ # now check again
$ ls
myfile  test.tar.gz
$
$

Quote:
...
I think it may have to do with the extra ^M ( \r\n ) that gets put on in the windows email client or system.
I tried to get rid of that also, no joy.

Any further ideas about that ??
...
Not sure why you'd want to "save email in Windows, ftp back to *nix, uudecode back to original attachment".
If you are able to send the view the attachment correctly (in Windows), then your purpose is served, isn't it ?

In any case - I do not have that kind of set up (emails to Internet addresses) in my Linux box, but I'd say try this -

(1) Send the attachment to a *nix user in your *nix box.
(2) Send the same email to an Internet address.
(3) Download, save and ftp the email back to *nix.
(4) Save the email sent in Step (1) to a local directory in *nix.
(5) Do a diff between the files in (3) and (4). Quite possibly, the Windows mail agent adds some extra stuff.

tyler_durden
# 10  
Old 01-05-2010
i remember wrestling with this problem years ago...

in order to receive the email, and see the "uuencode" output as an attachment, I believe you needed something like this:

Code:
(
cat << EOF
TO: joker@batman.com
FROM: riddler@chaos.com
SUBJECT: bananas rochester
CONTENT-TYPE: html/text;

I will crush you.

CONTENT-TYPE: binary;

EOF

uuencode chicken_bomb.gz  not_a_chicken_bomb.gz

) | /usr/lib/sendmail -t

That 'content-type: binary;' is wrong but . . .

there was some way of telling sendmail that the second "content" was an attachment.
Darned if I can recall what it was or find it though....

But the email ~did~ show up with the standard attachment, and not the uuencoded stuff.

But if someone could check the output of the perl module: Sendmail.pm then we'd have the answer.
# 11  
Old 01-05-2010
This has worked for me but it's been a while since I've used it. You'll want to at least modify the To/From lines:

Code:
tmpfile=/tmp/mimetmp.$$
basefile=`basename $your_file`

cat > $tmpfile << EOT
From: there@there.com
To: here@here.com
Subject: The subject
MIME-Version: 1.0
Content-Type: multipart/mixed;boundary="mime-attachment-boundary"

--mime-attachment-boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

A MIME encoded file is attached called: $basefile
--mime-attachment-boundary
Content-Type: application/octet-stream; name="$basefile"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$basefile"
EOT

mimencode $your_file >> $tmpfile
echo --mime-attachment-boundary-- >> $tmpfile
/usr/lib/sendmail -t < $tmpfile
rm $tmpfile

See more details in Chapter 23 of "Expert Shell Scripting"
# 12  
Old 01-05-2010
You can try this, should work for html/txt message body with multiple attachments

Code:
#!/usr/bin/bash
#
#    send_mail_html.sh (send_mail_html/txt_body_plus_attachments) 
#U
#U  Usage:
#U         ##SCRIPT_NAME## "<TO:email1@sample.com,email2@sample.com>" "<Subject:>" "<html_BODY_file>" "<File-1>" "<File-2>" "<File-nth>"
#U
#
Script_name=$(echo $0 | tr '\/' '\n' | tail -1)

########################################
#                                      #
#
Usage_F(){ ###+ START
        echo $1
        echo "Incorrect Syntax, missing arguments.  Exiting ...";
        grep ^#U $0 | sed -e s/^#U// -e s/##SCRIPT_NAME##/$Script_name/;
        exit 1;
} ###+ END Usage_F
#                                      #
########################################

[ $# -lt 4 ] && { Usage_F; }

TO="$1"
SUBJECT="$2"
BODY="$3"
FILE_ATTACHMENT="$4"
BOUNDARY="-"
FROM="email_from@goes-here.us"

#M0 From:##FROM##
#M0 To:##TO##
#M0 Mime-Version:1.0
#M0 Content-Type:Multipart/mixed;boundary=Message-Boundary-##BOUNDARY##
#M0 Subject:##SUBJECT##

#M1 
#M1 --Message-Boundary-##BOUNDARY##
#M1 Content-type:text/html;charset=ISO-8859-1
##US-ASCII
#M1 Content-transfer-encoding: 7BIT
#M1 Content-Disposition:inline
#M1 Content-Description: Read Me First
#M1 
###_GET_HTML_CONTENT_HERE

#M2 --Message-Boundary-##BOUNDARY##
#M2 Content-type:Application/Octet-Stream;name=##FILE_ATTACHMENT##;type=Binary
#M2 Content-disposition:inline;filename=##FILE_ATTACHMENT##
#M2 Content-transfer-encoding: X-UUencode
#M2 Message-Boundary-##BOUNDARY##
#M2 
###_uuencode ./${FILE_ATTACHMENT} ${FILE_ATTACHMENT} >>mail_out

###_Sample of mailx sending attachment with message in text format (NO HTML)
##M TO="##TO##"
##M SUBJECT="##SUBJECT##"
##M FILE_ATTACHMENT="##FILE_ATTACHMENT##"
##M cat <<EOF | ( cat -; uuencode ./$FILE_ATTACHMENT $FILE_ATTACHMENT; ) | mailx -s "$SUBJECT" "$TO"
##M --Body_here_optional--

TMP=_tmp_mail_out_$(date '+%H%M%S%Y%m%d')
egrep '^#M0|^#M1' $0 | sed -e s/^#M.\ // -e s/##FROM##/$FROM/ -e s/##TO##/$TO/ -e s/##SUBJECT##/$SUBJECT/ -e s/##FILE_ATTACHMENT##/$FILE_ATTACHMENT/g -e s/##BODY##/$BODY/ >$TMP
cat $BODY >>$TMP

shift 3;
while [ ! -z $1 ]; do
        echo $1
        if [ -f $1 ]; then
                grep ^#M2 $0 | sed -e s/^#M.\ // -e s/##FROM##/$FROM/ -e s/##TO##/$TO/ -e s/##SUBJECT##/$SUBJECT/ -e /##FILE_ATTACHMENT##/$1/g -e s/##BODY##/$BODY/ >>$TMP
                uuencode ./$1 $1 >>$TMP
        else
                rm -f $TMP; Usage_F;
        fi
        shift
done

/usr/lib/sendmail "$TO" <$TMP
rm -f $TMP

exit 0

# 13  
Old 01-05-2010
Thanks for all the replies !!
I think I got it worked out.
The remaining crux of my problem was what windows was doing to the file. once I stripped the \r off of each line, and added a \n to the last character of the file ( that windows stripped off ), it seemed to work fine.

Great stuff, great forum.

thanks again !!
Floyd

---------- Post updated at 06:05 PM ---------- Previous update was at 05:40 PM ----------

Quirkasaurus,
Just wanted to let you know, that this is the best one yet.
I didn't even have to do any decoding when it went back over to the unix side. Worked like a charm.

Thanks much.

Floyd



Quote:
Originally Posted by quirkasaurus
i remember wrestling with this problem years ago...

in order to receive the email, and see the "uuencode" output as an attachment, I believe you needed something like this:

Code:
(
cat << EOF
TO: joker@batman.com
FROM: riddler@chaos.com
SUBJECT: bananas rochester
CONTENT-TYPE: html/text;

I will crush you.

CONTENT-TYPE: binary;

EOF

uuencode chicken_bomb.gz  not_a_chicken_bomb.gz

) | /usr/lib/sendmail -t

That 'content-type: binary;' is wrong but . . .

there was some way of telling sendmail that the second "content" was an attachment.
Darned if I can recall what it was or find it though....

But the email ~did~ show up with the standard attachment, and not the uuencoded stuff.

But if someone could check the output of the perl module: Sendmail.pm then we'd have the answer.
# 14  
Old 01-05-2010
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check Error File attach and email zip file

I need something to say if these two file extensions exist in this directory *err and *rpt zip up these files into one zip file and email them to me. If they don't exist wait 2 hours and check again.... Not sure how to determine if I need to do an if then statement or a while true or a for... (1 Reply)
Discussion started by: xgringo
1 Replies

2. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

3. Shell Programming and Scripting

Script to attach file to mail

Hello, I have a .dat file containing one line. I need a script to read that line and make it part of the body and send a mail... Let's say the line is $line. I need the script to send a mail with the body "The last disposal feed is $line". Thanks (4 Replies)
Discussion started by: sfetea
4 Replies

4. Shell Programming and Scripting

Not able to attach text in body of email while sending mail with attachment

Hi, We have been trying to send mail with attachment and it is going fine, but when we try to attach a text to the body of the email, we find that the mail is going fine with the body text but the attachment is not going through. We are using ksh. The command that is successfull without the... (6 Replies)
Discussion started by: jmathew99
6 Replies

5. UNIX for Dummies Questions & Answers

How to attach a file in a email

Anyone can help me, in Unix, how can I attach a file in the email? Thank (3 Replies)
Discussion started by: ting123
3 Replies

6. Shell Programming and Scripting

attach multiple file in an email

Hello I have to attach multiple file as an email attachment. here is what i my understanding so far: 1. search in a certain directory that is there any specific file say for example .xml file exist or not 2. if exist then take those file name from the folder and attach it to another text... (1 Reply)
Discussion started by: osrukarigor
1 Replies

7. Shell Programming and Scripting

attach multiple files in email

I am trying to send multiple files as attachment in one email, I tried to search but couldn't find. Please let me know if any solutions. (2 Replies)
Discussion started by: mgirinath
2 Replies

8. UNIX for Dummies Questions & Answers

How to use "mail" to attach a file with an email?

echo "content" | mail email@address -s subject Where to attach a file with the email? (2 Replies)
Discussion started by: meili100
2 Replies

9. Shell Programming and Scripting

hi.. how to attach a tar file using shell script

Hi, How to attach a tar file using shell script or the command liine.. I following command just send the mail to the person with .txt file as body, I want to send it as attachment. /usr/sbin/sendmail -f "user1@daemon.com" user2@daemon.com <hi.txt The contents of the hi.txt will be... (1 Reply)
Discussion started by: madhumathikv
1 Replies

10. Shell Programming and Scripting

How to attach a file & send mail thru script

In shell script how can I attach a file and send a mail. suppose if I written like the following way usr/bin/mail 'subject" "mail_id" < file. a mail goes to the mail-id with the content of file.But I want the file to be atttached to the mail.How can I get it.is there any way for this. ... (9 Replies)
Discussion started by: Mar1006
9 Replies
Login or Register to Ask a Question