Sending email or mail with attachment from command or shell prompt

by nixcraft · 53 comments

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:

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 2 trackbacks }

How to send anonymous email from shell prompt using mixmaster : Tech Tips
01.08.07 at 8:42 am
Send mail bash script
12.29.08 at 12:37 pm

{ 51 comments… read them below or add one }

1 Anonymous 11.02.04 at 10:22 am

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

2 nixcraft 11.18.04 at 5:36 pm

May be yahoo is blocking your email! It should work. If you are on PPP then it is high possiblity!


Vivek Gite

3 Anonymous 04.10.05 at 11:23 pm

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 “

4 nixcraft 04.11.05 at 2:18 pm

In the command:

mutt -s “Test mail” -a /tmp/file.tar.gz vivek@nixcraft.co.in

5 Anonymous 07.23.05 at 2:23 pm

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.

6 tony jr. 07.24.05 at 6:03 pm

thanks this blog is bookmarked now.

7 Anonymous 08.19.05 at 3:51 am

thanks, you made my day (or better, tomorow ) with this hint…

8 Anonymous 11.19.05 at 12:52 am

nice post worth to bookmark :)

9 ebasi 12.19.06 at 8:25 am

thanks for this post!

10 Hagel 04.28.07 at 12:00 am

How do I use MUTT to send multiple attachments in a single email?

11 MickZA 05.23.07 at 8:57 am

Multiple attachments use -a for each ie:

mutt -s “Test Mail” -a attach1 -a attach2 -a attach3 vivek@nixcraft.co.in

12 AJS 06.06.07 at 7:50 pm

Nice post thanks guys!

13 kumaryadav 07.31.07 at 3:57 pm

I just want learn more things in linux,

14 Mary Killpatrick 08.07.07 at 5:40 am

How do you send an e-mail to multiple recipients with mutt?

15 aaron 12.11.07 at 2:35 am

is the reading in of the text file required or can I type a one-line body from the command line?

16 Sue 12.12.07 at 7:27 am

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”?

17 Barry 12.17.07 at 9:52 pm

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

18 bhush 04.03.08 at 6:06 am

How do you send an e-mail to multiple recipients with mutt?

19 gpsforum 05.01.08 at 4:49 pm

In reply to 19: Putting all addresses into quotes should work.

mutt -s “My message” -a attchmnt “address1 address2″

20 macs 06.05.08 at 6:13 am

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 ?

21 Rob 06.27.08 at 4:53 am

Thanks, I was searching for an hour for this! To do the equivalent with sendmail is crazy!

22 zyga 08.27.08 at 1:54 pm

man mpack

23 Nokao 09.16.08 at 5:45 am

Thank you!

24 Carlos Garcia 10.27.08 at 6:39 pm

worked like a charm for me, thanx guys :]

25 Malath 10.31.08 at 10:12 am

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.

26 Kaniska 11.01.08 at 7:06 pm

How to use mapck to send multiple files? i am able to send only one file with the command. Please help

27 vivek 11.01.08 at 7:22 pm
28 Shamjith 12.13.08 at 3:15 pm

Thanks

29 ace 12.21.08 at 7:35 am

i am unable to send mail from shell .. getting an error /etc/postfic/main.cf no such file or directory ..pls help

30 EMG 01.12.09 at 7:26 pm

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.

31 jesper hansen 01.19.09 at 6:52 pm

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”?

32 Sam 02.12.09 at 5:29 pm

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”

33 tarun singhal 03.04.09 at 10:40 am

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

34 jiltin 04.09.09 at 11:14 am

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

35 Prudhvi raj 05.15.09 at 11:47 am

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”

36 Amritesh 06.18.09 at 12:26 pm

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…?

37 Tom 07.04.09 at 10:31 pm

in debian lenny:
mutt -s “subject” -a file1 file2 file3 — email@gmail.com < email-text.file

between attachments and recipients need to putt double dash and files for attachment with out anything

with out double dash wroute:
"Can't stat email@gmail.com: No such file or directory
email@gmail.com: unable to attach file."

because it's thinking that email@gmail.com it's one more attachemt file )))

Hope it's help you )))

