Sending Email With Attachments From Unix / Linux Command [ Shell Prompt ]

last updated in Categories Debian Linux, FreeBSD, Howto, Linux, Mail server, RedHat/Fedora Linux, Shell scripting, Suse Linux, Tips, Ubuntu Linux, UNIX

If you need to send an email with a text file (or binary file) as attachment using shell script or command prompt in Unix or Linux; try mutt – a terminal-based e-mail client for Unix-like systems.

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 a working email server in your office.

How do I install mutt email client?

If mutt is not installed, use the apt-get or yum or up2date commands as follows (you must login as a root user). Debian / Ubuntu Linux user type the following command to install mutt client:
# apt-get install mutt
Fedora / CentOS or Red Hat Enterprise Linux (RHEL) user type following command to install mutt:
# yum install mutt
OR (RHEL version <= 4 ) # up2date mutt
FreeBSD user type the following command to install mutt via pkg_add command:
# pkg_add -v -r mutt

How do I send email attachments from a command prompt?

Use mutt command as follows to send an email with attachment:
$ mutt -s "Test mail" -a /tmp/file.tar.gz < /tmp/mailmessage.txt

  • – is the recipient.
  • /tmp/mailmessage.txt – is the main body of the e-mail (read message from the file “mailmessage.txt”).
  • -a /tmp/file.tar.gz – is an attachment.
  • -s “Test mail” – is a subject line.

If you don’t have mutt command installed, try uuencode command to send emails with attachments.

See also:

Page last updated at 12:33 AM, April 18, 2012.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


