Jun 28, 2017

How to send attachments from command line

Use the attachment switch (-a) in mailx to send attachments with the mail. 

# mailx -a attatch.txt -s "Subject" [email protected]
The above command will print a new blank line. Type the body of the message here and press [ctrl] + [d] to send. This will attach the file to the outbound email correctly with proper Content-Type and boundary headers.
To make this more “scriptable”, you can send the message without a body with the following:
# mailx -a file.txt -s "Subject" [email protected] < /dev/null
To send mails with a message body, replace /dev/null in above command with your message body file.

By mail command:

mail -s "your subject" [email protected] -A /your/attachment/absolute/path < /home/you/message.txt

The option attachment should be -A which is CAPITAL A.