How To Send an Email With Attachment and Body from Linux

by Ramesh Natarajan on December 30, 2009

Question: How do I send an email with attachment from Linux command-line (or shell script)? Also, can I send both attachment and body text together in an email from Linux command-line?

Answer: You can send both attachment and body text (or just the attachment with a subject line) from Linux command line as explained below.

1. Send an Email with Subject and Body

Typically you would send an email from the Linux command line with a subject line and body text as shown below. Please note that you should type a . (period) in a separate line to indicate the body of the text is over.

$ mail ramesh.thegeekstuff@gmail.com
Subject: Email Testing from Linux
Dear,

It is very easy to send an email from Linux command line. 

Thanks, Ramesh
.
Cc: ramesh@thegeekstuff.com

If you want to read the body text from a file (for example, body-message.txt), send the email as shown below.

$ cat body-message.txt | mail -s "Email testing from Linux" ramesh@thegeekstuff.com

2. Send an Email with Attachment

To send an attachment from the email, use uuencode command. On RedHat (and related distributions), uuencode is part of the sharutils package. So, install the sharutils as shown below.

# rpm -ivh sharutils-4.6.1-2.i386.rpm
Preparing...        ############################## [100%]
   1:sharutils      ############################## [100%]

Once you’ve confirmed that you have uuencode, send the email with an attachment as shown below.

$ uuencode input-attachment.txt output-attachment.txt | \
mail -s "Email With Attachment" ramesh.thegeekstuff@gmail.com

In this example,

  • input-attachment.txt is the file that you like to attach to the email.
  • If you like the file to be attached with a different name, specify it as 2nd parameter to the uuencode. In this example, the intput-attachment.txt file content will be attached as output-attachment.txt

Note: uuencode can also be used to send base64 attachments as shown below.

$ uuencode -m input-attachment.txt output-attachment.txt | \
mail -s "Email With Base64 Attachment" ramesh.thegeekstuff@gmail.com

3. Send an Email with Attachment and Body

You can send an email with both attachment and body message as shown below.

$ ( cat body-message.txt; uuencode input-attachment.txt output-attachment.txt ) \
| mail -s "Email With Body Text and Attachment" ramesh.thegeekstuff@gmail.com

Share

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 14 comments… read them below or add one }

1 kyanh December 30, 2009 at 3:14 am

I found that sendEmail is more comfortable and easy

sendEmail -a

sendEmails has binary package on Ubuntu, for e.g

2 hdaz December 30, 2009 at 3:15 am

Erm most people would probably have to setup mail first ??

send-mail: Cannot open mail:25
Can’t send mail: sendmail process failed with error code 1

3 Chris F.A. Johnson December 30, 2009 at 5:18 am

“$ cat body-message.txt | mail -s “Email testing from Linux” ramesh@thegeekstuff.com

UUOC

mail -s “Email testing from Linux” ramesh@thegeekstuff.com < body-message.txt

4 Rk December 30, 2009 at 5:21 am

When I send mail using above command I am getting two mails
One mail with body and another mail with body and attachment.
Do we need to configure any settings for mail ??

5 Mahesh December 30, 2009 at 7:21 am

I have found mutt to be a lot useful for sending attachment.

6 Ed Deloye December 30, 2009 at 7:46 am

You can do the same thing with mutt:

mutt -s ‘subject’ -i file1 -a file2 -c user1@domain -b user2@domain user3@doman </dev/null

-s Specify the subject of the message.
-i Specify a file to include into the body of a message.
-a Attach a file to your message using MIME.
-c Specify a carbon-copy (CC) recipient
-b Specify a blind-carbon-copy (BCC) recipient

The redirect from /dev/null prevents mutt from going into interactive mode

7 John December 30, 2009 at 10:46 am

I have for a long time used a little app called “email” from http://www.cleancode.org/projects/email .. It compiles easily and has served me really well. It will use your favourite console editor to write the body text, attach files very easily, cat text from a file for body text, use a sig line if wanted and I believe it even has a simple addressbook and some other tricks, like multiple attachments very simply.

I almost only use it from scripts though, backing up configs and stuff to a file dump email address

8 Frank Daley December 30, 2009 at 3:25 pm

I think that mutt is a lot simpler. For example, it can be installed on Fedora

yum -y install mutt

To use it is simply

mutt -s “Subject text” emailaddress < filetosend.odt

