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
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. How to Send SMS Using Email to Major US Cellphone Carriers
  2. Transfer the Power of Vim Editor to Thunderbird for Composing Email
  3. Compress, Encrypt, Split and Transport Big Files Safely
  4. Increase Bugzilla Attachment Size Using MySQL max_allowed_packet
  5. 15 Essential Gmail Labs Feature
  

Vim 101 Hacks Book

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

Leave a Comment

Previous post:

Next post: