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

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

{ 26 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”;

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,
David

16 ram August 2, 2012 at 1:40 pm

hi,

thanks its very helpful…..

Thanks for sharing

-Ram

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 test@test.com

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 mail@gmail.com

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

@Rahul

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

Hey,
Thanks.
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.

Success

26 Nitesh June 30, 2014 at 5:58 am

Works great . I am recieving the mail in spam folder .Is that related to sendmail configuration ? If so whats configuration I should apply .

Thanks for the post .It helped :)

Leave a Comment

Previous post:

Next post: