Link to home
Start Free TrialLog in
Avatar of digs developer
digs developer

asked on

Unix shell script

How to send email with attachement through unix shell script ?
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of digs developer
digs developer


Could you please tell me where is the attached file syntax and how can I attcahed the test.txt file.
>>Could you please tell me where is the attached file syntax and how can I attcahed the test.txt file.

uuencode takes the file you want to attach and 'encodes' it so mailx can handle it.

uuencode test.txt   | mailx -s "sending an attachment"
mail or mailx do not take attachments directly. What can be done, is to create archive file then you may compress it, then uuencode the compressed file:

tar cf - file1 file2 file3 dir1 | compress | uuencode myfiles.tar.Z | mailx -s "email with attachments"
While mail or mailx is not capable of sending a multi-part message, sendmail, which is the underlying program to everything, is capable of doing it.  With the methods already posted, all of which will work, you can only send one attachment, and no body to the message.  The way around this is to create your own MIME encoded file and send that to sendmail.

While it sounds difficult, if you are sending only text files.  No special characters, no images, nothing like that, there is actually no need to encode anything.  What you need to do is to create a single text file that has your message and attachment included and then pass it to sendmail.

This page has an excellent example file ->

If you want to put in multiple attachements, not a problem.  Just create more boundaries and put all the relevant information to identify the additional attachments.

It has been quite a while since I have written a script to do this and I don't think I have a server set up right now that I can test it on, but those are the basics.  It really is pretty easy to do.  You just have to do the legwork that mail or mailx does.
Thank you !
>>Thank you !

This implies you found a solution.

If so don't forget to properly close out the question by accepting one or more posts that helped you resolve the question.
I've requested that this question be closed as follows:

Accepted answer: 0 points for digs developer's comment #a40277120

for the following reason:

I got the solution.

Thank you for your help !!
I have to again object to choosing your own post as the solution.  Especially when it isn't a solution.