Sunday, April 22, 2012

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

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
 

No comments:

Post a Comment