Sending Yourself a File as an Attachment From the Command Line

There are many occasions where I’ll have a file on a Linux machine that I want to email myself. If it is a text file, it’s pretty easy to just pipe the file to a mail command like this:

cat /home/brandon/some-file.txt | mail -s "Here is the file" some@body.com

That works well for the simple case of a text file. However, there are often cases where it either isn’t a text file, or maybe the text file is too large. Sending the file as an attachment would be ideal, but you can’t just pipe any type of data to the mail command.

Instead, I just learned how to use ‘mutt’ to send a file as an attachment. The command is something like this:

echo "see attached file" | mutt -s "Here is the file" -a /home/brandon/some-file.bin some@body.com

Where ‘See attached file’ is the body of the message. The -s argument of ‘Here is the file’ is the subject. /home/brandon/some-file.bin is the file I want to attach. And some@body.com is who I want to send the message to.

This would work well for quick files that you want to transfer to your PC – perhaps instead of using a file-transfer program like FTP. It can also be used in scripts. I had one customer, for example, who used this to email themselves a mysqldump of their database each night as a sortof backup.

One thought on “Sending Yourself a File as an Attachment From the Command Line”

  1. For those on UNIX systems without mutt, you simply use uuencode. (It’s also available if you install the sharutils rpm on Red Hat or CentOS systems). Just type in:

    uuencode /home/brandon/some-file.bin | mail -s “Here is the file” some@body.com

Leave a Reply

Your email address will not be published. Required fields are marked *