9 felipealvarez January 3, 2010 at 10:47 pm

mail -s subject email@domain < filetosend
base64 filetosend | mail -s subject email@domain

The above two do not 'attach' the file to message. Instead, they are in the BODY of the message. Why is this?

Using opensuse 11.2 (postfix)

10 Silviu Marin-Caea January 5, 2010 at 2:58 am

On opensuse 11.2 do this:
echo text body | mail -s “Subject line” -a attachment.txt person@internet.com

11 hideaki January 15, 2010 at 3:57 am

cat-uuencode-and-a-pie is pretty lame. There ought to be some tool that does a proper MIME packing of the mail.

12 Carl January 25, 2010 at 11:29 am

How about

cat mail.txt | mutt -a attachment -s subject me@to.com

13 Martin October 7, 2010 at 9:07 am

Thanks a bunch. That last part about emailing both an attachment and a body file was exactly what I needed. We’re moving from AIX to Linux and I needed to find a replacement for metasend. We have a lot of programs that use metasend. Instead of changing 60 programs, I now have a 60 line script to parse out the info from the metasend command and pass it to uuencode and mail. Fantastic.

Thanks Again,

Martin

14 Network-Engineer February 24, 2011 at 6:26 am

Hi

First congrats on your daughter’s birth. Be happy and be safe. I have a question related to postfix. I want to monitor one single email user of my domain. e.g. i want to monitor ne@xyz.com how many emails it has sent out, how many sent successfully, how many rejected and how many deffered. now i have a script with me that works fine for me and give me output like how many total ne@xyz..com sent out, how many rejected and how many deffered. i’m goin to share this script with you here. what i want to add to this script is that it should give me the email addresses that are rejected. you know what i mean. it current tells me 750 emails are sent by this user. 200 rejected 100 deffered etc i want to know what email addresses are rejected. so if 200 are rejected it should give me 200 emails address aswell so i can see if there is spell mistake or something. here is the script. please have a look at this. or if you know any other software to monitor this kindly let me know. i want to moniter my postfix user who is sending out newsletters. i want reports how many are sent how many rejected how many deffered etc. which rejected emails.

here is script that i am using but it’s not givin me emails rejected. just the counts.

#!/usr/bin/perl
# vim:ts=2:sw=2:expandtab:cindent

### MAKE MODIFICATIONS FOR DATE AND SENDING EMAIL ADDRESS HERE ###

#$date=”Oct 14[5-9]“;
$date=”Dec 18″;
#$sender=”(.*)\@domain.com”; # Matches any sender from domain.com
#$sender=”username\@domain.com”; # Matches abc@domain.com
$sender=”user\@domain.com”; # Matches user@domain.com

### END OF USER MODIFICATIONS ###

$sent = 0;
$bounced = 0;
$deferred = 0;

$logFile=”/var/log/maillog”;
open LOGFILE, “<", $logFile;
@rawLogs = ;
close LOGFILE;

$date =~ s/^([A-Z][a-z]{2})( )([0-9])$/$1 $3/;
printf(“%s %s\n”,$sender, $date);

if ( grep(/(^[A-Z][a-z]{2} [0-9])$/, $date) ) {
$field = 6;
} else {
$field = 5;
}

@logs = grep(/$date .* postfix\/qmgr\[[0-9]+\]: .* from=/i, @rawLogs);
foreach (@logs) {
@line = split(/ /);
push(@msgIds, $line[$field]);
}

@sent = grep (/status=sent/, @rawLogs);
@bounced = grep (/status=bounced/, @rawLogs);

@ids = grep {defined} map {
if (exists $u{$_}) {
undef;
} else {
$u{$_}=undef; $_;
}
} @msgIds;

undef %u;

$i=0;
$cnt = $#ids + 1;
foreach $id(@ids) {
$final=0;
if (grep(/$id/, @sent)) {
$sent++;
$final=1;
} elsif (grep(/$id/, @bounced)) {
$bounced++;
$final=1;
}
if ($final != 1) {
$deferred++;
}
$i++;
}
print “Messages Delivered: $sent\n”;
print “Messages Bounced: $bounced\n”;
print “Messages Deferred: $deferred\n”;
print “Total Sent: “. ( $sent + $bounced + $deferred ) .”\n”;

Leave a Comment

Previous post:

Next post: