Howto: Send The Content Of a Text File Using mail Command In Unix / Linux

by on April 17, 2012 · 13 comments· LAST UPDATED April 17, 2012

in

I'd like to send an email with the content of a text file using mail command in Unix / Linux operating system. How do I do it?

mail command (also mailx command) is an intelligent mail processing system under Unix and Linux. You need to use the following syntax to send an email using mail command:

 
mail -s 'Subject-Here' you@cyberciti.biz < input.file
mail -s 'Uptime Report' you@cyberciti.biz < /tmp/output.txt
 

Where,

  • -s 'Subject' : Specify subject on command line.
  • you@cyberciti.biz: To email user.
  • /tmp/output.txt : Send the content of /tmp/output.txt file using mail command.

Send an email as attachment from Unix command line

Either can use the mutt command or uuencode command as described below:

 
### Attach /tmp/filelist.tar.gz and read the content of a text using /tmp/body.txt ###
mutt -s "Backup status" -a /tmp/filelist.tar.gz you@cyberciti.biz < /tmp/body.txt
 

OR

 
### Attach /tmp/list.tar.gz and and send it  ###
uuencode /tmp/list.tar.gz /tmp/list.tar.gz | mailx -s "Reports" user@eu.corp.cyberciti.biz
 
### Email photo.png along with a text message read from body.txt ##
(cat body.txt; uuencode photo.png photo.png) | mail -s "Subject" user@cyberciti.biz
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 13 comments… read them below or add one }

1 pubudu April 24, 2012 at 11:28 am

Thanks nixCraft…you guys are doing a awesome job..keep it up

Reply

2 faizul February 10, 2014 at 9:07 am

you don’t know any thing in unix like CASe

Reply

3 Yogesh May 4, 2012 at 3:39 am

Use mail with from and to email IDs
cat /home/user/input.file | mail -s ‘Subject-Here’ -v notify-to@email-id.com — -f from.email-id.com
I wonder if we can specify from email ID while using mutt too?

Reply

4 Imran May 14, 2012 at 7:24 am

Hi, thank you for this great tutorial. Just wondering if there’s a pre-requirement for the above to work? I have tried on my Debian Linux box but my emails have not received the sent message. My Linux box is connected to the Internet. It’s just a web server.

Reply

5 nixCraft May 14, 2012 at 7:44 am

You need local smtp (such as Postfix, sendmail) installed. On debian you need to start exim email service.

Reply

6 Minh Phu March 18, 2013 at 1:34 am

i need to send mail with multi attach file by commands line, can you help me

Reply

7 Suresh Podishetti August 8, 2013 at 10:45 am

Try this, It worked:
(echo “$mail_body_text” ; uuencode “attachment1.out” “attachment1.out” )| mailx -s “$mail_Subject” $EmailUser

Reply

8 harsha September 10, 2013 at 10:18 am

I see issue with uuencode not showing the file attachment when opened in webmail

begin 640 t1.txt
&:&5L;&\*
`
end

above content is coming in body of the mail in place of attachment. Please let me know how to fix this.

Reply

9 Ali September 22, 2013 at 10:52 pm

My attachment is blank. I’m having a similar problem as harsha above.

Any clues? I can’t seem to send this file without it getting messed up. Is there another way to send text from a file to another email?

Reply

10 abc November 18, 2013 at 8:51 am

Is it possible to send the output by doing cat on 2 files. Say like

cat {fil1} {fil2} | mailx

Reply

11 Yogesh November 22, 2013 at 10:01 pm

Hi,

Great one! I have a question for you.

mailx is not working properly when sending the contents of the text file. Received mail as the content in one line even though the text file contains line by line.

Please help me out on this issue.

Thanks,
Yogesh

Reply

12 PJeet November 26, 2013 at 12:14 am

I am trying below command
mail -s ‘unix test’ xyz@gmail.com < body.txt
where "unix test" is my subject, xyz@gmail.com is my gmail id and body.txt is my text file from which i want to copy content into my email body.

I am getting below error message:
postdrop: warning: unable to look up public/pickup: No such file or directory

Is something missing in my command or is there any pre-requisit i need to follow to use this command.
I am new to UNIX

Thanks

Reply

13 sandeep December 17, 2013 at 11:52 am

Thanks for the information. When we use mail for display content of a file as body of an email – we’re getting an email in not understandable language. Can you help me out here?

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: