Sep 17, 2018

How To Send an Email With Attachment from Linux Command line

To send the attachment using uuencode: 

$ uuencode  test_msg test_attach  | mailx -s "email subject" [email protected]

test_msg is the file you want to attach.
test_attach is the file name the recipient will see.

To also send a body with the attachment:

$ ( cat my_body_file; uuencode  test_msg  test_attach ) | mailx -s "email subject" [email protected]

It is better to send attachment by MIME Standard ("Multipurpose Internet Mail Extensions") because MIME has more new features for e-mail messages, such as: 
  • arbitrary file attachments, e.g. images, audio files
  • more than one file attachment per mail
  • multi-part messages
  • non-ASCII character sets for message bodies and message headers.
mutt is an interactive, text-oriented e-mail client that can send attachment by MIME.

$ mutt -s "email subject" -a my_attach_file [email protected]my_body_file 
(You could have added multiple attachments by using multiple -a options.)

If the mail should consist of attachments only, we can either specify /dev/null as the file to read, e.g.

$ mutt -s "email subject" -a my_attach_file [email protected] < /dev/null 

or use an empty line as the mail body:

$ echo | mutt -s "email subject" -a my_attach_file [email protected]