38 ram 07.29.09 at 5:00 am

Hi,

when i am sending a mail with pdf attachment thru mailx utility on MS Outlook than it wooks fine but whenver i am sending the same to rediff/gamil/yahoo than instead of pdf attachment it is sending junk characters,please advice.
uuencode xx_file.pdf xx_file.pdf| mailx –s “Subject of mail” “mail_id”
i am using the above command.

please suggets to do the same.

ram

39 Murali 09.16.09 at 6:07 am

Hi

This e-mail (mutt) utility looks very useful, I would like to use it on AIX, can any one let me know how I could install this utility on AIX and use it.

Thank you

Regards
Murali

40 Sanil 10.06.09 at 5:14 am

if you want to send the contents of a text file from the shell prompt do the following.

# mail -s “subject of the mail” me@me.com < textfile.txt

41 lien 10.06.09 at 3:36 pm

What an excellent tool. I have been looking for something like this for ages!

42 Fun Nepali Games 10.13.09 at 9:50 pm

How to specify “from name” when sending email thru mutt? I am using php cli to send email and I could only set the “from email”, and not the “from name”. Here is my php statement with mutt command:

shell_exec(“export EMAIL=\”from@email.com\”; echo ‘email text’ | mutt -s ‘email subject’ -a ‘/attachment/file.pdf’ to@email.com“);

When I ran this php program, the recepient receives email like this:
From: “My Real Name”

But I want “My Company Name” to show in the From: line. Where to specify this when running mutt ?

43 Maroun 10.26.09 at 2:17 pm

Hi,

I need a favor, I am using mutt command to send an email and everything is working fine, but as you know when email arrived to the proper person, the email is coming from internal name of the instance of the server (e.g From: XX@servername.companyname.extension), can we put an constanct alias for ‘from email account’, I mean from: payables@companyname.net for example. I think this is an internal linux parameters structure?

Thanks
Maroun

44 Ken 11.12.09 at 4:16 am

Hi Fun Nepali Games/ Maroun,

Use this command format to set the from name and the from email to whatever you want:
export EMAIL=’from name<from email address>’; echo ‘email body text‘ | mutt -a ‘attachment file‘ -s ‘subjectrecipient address

Hope I helped! Greetings

45 Fun Nepali Games 11.12.09 at 11:48 am

Hi Ken, thank you for your help. I had come up with this syntax to fix my issue.

shell_exec(”export EMAIL=\”from@email.com\”; export REPLYTO=\”from@email.com\”; echo ‘email text’ | mutt -n -e ’set envelope_from_address=\”from@email.com\”‘ -e ’set realname=\”My Company Name\”‘ -s ‘email subject’ -a ‘/attachment/file.pdf’ to@email.com“);

46 vrc 11.15.09 at 3:02 am

Hi…
I am working on ubuntu. i did install mutt and when i typed mutt -s “hello” mymail@gmail.com in my terminal a nano editor came up and i manually had to proceed the process of sending the mail…finally it attached a file from my /tmp folder in which the contents were buffered and this file was sent…
i just want to automate the entire process without any human intervention…
Can someone please help me out in automating the entire process…
thanks

47 Andres Kwan 11.27.09 at 12:35 am

Thanks guys, with your help I did solve the problem
“Can’t stat email@gmail.com: No such file or directory
email@gmail.com: unable to attach file.”

48 Paul 01.17.10 at 1:26 am

How can you mutt only the newest file from a particular directory in a bash script?

49 lsi 01.30.10 at 3:42 am

hey nice thread, i got this working with from field set as I desire, from a shell script as follows:

${TARGETEMAIL}="target@protected"
export EMAIL="My Name "
export REPLYTO="email@protected"
echo "body text" | mutt -e 'set envelope_from' -a /filename.gif -s "subject" ${TARGETEMAIL}

50 lsi 01.30.10 at 3:45 am

(note, board munged code a bit, try again:

export EMAIL=”My Name “

51 lsi 01.30.10 at 2:01 pm

attempt 3:

export EMAIL=”My Name <email@protected>”

eg. it is “My Name (less-than-sign)email@protected(greater-than-sign)”

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: