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" firstname.lastname@example.org
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 email@example.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 firstname.lastname@example.org 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.