nixCraft Poll

Topics

Sending email or mail with attachment from command or shell prompt

Posted by Vivek Gite [Last updated: October 3, 2007]

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,

See also:

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Anonymous Says:

    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 Says:

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


    Vivek Gite

  3. Anonymous Says:

    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 Says:

    In the command:

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

  5. Anonymous Says:

    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. Says:

    thanks this blog is bookmarked now.

  7. Anonymous Says:

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

  8. Anonymous Says:

    nice post worth to bookmark :)

  9. ebasi Says:

    thanks for this post!

  10. How to send anonymous email from shell prompt using mixmaster : Tech Tips Says:

    [...] You can send email with attachment via shell script or at shell prompt/command line using mutt or mail command. This will not hide your IP address. [...]

  11. Hagel Says:

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

  12. MickZA Says:

    Multiple attachments use -a for each ie:

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

  13. AJS Says:

    Nice post thanks guys!

  14. kumaryadav Says:

    I just want learn more things in linux,

  15. Mary Killpatrick Says:

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

  16. aaron Says:

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

  17. Sue Says:

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

  18. Barry Says:

    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

  19. bhush Says:

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

  20. gpsforum Says:

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

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

  21. macs Says:

    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 ?

  22. Rob Says:

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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.