Blog@Net

Friday, May 1, 2009

Sending Attachments With Mail In Unix

Don't we think sometimes to send attachments also with email message ?

Definitely, sometimes we feel that it would be just great if we can give some attachment option to our simple mail programs so that my message could include a file as attachment as well apart from body message .

Let us try to understand basics behind this attachment stuff .

In Unix Like OSes, we have a number of mail commands/utilities which usually come free with distro such as
1. mail ( Version 6 AT&T UNIX )
2. mailx ( Open Group At IEEE )
3. sendmail( 4.2 BSD)

There are some other utilities which have some better features and encoding standards ( supports MIME ) to support like mpack ( http://ftp.andrew.cmu.edu/pub/mpack/ ) ,mutt ( http://www.mutt.org/ ) , metamail , sendfile etc;


The first mail utility which I came across during college days is the email client Pine ( http://www.washington.edu/pine/tutorial.4/index.html ) which was of real interest since it was having menus and features very similar to viewing stuffs in C Program. It is now Alpine ( http://www.washington.edu/alpine/ )


Let us consider following inputs for sending a mail with attachment:


export INPUT_MSG_FILE=${HOME}/mail.txt ==> To include in body as mail message .
export ATTACH_FILE=${HOME}/input.txt ==> The file to be attached .
export ENCODED_FILE=${HOME}/ctewary.txt ==> The encoded file which will be relayed to receiver .
export MAIL_SUB="Welcome To CTEWARY BLOG" ==> Mail Subject
export TO_LIST="ctewary@gmail.com" ==> Recipient Mail Ids Separated By Space

Basically for binary files, we need to encode the attachment file with some standard encoding ( should be supported by recipient email agent ) so that it arrives in the same byte order to recipient ( addition and deletion of control bytes at various levels of message transmission) .

One of the widely used encoding technique is Unix-To-Unix Encoding which expands the file by 37% as it adds some control info ( header info , hashsum , etc;) .

There is also one encoding technique is base64 encoding which expands the file by 35% . If you really want to send some huge file then base64 is recommended as 2-3% of a huge file will also matter in network traffic .

Both of these encoding techniques are supported by uuencode command of unix shell and it is also a free distribution .

uuencode needs few parameters: 1. The full path of original attachment file 2. The encoded file name . It is the file name which will be seen at the receiver end.

The default encoding format is Unix-To-Unix and it uses base64 encoding format of the option -m is given on the command line .



1. Single Attachment:
uuencode ${ATTACH_FILE} ${ENCODED_FILE} | mail -s "${MAIL_SUB}" "${TO_LIST}"

2. Multiple Attachments:
(uuencode ${ATTACH_FILE_1} $[ENCODED_FILE_1}; uuencode ${ATTACH_FILE_2} $[ENCODED_FILE_2}) | mail -s "${MAIL_SUB}" ${TO_LIST}"

3. Message with Attachment and base64 encoding:
(cat ${INPUT_MSG_FILE};uuencode -m ${ATTACH_FILE} ${ENCODED_FILE}) | mail -s "${MAIL_SUB}" "${TO_LIST}"

**: We can also use mailx/sendmail instead of mail . I have taken here mail as it is the first mail prgram in unix systems starting from 1971 came under 1st Unix Edition Manual http://cm.bell-labs.com/cm/cs/who/dmr/1stEdman.html .

You can refer below link for more history of various mail programs available in unix systems :

http://heirloom.sourceforge.net/mailx_history.html



This uuencode encoding format does not involve portability and uudecode which is used to decode the encoded file using uuencode is not supported explicitly by many mail agents . The "uuencode" encoding is not standardized. Text encoded on one system may not be decoded correctly by the recipient's system.

Multipurpose Internet Mail Extensions is one method which is widely supported in all mail agnets for this encoding and decoding .
In unix shell, we can not have default installs of MIME mailing packages but, we can easily get utilities like mpack .

0 comments:

Search@Blog

Loading...

World: News@Net

Loading...

India: News@Net

Loading...

DO U NO :-)

"On a train from Shimla to Delhi, there was a halt in one of the stations. The train stopped by for few minutes as usual. Sachin was nearing century, batting on 98. The passengers, railway officials, everyone on the train waited for Sachin to complete the century. This Genius can stop time in India!!”.....He is my Fav ....Sachhhh Is Life !!

Saluting Netaji !!

Pending Tasks

Pending Tasks
Taare Zameen Par !!!

Geo@Net

ip-location