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.

One Comment

  • moto x3m 5 Reply

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.