Topics: System Admin

Sending emails with attachments

If you wish to send a file from a Linux or UNIX system, you can cat the file and pipe it into an email. For example, if you wish to send the contents of the /etc/hosts file in an email:

# cat /etc/hosts | mailx -s "subject goes here"
This may not always be what you want, though. Sometimes, the file may be too large, or you may rather attach the file to an email.

To attach a file to an email, run it like this using -a option:
# echo "your message goes here" | \
mailx -s "your subject goes here" -a /etc/hosts
Most email applications, like Microsoft Outlook, will put you text ("your message goes here") into the body of the email, and will attach /etc/hosts as a hosts.txt file for easy reading.

To attach larger files to an email (or for mail applications that handle zip files better as attachments), zip the file first before attaching it to an email message:
# zip /tmp/ /etc/hosts
# echo "your message goes here" | \
mailx -s "your subject goes here" -a /tmp/

If you found this useful, here's more on the same topic(s) in our blog:

UNIX Health Check delivers software to scan Linux and AIX systems for potential issues. Run our software on your system, and receive a report in just a few minutes. UNIX Health Check is an automated check list. It will report on perfomance, capacity, stability and security issues. It will alert on configurations that can be improved per best practices, or items that should be improved per audit guidelines. A report will be generated in the format you wish, and the report includes the issues discovered and information on how to solve the issues as well.

Interested in learning more?