Email files directly from the Linux console

It’s not a great habit to get into, but sometimes it can be very quick and convenient to email a file to yourself directly from a bash SSH console.

Note: Because this uses email, this method isn’t secure. Think carefully about the contents of what you’re sending before you email files directly from your system. Even /etc/ configuration files can give away too much about your system.

Personally, I like to use the mailx utility in Debian to email from the console; it’s an alternative to sendmail, and it’s less fussy about some of the parameter format.

If you haven’t got mailx, you can install it with:

apt-get install mailx

If you haven’t already got an MTA (mail transfer agent) installed, this will prompt you to install one. The default in Debian is exim, which is fine. I prefer to use postfix, so, I run an apt-get install postfix first. That’s a matter of preference.

You’re also going to need the uuencode tool. This is part of the sharutils package. Install this with:

apt-get install sharutils

Now you’re ready. Let’s assume you’ve got a gzipped log file that you want to email to yourself. The command is simply:

uuencode /var/log/apache.log.gz apache.log.gz | mailx -s "Apache Log File (compressed)" me@domain.com

…and that’s it. Note that the uuencode utility takes two parameters:

Usage: uuencode [INFILE] REMOTEFILE

The first argument (/var/log/apache.log.gz above) is the local filename. This could include a path to the file. The second argument is the attachment ‘name’. This is the filename that will show up in the email when the recipient opens it.

This entry was posted in Debian, General, Linux, Systems Administration. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What is 7 + 12 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple question (so we know that you are a human)