|Forum Home > Unix Learnings > Sending mail with attachment from command line|
"mailx " packakge is very popular while sending mails from command line
You can even use it to send attachements. For that you would need install a utility "uuencode". Package name for this is "sharutils"( in linux).
Syntax for sending mail with attachment is :
uuencode filename filename | mailx -s " hi " [email protected]
Here first filename is name of the file you want to send as an attachment and second filename would become the name of the file when it gets attached to the mail. You can keep it same or different as per your convenience.
-s option with mailx specifies the subject of the mail.
For example, if I have a file named " test" which I want to send, I can send it as follows:
uuencode test test | mail -s " hi " [email protected]
However, "uuencode" sends file in binary format. There are some email clients which cannot decode it . So instead of attachment you would find an encoded text wriiten in email.
To overcome , there is an another package "mutt" , which can be used to send email attachment from terminal.
Syntax for "mutt" is :
mutt -s "hi " -a file -- use[email protected] < /dev/null
Here " -s " option specifies subject of the mail
"-a" option provides the file to be attached.
You can also attach multiple files with mail using "mutt" as :
mutt -s "hi " -a file1 file2 -- [email protected] < /dev/null
Make sure to separate attachments and destination address with "--"(two hyphen signs)
If you dont provide "/dev/null " as input to "mutt", it will open up in an interactive mode and ask some inputs . However, if you are using it in shell script , provide "/dev/null" as an input.