Posts Tagged ‘shell’

Send Mail with Attachment from the Commandline

November 23, 2011 Leave a comment

Under Linux, you can send some file “test.pdf” attached to an email by typing:

uuencode test.pdf test.pdf | mailx -s "Title for my email"

So what’s going on there? uuencode takes two input parameters: the file to read and the filename how it should appear at the mail. The file is converted to an ASCII-string and piped to mailx. The email is created with the given title (after the -subject flag) and then send to the given email address. The text-encoded file as piped from uuencode is attached to the body. Any modern email client is able to recognize this attachement.

The following script can be used to send a file including a subject to some fixed email. Some help is printed if it is called with the wrong number of parameters.

if [ $# -ne 2 ]; then
        echo "sendfile <filename> \"title\""
        exit 1
uuencode $1 $1 | mailx -s "$2"

Just save these lines in some file (e.g., sendfile) and make it executable (e.g., with chmod 755 sendfile).