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
Subject: Email Testing from Linux

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

Thanks, Ramesh

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"

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"

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"

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"

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 25 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”


mail -s “Email testing from Linux” < 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 .. 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

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

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,


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


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 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 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.

# vim:ts=2:sw=2:expandtab:cindent


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


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

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{$_}) {
} else {
$u{$_}=undef; $_;
} @msgIds;

undef %u;

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

15 David Beecher June 10, 2012 at 12:17 pm

This method of attaching files hasn’t worked for a long time. Very few (outlook at one time) clients today will recognize and decode embedded UUENCODED data and even fewer will recognize base64 encoding in an UUENCODED message.

This link has a better answer AND several variations depending on your environment.

Good luck,

16 ram August 2, 2012 at 1:40 pm


thanks its very helpful…..

Thanks for sharing


17 Kuldeepak September 23, 2012 at 6:30 am

I am new to linux, i used mail command to send file from linux machine to my email account using linux command.

But it is taking too much time to deliver the email to my email account.

Could any one help me in order to shorten this delay time in between the sending and receiving of emails from the linux machine.

I need this help on urgent basis.

I will be very thankful to you guyz.

18 Ashok October 12, 2012 at 10:37 am

Your post is so Helpful…
Thanks so much for sharing this information.

19 Stefan October 24, 2012 at 10:01 am

Does anybody read the man pages?
If you have a newer version of sendmail you just can use -a

echo “Body text” |mail -s “Subject” -a /var/tmp/file.txt

20 Rahul Gupta February 16, 2013 at 12:13 pm

HI friends

I ran below command to send mail.but i did not get mail in my inbox.

Please help me out to resolve this problem.

“echo “file_to_send” | nail -s “Your File” -a mail_id.txt

Yesterday i ran same command then it worked for me. i just rebooted the fedora machine, after that the problem occurred. please help me whether i have to chamge some setting or any thing else..

Thanks in advance..

21 hdaz February 17, 2013 at 4:37 am


is sendmail or postfix running ?
service sendmail status
service postfix status

yes or no if no then start them
service sendmail start
next is it starting after a reboot
chkconfig –list |egrep “sendmail | postfix”

yes no if no then start which ever one you have installed
chkconfig –level 35 sendmail on

still got a problem have a look in the mailq

or look in /var/log/maillog to see any errors

22 Rahul Gupta February 17, 2013 at 10:31 am

Hi hdaz,

Thanks for your help.

I saw my “/var/spool/mail/root” logs it sowing below error

” The IP you’re using to send email is not authorized…”

Please help me out to resolve this problem

Thanks in advance

23 hdaz February 17, 2013 at 10:33 am
24 Rahul Gupta February 17, 2013 at 10:44 am

Now i am able to send mail..

But all my mails are going in spam..

Now what should i do to get them in inbox instead of spam,

25 jorge March 7, 2014 at 5:41 pm

Excelent , the solution to send a mail including an attached file, was great. Really Thank you.


Leave a Comment

Previous post:

Next post: