If you are looking to send email with attachment via shell script or at shell prompt/command line (read as bash prompt), use mutt command.
Mutt is a small but very powerful text based program for reading electronic mail under UNIX /Linux operating systems, including support for color terminals, MIME, and a threaded sorting mode.
Please note that mutt is a pure MUA and cannot send e-mail without proper email server . You need a working Mail Transfer Agent (MTA) such as sendmail or postfix. I am assuming that you have configured email server.
Install mutt
If mutt is not installed, use apt-get or yum or up2date command as follows (login as a root user):
(A) Debian Linux / Ubuntu Linux user use following command to install mutt:
# apt-get install mutt
B) Fedora / CentOS or Red Hat Linux (RHEL) user can use following command to install mutt:
# yum install mutt
OR
# up2date mutt
C) FreeBSD user use following command to install mutt via pkg_add command:
# pkg_add -v -r mutt
How do I send email attachments from a command prompt?
1) Use mutt command as follows to send an email with attachment:
$ mutt -s "Test mail" -a /tmp/file.tar.gz vivek@nixcraft.co.in < /tmp/mailmessage.txt
Where,
- vivek@nixcraft.co.in - is the recipient
- /tmp/mailmessage.txt - is the main body of the e-mail (read message from the file "mailmessage.txt")
- /tmp/file.tar.gz - is an attachment (with option -a)
- "Test mail" - is a subject line (option -s)
See also:
- Sample bash script to send an email
- mybackup script to backup selected files/directories and email them as .tar.gz file to your email account
- Metamail ftp://ftp.research.telcordia.com/pub/nsb/
- Email this to a friend
- Printable version
- Rss Feed
- Last Updated: Oct/3/2007

{ 2 trackbacks }
{ 36 comments… read them below or add one }
I didnt recieve any mail in my yahoo inbox when i tried to send the message from the command line using mutt
mutt -s “testmail” anugula_kiran@yahoo.co.in > /tmp/k
please i am eager to know about it
May be yahoo is blocking your email! It should work. If you are on PPP then it is high possiblity!
–
Vivek Gite
I can’t be sure, but it looks like you’re using the wrong directional switch. In your command use, you have “>”. I’m presuming you’re attempting to send the message in the file /tmp/k which means you should probably be using the “
In the command:
mutt -s “Test mail” -a /tmp/file.tar.gz vivek@nixcraft.co.in
Hey i was hunting 4 this info and found it via yahoo. This is easy than those crazy scripts… thx for good info in this blog.
thanks this blog is bookmarked now.
thanks, you made my day (or better, tomorow ) with this hint…
nice post worth to bookmark :)
thanks for this post!
How do I use MUTT to send multiple attachments in a single email?
Multiple attachments use -a for each ie:
mutt -s “Test Mail” -a attach1 -a attach2 -a attach3 vivek@nixcraft.co.in
Nice post thanks guys!
I just want learn more things in linux,
How do you send an e-mail to multiple recipients with mutt?
is the reading in of the text file required or can I type a one-line body from the command line?
This is a reply to message 12, regarding using “-a” to send multiple attachments in one email.
I use the same mutt command line in my script. My challenge is I can’t hard code the files with “-a”. I need to search for all logs, find out what they are and mutt them.
I need something like:
mutt -s “log files” -a *.log sue@homtmail.com < summary
How do I code “-a *.log”?
Reply to #17: Regarding how to use “-a” for an indeterminate number of attachments; here’s one way:
y=”"
for x in $(ls -1 *.log) ; do
y=”$y -a $x”
done
mutt -x “subject” $y myaddr@company.com < somefile
How do you send an e-mail to multiple recipients with mutt?
In reply to 19: Putting all addresses into quotes should work.
mutt -s “My message” -a attchmnt “address1 address2″
its working well in system which serve as proxy but not working from my desk. How to get it work in a proxy ed network ?
Thanks, I was searching for an hour for this! To do the equivalent with sendmail is crazy!
man mpack
Thank you!
worked like a charm for me, thanx guys :]
hi all, i am getting this error
mutt -s “Test Shell Mail” malath.alakeel@gmail.com < email
Error sending message, child exited 127 (Exec error.).
Could not send the message.
How to use mapck to send multiple files? i am able to send only one file with the command. Please help
See our FAQ:
Email multiple file attachment from Solairs / AIX / HP-UX UNIX/ Linux command line
HTH
Thanks
i am unable to send mail from shell .. getting an error /etc/postfic/main.cf no such file or directory ..pls help
I am unable to send an .htlm file as the mail body. When I use the command below, it mails the actual code.
mutt -s “Test Mail” user@uh.edu < mail_attachment.html
I can send it as an attachment and it comes through fine with this command:
mutt -s “Test Mail” -a mail_attachment.html user@uh.edu < mail_body.txt
the mail_body.txt does not contain anything. I just get the attachemnt. Sun system can e-mail it to Outlook as an .htlm mail.
Hmmm why all this reported succes, I seem to get it all wrong.
mutt -s “test Mail” -a /home/jesper/Y.tar gvzfs@yahoo.dk < t.txt
so where am I going wrong?
I am using a laptop with wireless connection. How do I specify “from”?
Is it possible to send specific text in the body of the mail, rather than sending the body from the attached file?
mutt -s “Test Mail” -a /home/test.txt user@abc.com < “TEXT BODY”
is it possible to set “from” option, becouse by default i getting from: root
i using command like:—
mutt -s ‘Test mail’ -a file.xls usernamel@domain.com < file
i want from option should become useranem who is giving mail to someone
you can use uuencode too. Here is an example I used for many years. How to extract,using bash/unix/linux shell scripting, the oracle data like excel report and …-> http://tinyurl.com/dbcy98
Sam,
if u want to send certian text in body instead of using a body file then go for this:
#> echo -e “This is the body message of the email” | mutt -a server.key raj@alpha.xion.com -s “This is the email subject”
Actually I am using proxy server to access internet
i did
export http_proxy=”http://10.100.100.172:3128/”
and
export http_proxy=http://10.100.100.172:3128/
to configure proxy
then
mutt -s “Test mail” -a /root/amritesh/rs.txt amriteshsmsc@gmail.com < /root/amritesh/gw.txt
to send mail but it wont work plz guide me what to do…?