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
Get free Unix tutorials, tips and tricks straight to your email in-box.
If you enjoyed this article, you might also like..


My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 12 comments… read them below or add one }
I found that sendEmail is more comfortable and easy
sendEmail -a
sendEmails has binary package on Ubuntu, for e.g
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
“$ 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
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 ??
I have found mutt to be a lot useful for sending attachment.
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
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
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
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)
On opensuse 11.2 do this:
echo text body | mail -s “Subject line” -a attachment.txt person@internet.com
cat-uuencode-and-a-pie is pretty lame. There ought to be some tool that does a proper MIME packing of the mail.
How about
cat mail.txt | mutt -a attachment -s subject me@to.com