102 comment

    1. Check your bulk mail. I am using it and my emails when are sent out of the computer goes in the “from: www-data” so yahoo take the email as spam and if your settings in is to delete all spam then is deleting it. Try using another email a POP account. It will work like charm.

  1. 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 “

  2. 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.

    1. You can define a variable with recipient mentioned as coma separated values.,
      mutt -s “Test Mail” -a attach1 -a attach2 -a attach3 — $Emails

    2. Using Vim:

      Write all of the emails separated by commas to a text file, compose an email in mutt (specify yourself as the recipient), write the subject, etc. When you’re finished, write and quit, then hit E to edit the headers. Scroll down to CC: and then type :r . Your addresses should appear on the next line, so scroll to that line and hit backspace. Ta-da! Magic. 🙂

      1. Hi experts
        I would like to send e-mail using command-line-mutt; but I’d like it to read the recipients from the “To:” header in the file (in mailx, there is such an option: “-t”, like this: mailx -t < filename, and then you don't need to specify recipients on the command line). How can it be done in mutt?

  3. 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 < summary

    How do I code “-a *.log”?

  4. Reply to #17: Regarding how to use “-a” for an indeterminate number of attachments; here’s one way:

    for x in $(ls -1 *.log) ; do
    y=”$y -a $x”

    mutt -x “subject” $y < somefile

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

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

  6. 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 ?

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

  8. Hmmm why all this reported succes, I seem to get it all wrong.

    mutt -s “test Mail” -a /home/jesper/Y.tar < t.txt

    so where am I going wrong?

    I am using a laptop with wireless connection. How do I specify “from”?

  9. 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 < file

    i want from option should become useranem who is giving mail to someone

  10. 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 -s “This is the email subject”

  11. Actually I am using proxy server to access internet
    i did
    export http_proxy=””
    export http_proxy=
    to configure proxy
    mutt -s “Test mail” -a /root/amritesh/rs.txt < /root/amritesh/gw.txt
    to send mail but it wont work plz guide me what to do…?

  12. in debian lenny:
    mutt -s “subject” -a file1 file2 file3 — < 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 No such file or directory unable to attach file."

    because it's thinking that it's one more attachemt file )))

    Hope it's help you )))

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

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

      same for squeeze. thanks mate

  13. 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.


  14. 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


  15. 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=””; echo ’email text’ | mutt -s ’email subject’ -a ‘/attachment/file.pdf’“);

    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 ?

  16. 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: for example. I think this is an internal linux parameters structure?


  17. 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

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

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

  19. Hi…
    I am working on ubuntu. i did install mutt and when i typed mutt -s “hello” 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…

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

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

  21. attempt 3:

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

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

  22. Just spent a couple of hours working this out.

    The mutt way to set the from address and name in mutt is ;

    mutt -e "unmy_hdr from; my_hdr From:"
    -e "set realname="Ronald McDonald" "
    -s "this is the subject line ... "
    -a attachmentfile.txt -a anotherFile.doc
    -- < bodyOfEmail.txt

    Note that the -e “command” lets you do any set, unsets, or mutt configurations


    For the record, the other approaches to customising From addresses (and by extention other attributes) are as follows.

    I have listed all the methods that mutt make available, though two of them don’t work.

    Method 3 works, #2 half works, method #1 and 4 don’t work.

    #from method #1
    set use_envelope_from=yes
    set envelope_from_address=""
    # NOTE : use_from defaults to yes, so that mutt forces the from address
    # *** FROM ADDRESS

    # from method #2 :
    # use_from=no works. Now the MTA, e.g. ssmtp sets the from address
    set use_from=no
    # however, the set_from DOESNT WORK.
    set from="" # doesn't work

    # from method #3
    # this is the approach to use. See my command line example
    # you must use the my_hdr to set a new from address. See 4
    unmy_hdr from
    my_hdr From:""

    # from method #4 : let the MTA set the from address
    # doesn't work. Mutt fails to remove the from header line.
    unmy_hdr from

  23. Hi Friends…

    I have a requiremnt where i need to send a mail with an attachement (pdf file) for the same i have written this code and it is getting executed without any error but still i am not receiving any mail. Please adive:

    Following code is for you reference:

    DATETIME=`date “+%Y%m%d%H%M%S”`
    DATETIME1=`date “+%Y%m%d”`

    REQUESTID=echo $1 | awk ‘{print $2}’ – | sed ‘s;FCP_REQID=;;’
    USERPASS=echo $1 | awk ‘{print $3}’ – | sed ‘s;FCP_LOGIN=;;’ | sed ‘s;”;;g’
    P_FILE_PATH=echo $1 | awk ‘{print $9}’ – | sed ‘s;”;;g’
    P_FILE_NAME=echo $1 | awk ‘{print $10}’ – | sed ‘s;”;;g’
    P_USER_EMAIL_ID=echo $1 | awk ‘{print $11}’ – | sed ‘s;”;;g’
    FILE_SUB=echo $1 | awk ‘{print $12}’ – | sed ‘s;”;;g’

    # “/oracle/apps/inst/apps/TEST_erpapts2/logs/appl/conc/out XXADIB_PO_STANDARD_467106_1.PDF temp1@adibdev.local testmail”

    echo “——————————————–”
    echo “DatTime=” $DATETIME
    echo “DATETIME1=” $DATETIME1
    echo “——————————————–”
    echo “param=” $1
    echo “P_FILE_PATH=” $P_FILE_PATH
    echo “P_FILE_NAME=” $P_FILE_NAME
    echo “——————————————–”
    echo ” ”
    echo “——————————————–”
    echo $FILE_SUB
    echo “——————————————–”
    echo ” ”
    echo “Just before sending mail..”
    echo ” ”
    echo “——————————————–”
    if test $EXIT_STATUS = 0 ; then
    echo “Success”
    (echo “Please find the enclosed all vacancies and the details of vacacy statues. This is system generated email. Please do not reply.”;uuencode $P_FILE_PATH $FILE_SUB) | mailx -s “$FILE_SUB – on $DATETIME” $P_USER_EMAIL_ID
    echo “Fail”
    echo “——————————————–”
    echo ” ”
    echo “Un-successful run of the Error Log File”
    echo ” ”
    echo “——————————————–”
    echo “——————————————————-”
    exit 0

  24. (1) have you considered looking at the examples in this thread ?
    (2) this thread is about mutt, and you are using mailx
    (3) my example above shows how to send an email of any configuration, with any attachments, from the command line, using mutt

    In general, do this :
    add the
    line /bin/bash -x
    to the start of your scripts. NOTE THE -x
    Then watch the output, it will show you all the assignments. You probably have made a mistake in one of your awk assignments.
    Also don’t try to be smart, doing stuff like running multiple commands in a subshell (in brackets) and then piping the result to mailx. Instead do clear, seperate steps, and write temporary files, in testing AND PRODUCTION so you can investigate what is going on. I mean you haven’t even been able to narrow the problem down to the step that is going wrong.

    The answer to why this isn’t working is that, probably, your don’t have a MTA running. mutt and mailx and manyothers just manage the generation of the email, you need a MTA (mail transfer agent) like ssmtp (simple, easy, great) or exim4 (not simple, fully featured)

    —-in summary: —-
    use /bin/bash -x
    test each intermediate step
    use mutt, and copy one of the examples above.
    find where the error is BEFORE you post on a forum.

    1. It’s an idiom: the “$” is used by some people to represent the prompt character In a terminal, shorthand for I-am-typing-in-a-terminal-here. I think that they use “$” to represent the root user, and # to represent the prompt of a normal user.

      Just ignore it….
      …. and the rest works … right ? … cool.

      Depending on permissions, doesn’t have to be a root user,

  25. Thank you for commenting however i’m begining to think that my computer doesn’t support mutt. Is this specifically for linux users because there is no mutt install link that works with my computer.

  26. Thank you Vivek. This came handy when I realized that I didn’t have the sharutils package (for uuencode) installed and so could not use mailx/uuencode combination. Anyway, the “-a” option of mutt is better than mailx/uuencode.

  27. Hey, thanks, I needed to send a quick mail and your tip worked like a charm — except I needed to send two attachments and mutt required a — before the destination. Thanks!

  28. Hi,
    Recently I have installed Linux Ubuntu on my Laptop and was trying some command line to send emails. I do not know what package needs to be installed but same command is working fine at my work place which has Unix AIX machine.

    For example this syntax is working at my work place but not working on my Ubuntu command prompt.
    (echo “test”;) | mail -r -s “Send Email ”

    Can somebody please advice what do I need to do if I want to use these above options.

    Thanks in Advance

  29. I am not able to send more than ~7Mb (+ headers info which become in a 9.9 Mb file size). How can I increase the attachment size sent via mutt?. I’ve looking at the conf file and I didn’t see any line where the ~10 Mb file size attachments were set by default. I really need to increase this thing at least 25 Mb to send database backups to my email account (which supports 50 Mb attachments).

    1. Yes, I forgot to mention it’s a file compressed with LZMA algorithm which is the highest compression level around. My server OS is Linux/RedHat. I just need to know if mutt is capable to send bigger than ~10 MB attachment, the current backup file that I am getting is about ~7 Mb but with headers it just become into a 10 Mb. As I said I research about it, I read the config file and nothing yet.
      Appreciate your help.

      1. Actually I was looking at the wrong place. The attachment size limit is not controlled by mutt else the smpt server itself, in my case postfix.

        If somebody is having this same issue, just go the postfix config file at /etc/postfix/ (on RedHat based kernels) and look for this directive: message_size_limit (or add it to the end if it’s not in there) and just increase the value to: message_size_limit = 20480000 (20 MB for example).

        Hope that helps.

  30. Is it possible to specify a different name for an attachment than the actual file name? For instance, we have many programs the call the mutt command and send files with extensions that our recipient’s PCs don’t recognize. It would be nice to be able to rename the attachment in mutt without having to actually modify the source file.



  31. How do I use a Variable Subject. For example:

    mySUBJECT="No Errors in Log File"
    mutt -s "$mySUBJECT" < Nightly-Backup-Log.txt
    exit 0

    This always hangs and thus fails. But typing-in the same string where $mySUBJECT is works fine. Could someone tell me what I'm doing wrong?


  32. Answered my own question!

    Need to use Single Quotes rather than Double Quotes around $mySUBJECT. For example this works:

    mySUBJECT=”No Errors in Log File”
    mutt -s '$mySUBJECT' < Nightly-Backup-Log.txt
    exit 0

    Hope it helps someone!

  33. The mutt program is super cool this line is part of a script I did that make a backup of all my SQL Server Info and send it to my email. Because I have a High protection for Spam this email always go to junk. I also got a POP3 account in my work and there I have no problem also I receive it in my iPhone 3GS. This is the hell of a tool. THanks for sharing this with us.

    #Email will be send to my account.
    mutt -s "MySql Server Database backup files" < /home/julio/dbbak/message.txt -a /home/julio/dbbak/
    #Email will be send to my pop3 company account.
    mutt -s "MySql Server Database backup files" < /home/julio/dbbak/message.txt -a /home/julio/dbbak/
  34. hulo,i hav just instaled mutt but dont even no how to start it,how to configure it 2 read from mi email account,ani help will b apreciated

    1. I had a similar problem: it would run from the command line while I was logged-in but running it as a cron job always failed. After some testing I decided the problem had to do with not being the same user under cron as I was at the command line. I’m not sure about the exact steps to fix it, but I’d look in that direction.

  35. How can I get MUTT to send as an HTML email? I can attatch the html document but I want to have it viewed as soon as you look at it.. instead of seing all the text HTML tags.

  36. Hi,

    I am using script for diskspcae in linux server and output is like alert that all team member should get mail as alert, when i install mutt i.e. up2date mutt …..command shows like temporary failure in name resolution so what should i do pls kindly help for same. ?????

  37. Re: “Method 3 works, #2 half works, method #1 and 4 don’t work.”
    you can’t rewrite headers when using gmail, as google violates protocol by rewriting these headers back

  38. Hello to all,
    I’ve an issue sending email with Mutt.
    Command is inside a bash script and, if the email address contains an undescore _ it fail, the email sent.
    If a use Mutt in the command line, it works.
    I’ve no idea how to fix this problem.
    Thanks in advance

  39. Hello,
    I have faced one issue related with mutt (mailing) command on RHEL-6 server.
    Issue: mutt command not able to send email to any email id from linux 6.x machine to msoutlook.
    the postfix and sendmail was in installed on the server and mutt command was unable to send email. no one service will help to send the email.
    remove the postfix package on linux server and then it will work fine.
    pls try this command to send attachment with mutt command
    echo | mutt -s Test_mail -a --

  40. I searched the web for a way to recieve mail from my Hotmail acount using Mut but no luck.
    I have no problem receiving mail from my gmail acount but cant find settings that will let me download hotmail email. Anyone had any luck with Hotmail?

  41. Is it possible to attach a file with different name? Files with sh and xml extensions are filtered. Thanks for the help!

  42. I am running a script using the mutt command (see below) When the email is send form the LINUX server, the .xls file looks good except it is missing the numbers in the columns. How would I send the file so it shows all the data? If I do an ftp from the server, grab the file and put it on my PC I can open the excel file and the data is there, if I use Mutt the email shows the excel file but the data is not populated

    Capacity Totals (TB) Platform Base (TB)
    Master Server Calculated Charged
    alvmnrnba01 0.00 0.00
    Total 0.00 0.00

    cat $tmp_file1 | /usr/bin/mutt -e "set from=no_reply" -e "set realname=NetBackup" -s "Report: NetBackup Capacity " -a $output_rpt -- $recipients   # the "--" may be a single "-", depending upon *nix version.
  43. Hi,

    i have tried below code to send one file as attachment but now while trying to send two files i am not able to send it.could you please help with this.Here one file sent but not able to send two files at one time

    SUBJECT=”Internal calloff file for VCT”

    echo “Send the E-mail message…”
    #/usr/bin/mailx -s “${SUBJECT}” ${TO} <<-EOF
    /usr/bin/mailx -s "${SUBJECT}" -r ${FROM} ${TO} <<-EOF

    See attachment for internal calloff from PLUS.

    Attachment: $P_FROM_ORA/$ResultFile
    ~< ! uuencode $P_FROM_ORA/$ResultFile $ResultFile

Leave a Comment