Walker News

Linux Dummy Guide: How To Send Email With Content And Attachment?

For those who are using graphical-based email client should have no problem on how to send an email with attachment.

This dummy guide is for those who want to send an email with content and attachment at the same time in Linux command prompt, not at the Linux Desktop.

But, why you need to do so when there are plenty of email clients appear with intuitive GUI (graphical user interface)?

Well, system administrators or programmers who write Linux shell script to send email with attachments of log file, report, music, picture, etc, will find it easier if working with command-line email client (e.g. sendmail or mail program).

How to send email with attachment in Linux command prompt?

Basically, you need two programs for this topic, i.e. an email client (e.g. mail that bundled with most Linux distributions) and a binary file encoder (e.g. uuencode which is bundled with sharutils package).

For example, to write an email message that’s attached with Logo.JPG:
(echo "This is the content of email and it's attached with a jpeg file called Logo.JPG"; uuencode /tmp/1.JPG Logo.JPG) | mail -s "This is email subject" walker@walkernews.net

The parenthesis groups the both echo and uunencode commands to run as subshell processes, where the output of subshell appears as single input to the mail client.

The uuencode encodes the binary picture file (i.e. /tmp/1.JPG) to ASCII format (i.e. Logo.JPG), which is then sent as email attachment.

If you just want to email attachments without writing the content/message, then the command will be as this:

Update: This </dev/null shouldn’t be here!
uuencode /tmp/1.JPG Logo.JPG | mail -s "This is email subject" walker@walkernews.net </dev/null

So, the correct code is as this:
uuencode /tmp/1.JPG Logo.JPG | mail -s "This is email subject" walker@walkernews.net

So, it’s your choice, whether to email attachment with content/message or not. But, those two options are available.

Custom Search

  1. GNU Date Generates Consistent UNIX Epoch Time Between Time Zones – Walker News 30-08-08@10:21

    […] date is easier for Linux administrators who write shell script that involves Unix Epoch time or Julian time (Julian time is the common term we used to refer this […]

  2. Get Linux Ls Command To Show File Timestamp In Detail Of Seconds 11-03-09@15:43

    […] There is a shell script scheduled to run as cronjob in RHEL 4 server. During trigger time, the Linux shell script executes a C++ program and wait 30 seconds for the program output redirects to a regular file. […]

2017  •  Privacy Policy