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
# 1  
Old 01-05-2010
Attach a binary file to email in a script

Hi,
I am trying to get an email sent out by the unix ( aix ) system that has a .gz file attached to it.
I can get the attachment, but it's not working when being looked at from outlook.
I think there is a problem because of the way I am doing it, and the fact that it's binary.

I am trying to use mime type to attach it. This works with ascii files but not with the binary .gz file. I am thinking it's because of the way I am catting the file into the message. Not sure.
Is there a better way to do this ?

Code:
echo "To: me" >$msgfile
echo "Subject: informix specific files for DR `date` " >>$msgfile
echo "Mime-Version: 1.0" >> $msgfile
echo "Content-Type: multipart/mixed; boundary=\"DMW.Boundary.605592468\"" >>$msgfile
echo "" >>$msgfile
echo "--DMW.Boundary.605592468" >>$msgfile
echo "Content-type: application/octet-stream; name=\"drfile.gz\"" >>$msgfile
echo "Content-Disposition: attachment;filename=\"drfile.gz\"" >>$msgfile
echo "Content-Transfer-Encoding: 7bit" >>$msgfile
echo "" >>$msgfile
cat drfile.gz >>$msgfile
echo "--DMW.Boundary.605592468--" >>$msgfile
cat $msgfile|/usr/lib/sendmail -t

Thanks,
floyd

Last edited by Scott; 01-05-2010 at 11:37 PM.. Reason: Added code tags
# 2  
Old 01-05-2010
You should convert the file to mime format with b64encod(e), and I think that you need a blank line after the "Content-Transfer-Encoding" line.
# 3  
Old 01-05-2010
Thanks.
I tried to do
uuencode -m file.gz and pipe that to mailx, but it doesn't work.
Looks like it needs an output file as an argument, but it still only outputs garbled stuff to the standard out.
# 4  
Old 01-05-2010
Code:
uuencode file.gz file.gz | mailx .....

# 5  
Old 01-05-2010
Quote:
Originally Posted by vgersh99
Code:
uuencode file.gz file.gz | mailx .....

So I ran
Code:
 uuencode -m test.tar.gz test.tar.gz  | mailx  floydw@mail.org

and I got an email that looked like this:
Code:
begin-base64 644 test.tar.gz
H4sICChVQ0sAA3Rlc3QudGFyAOz9W5MjS3aYC+p5/wo85jycasQ9QjY2ZjwSpcMxSuSQLdnMvJRl
4pJVrURlCkBVo//9QbhHeEQ4Au5Y3wKK3cUNaSeNm3vF8su6+bp++sN6/3n79W1z+MN/eNRvmS+r
olj8h+VymeWZ/Z+J/d/73yJJsnRZFmmWZotlkqdJ9R8WxcNWNPp9Pxyf9+elHDbfjvu/rLbX/jv7
f7/+nW4f7n/+jfw+Dfe/ev+2/fr66XDcvL09f0qXyfL8/4vlMk2UOM4fKvN8cXFE5z9J+yvL1Lv/
KqmK/7BY3mWHkV9///v392Povzv85Xwwu+v/97/R+/8/tL/f/l/n3+LL++H47Xm3Wfw/z7/f9B+1
RHiHD5nVrZ+Pd1vZH79vFv/v52+LRbFYLv9jmvzHM8H+/b/+cdHyy73W+3542/zYvN1rycWn7NPy
091W93b4+HGvpX1Zfz38r+XC/c6sU9bbdbparrdJ2TwvZn4tp/54Hf7359Xx64+N/VRy+al0k2Vp
ui6ln0rnPrV6rjfVeu5TX78dN/tvz2+fu++NP5Vd+9RzIf1UPv7UqqqS9ap4Of95aaq5T/3Lvyaf
/7Q99F+afKq48qnVupF+qrzyqU2ylH6quvKpbbqVfqqe/9R6mW+kn2pmP7VplkU9/6n06qdGRzL5
VDJP7cFPJfOfStdXiCHwqXT+U/kyEX8qm/9Uka6knyovP1U16+SlrOWfymc+tU1WdTXLg6FP1eNV
WXbOnsvqJS1mV/XyvPpf3z8GMTP5VD7/qefN7KqCnypmP7UqXzLxp8rZT62XxSxdBT9VzX/qucnF
n6pnPlU0xSaZ5cH1S/L56/Y0f4PN3KfWz5t0ltrXL+nVTzXLuU9tspd0VuOsX7Lrn0pmP7U6U/z8
.......


Last edited by Scott; 01-05-2010 at 11:39 PM.. Reason: Added code tags
# 6  
Old 01-05-2010
Code:
echo "subject: Something" ; cat $text_mail ; uuencode file.gz file.gz) | /usr/sbin/sendmail john@doe.nl

# 7  
Old 01-05-2010
Quote:
Originally Posted by fwellers
So I ran uuencode -m test.tar.gz test.tar.gz | mailx floydw@studentclearinghouse.org

and I got an email that looked like this:
begin-base64 644 test.tar.gz
H4sICChVQ0sAA3Rlc3QudGFyAOz9W5MjS3aYC+p5/wo85jycasQ9QjY2ZjwSpcMxSuSQLdnMvJRl
4pJVrURlCkBVo//9QbhHeEQ4Au5Y3wKK3cUNaSeNm3vF8su6+bp++sN6/3n79W1z+MN/eNRvmS+r
olj8h+VymeWZ/Z+J/d/73yJJsnRZFmmWZotlkqdJ9R8WxcNWNPp9Pxyf9+elHDbfjvu/rLbX/jv7
f7/+nW4f7n/+jfw+Dfe/ev+2/fr66XDcvL09f0qXyfL8/4vlMk2UOM4fKvN8cXFE5z9J+yvL1Lv/
KqmK/7BY3mWHkV9///v392Povzv85Xwwu+v/97/R+/8/tL/f/l/n3+LL++H47Xm3Wfw/z7/f9B+1
RHiHD5nVrZ+Pd1vZH79vFv/v52+LRbFYLv9jmvzHM8H+/b/+cdHyy73W+3542/zYvN1rycWn7NPy
091W93b4+HGvpX1Zfz38r+XC/c6sU9bbdbparrdJ2TwvZn4tp/54Hf7359Xx64+N/VRy+al0k2Vp
ui6ln0rnPrV6rjfVeu5TX78dN/tvz2+fu++NP5Vd+9RzIf1UPv7UqqqS9ap4Of95aaq5T/3Lvyaf
.......
... which is perfectly fine, since that is how it is supposed to look.

You'd then proceed to save the message and uudecode it to obtain the "test.tar.gz" file.

tyler_durden
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