FreeBSD send mail with attachments from command line / shell prompt

I need to send a few reports everyday as a mail attachment. I’m using FreeBSD 6.2 server. How do I send mail from a command line or a shell script?

You can easily send email attachment using mutt mail client. It works from command line or a shell prompt. However mutt is not installed by default. You need to install mutt command. mutt command also works under UNIX and Linux like operating systems.


Install mutt under FreeBSD

Mutt is a small but very powerful text based program for reading electronic mail under unix operating systems, including support color terminals, MIME, and a threaded sorting mode. Use the pkg_add command to install binary software package distributions, enter:
# pkg_add -v -r mutt
Alternatively, you can use FreeBSD ports collections:
# cd /usr/ports/mail/mutt
# make install clean

FreeBsd command line mail attach with mutt

Send reports.tar.gz file to as attachment, enter:
$ mutt -s "Reports" -a reports.tar.gz < /dev/null
$ mutt -s "Subject" -a reports.tar.gz < /tmp/mail-message.txt

  • -a file : Attach a file to your message using MIME.
  • -s subject : Specify the subject of the message.

For multiple file attachments use -a for each file as follows:
$ mutt -s "Subject" -a file1.tar.gz -a file2.tar.gz < /dev/null

For more information read mutt command man page:
$ man mutt

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

5 comments… add one
  • qp Apr 17, 2009 @ 3:39

    Can a mutt-1.5.19 be installed on a FreeBSD system (where there’s a mutt- by a user who doesn’t have root acccess? Using pkg_add or any other way? If so, how? Any tips appreciated. (I tried & got a lot of “permission denied.”)

    • 🐧 nixCraft Apr 17, 2009 @ 4:48

      Noop. You need to be root to install software.

  • qp50cklw Apr 17, 2009 @ 6:43

    > Noop. You need to be root to install software.

    Well, surely there must be some way for users on a FreeBSD system to install programs into their own home directory. If not, then I don’t see much “free” in FreeBSD.

    • 🐧 nixCraft Apr 17, 2009 @ 8:37

      You don’t understand security concept, do you? This is a security feature. Otherwise, it will just like MS-Windows, you can install anything including virus without your permission. I suggest you get a good book which explains basic UNIX concepts.

    • Ben Aug 6, 2014 @ 2:22

      You can install packages or source to your own home directory; you typically need to be root because, when installing a package/port system-wide (the usual behaviour), you need to place the pieces of this application in different system directories (/usr/bin, /etc, /usr/lib etc.), and this requires root/superuser permissions.

      In Windows, users are typically setup as ‘Administrators’, making it easier for them to install new software they want; the software is kept all in one place, in it’s own directory in Program Files, but this is still a system directory and needs Admin (root) privileges. Windows users are encouraged to have an Admin account for installing new software, and a limited User account just for their own space, however in practise nobody really does this because it’s too difficult, and Windows systems as home desktops are really just there for playing games and doing unimportant crap.

      In either situation – yes, it would actually be (arguably) better to let users only install applications to their local home directories, for their local use, running them with their own limited credentials; this way, even if you installed a piece of malware, it should only affect you, or at least, be unable to do system-wide damage (or, if you’ve chrooted/jailed your home dir somehow, it shouldn’t be able to get to the outer filesystem either).

      But BSD machines, and most Unix systems, are used as serious midrange business servers; only new software that’s been vetted by someone, or is in the standard ports tree to begin with, is installed – by someone with root privileges, and usually as part of a painstakingly complex upgrade process. This is not the same as a personal home Windows desktop system.

      TL;DR Needing root privileges to install a package is only to install it system-wide (which it usually is), because the process will need to add files to directories like /usr/bin, /etc, /usr/lib and so on.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.