Mailing Attachments via Commandline

I think everybody of you just had this situation. You need to send a log or configuration file from a server to a colleague. If there are some hard security guidelines implemented, it’s quite awkward to copy the files via scp/sftp to your desktop and then mailing the attachments to the receiver.

If you’re on a system with a working mail configuration, you can send the mail directly with one or more attachments. There are several mail clients on the most systems, which can be used for that. I prefer mutt.

echo "This is the content of my mail message" | mutt -s "subject" -a "/var/log/messages" -- receiver@example.com

-s  is the option for the mail subject, -a  is the option for the path to the attachment, receiver@example.com  is the mail receiver.

If you need to send multiple attachments, you just can use the -a  option more than once like in the example below. And it’s also possible to use a file as mail content at the end of the command with < /tmp/content.txt

mutt -s "subject" -a "/var/log/messages" -a "/etc/resolv.conf" -- receiver@example.com < /tmp/content.txt

It depends on your mutt version, if the steps above are working, this is tested on a debian 7 server. On RHEL, the following page could help.

8 Comments

  • moto x3m 5

    It’s quite awkward to copy the files via scp/sftp to your desktop and then mailing the attachments to the receiver…

  • Mesa masonry

    This is very informative. I did not even know that you can do this using mutt. Am not a computer Geek so forgive my ignorance. 🙂 Thanks again for sharing.

  • cheapessaywriter

    What are the content restrictions of the mail message? I’m doing a short contrast paragraph about the commandline errors in the attachments too. A receiver example would be great for my template (I will upload it to my Moodle task for students).

  • cabinets

    More posts!!