Cyberithub

How to Install mail command on Ubuntu 20.04 LTS (Focal Fossa)

Advertisements

In this article, we will see how to install mail command on Ubuntu 20.04 LTS (Focal Fossa). mail is a free and open source command line utility which allows us to send and manage emails from terminal. Thus also making our job of sending emails from bash or shell scripts convenient. mail utility can take message body from a file and hence can save lot of time and effort in writing all the mail body on a command line terminal.

It is relatively easy to use and can be installed on any of the famous linux based systems. It is available through different packages in different linux flavors. On Ubuntu/Debian based systems, it is available through mailutils package. So to install mail command, you need to install mailutils package from default Ubuntu repo by following below mentioned steps.

 

How to Install mail command on Ubuntu 20.04 LTS (Focal Fossa)

How to Install mail command on Ubuntu 20.04 LTS (Focal Fossa)

Also Read: How to Install cmatrix on Ubuntu 20.04 LTS (Focal Fossa)

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get utility available in your Server.

 

Step 2: Update Your Server

In the first step, you need to check for all the latest available updates and install them from default Ubuntu repo by using sudo apt update && sudo apt upgrade command as shown below.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
[sudo] password for cyberithub:
Hit:1 http://ppa.launchpad.net/flatpak/stable/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:3 http://ppa.launchpad.net/gencfsm/ppa/ubuntu focal InRelease
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:6 https://dl.winehq.org/wine-builds/ubuntu focal InRelease
Hit:7 http://ppa.launchpad.net/juju/stable/ubuntu focal InRelease
Hit:8 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:9 https://download.sublimetext.com apt/stable/ InRelease
Hit:10 http://ppa.launchpad.net/libreoffice/ppa/ubuntu focal InRelease
Hit:11 http://ppa.launchpad.net/mojo-maintainers/ppa/ubuntu focal InRelease
Get:12 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:13 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [59.9 kB]
Get:14 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [96.6 kB]
Get:15 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [940 B]
Get:16 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [274 kB]
Get:17 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [410 kB]
Get:18 http://in.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [944 B]
Get:19 http://in.archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,960 B]
Get:20 http://in.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [30.5 kB]
Fetched 1,217 kB in 10s (121 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
.......................................................................

 

Step 3: Install mail command

In the next step, you can install mailutils package from default ubuntu repo by using sudo apt install mailutils command as shown below. This will download and install the package along with all its dependencies.

cyberithub@ubuntu:~$ sudo apt install mailutils
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libgsasl7 libkyotocabinet16v5 libmailutils6 libntlm0 mailutils-common postfix
Suggested packages:
mailutils-mh mailutils-doc procmail postfix-mysql postfix-pgsql postfix-ldap postfix-pcre postfix-lmdb postfix-sqlite sasl2-bin | dovecot-common
resolvconf postfix-cdb postfix-doc
The following NEW packages will be installed:
libgsasl7 libkyotocabinet16v5 libmailutils6 libntlm0 mailutils mailutils-common postfix
0 upgraded, 7 newly installed, 0 to remove and 2 not upgraded.
Need to get 2,494 kB of archives.
After this operation, 10.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
.............................................................

During installation, you will be asked to choose the type of mail server configuration in Postfix configuration. Here we are choosing Internet Site for now but you can choose appropriate mail server configuration type based on your needs.

How to Install mail command on Ubuntu 20.04 LTS (Focal Fossa) 2

Below are all different mail server configuration type available to configure:-

  • No Configuration: Choose this type when you don't want current configuration to be changed
  • Internet Site: This type will allow you to send and receive email directly using SMTP
  • Internet with smarthost: This type will allow you to receive mail directly using SMTP or by running a utility such as fetchmail and the outgoing mail will be sent using a smarthost.
  • Satellite system: Choose this option when you want all the mails to be sent to another host called a smarthost for delivery.
  • Local only: This option is used for all local users only who does not have to connect to a network for mail delivery.

Next, you need to provide the domain name as shown below. For the moment, we have given ubuntu as the system mail name but you can provide the appropriate domain name and then Press Tab and Enter to select OK

How to Install mail command on Ubuntu 20.04 LTS (Focal Fossa) 3

If everything goes well, the installation will get completed in a while as you can see below.

......................................................
setting myhostname: ubuntu
setting alias maps
setting alias database
mailname is not a fully qualified domain name. Not changing /etc/mailname.
setting destinations: $myhostname, ubuntu, localhost.localdomain, , localhost
setting relayhost:
setting mynetworks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
setting mailbox_size_limit: 0
setting recipient_delimiter: +
setting inet_interfaces: all
setting inet_protocols: all
/etc/aliases does not exist, creating it.
WARNING: /etc/aliases exists, but does not have a root alias.

Postfix (main.cf) is now set up with a default configuration. If you need to
make changes, edit /etc/postfix/main.cf (and others) as needed. To view
Postfix configuration values, see postconf(1).

After modifying main.cf, be sure to run 'systemctl reload postfix'.

Running newaliases
Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /lib/systemd/system/postfix.service.
Setting up libgsasl7:amd64 (1.8.1-1) ...
Setting up libmailutils6:amd64 (1:3.7-2.1) ...
Setting up mailutils (1:3.7-2.1) ...
update-alternatives: using /usr/bin/frm.mailutils to provide /usr/bin/frm (frm) in auto mode
update-alternatives: using /usr/bin/from.mailutils to provide /usr/bin/from (from) in auto mode
update-alternatives: using /usr/bin/messages.mailutils to provide /usr/bin/messages (messages) in auto mode
update-alternatives: using /usr/bin/movemail.mailutils to provide /usr/bin/movemail (movemail) in auto mode
update-alternatives: using /usr/bin/readmsg.mailutils to provide /usr/bin/readmsg (readmsg) in auto mode
update-alternatives: using /usr/bin/dotlock.mailutils to provide /usr/bin/dotlock (dotlock) in auto mode
update-alternatives: using /usr/bin/mail.mailutils to provide /usr/bin/mailx (mailx) in auto mode
Processing triggers for rsyslog (8.2001.0-1ubuntu1.3) ...
Processing triggers for ufw (0.36-6ubuntu1) ...
Processing triggers for systemd (245.4-4ubuntu3.22) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

 

Step 4: Verify Installation

After successful installation of mailutils package, you can verify the installed files path by using dpkg -L mailutils command as shown below.

cyberithub@ubuntu:~$ dpkg -L mailutils
/.
/usr
/usr/bin
/usr/bin/dotlock.mailutils
/usr/bin/frm.mailutils
/usr/bin/from.mailutils
/usr/bin/mail.mailutils
/usr/bin/messages.mailutils
/usr/bin/mimeview
/usr/bin/movemail.mailutils
/usr/bin/readmsg.mailutils
/usr/bin/sieve
/usr/sbin
/usr/sbin/maidag
/usr/share
/usr/share/doc
/usr/share/doc/mailutils
/usr/share/doc/mailutils/README.Debian
/usr/share/doc/mailutils/TODO.Debian
/usr/share/doc/mailutils/copyright
/usr/share/doc/mailutils/examples
/usr/share/doc/mailutils/examples/mail.rc
/usr/share/lintian
........................................................

 

Step 5: Check Version

You can check the current installed version of mail utility by running mail --version command as shown below.

cyberithub@ubuntu:~$ mail --version
mail (GNU Mailutils) 3.7
Copyright (C) 2007-2019 Free Software Foundation, inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

 

Step 6: Send test email

Now that mail utility is installed in the system, it is time to send a test email to user cyberithub and check if it is working fine. We can send an email with subject "Test Email" and body containing "This is test email" to user cyberithub using echo "This is test email" | mail -s "Test Email" cyberithub@ubuntu command as shown below.

cyberithub@ubuntu:~$ echo "This is test email" | mail -s "Test Email" cyberithub@ubuntu

After sending email, you can verify its delivery by checking /var/spool/mail/cyberithub file using tail -f /var/spool/mail/cyberithub command as shown below.

cyberithub@ubuntu:~$ tail -f /var/spool/mail/cyberithub
id BC64E1204C3; Sat, 29 Jul 2023 21:48:34 +0530 (IST)
Subject: Test Email
To: <cyberithub@ubuntu>
X-Mailer: mail (GNU Mailutils 3.7)
Message-Id: <20230729161834.BC64E1204C3@ubuntu>
Date: Sat, 29 Jul 2023 21:48:34 +0530 (IST)
From: cyberithub <cyberithub@ubuntu>

This is test email

 

Step 7: Check all Available Options

You can check all the options available with mail utility by using mail --help command as shown below.

cyberithub@ubuntu:~$ mail --help
Usage: mail [OPTION...] [address...]
or: mail [OPTION...] [OPTION...] [file]
or: mail [OPTION...] --file [OPTION...] [file]
or: mail [OPTION...] --file=file [OPTION...]
GNU mail -- process mail messages.
If -f or --file is given, mail operates on the mailbox named by the first
argument, or the user's mbox, if no argument given.

-A, --attach=FILE          attach FILE
-a, --append=HEADER: VALUE append given header to the message being sent
--[no-]alternative         force multipart/alternative content type
--attach-fd=FD             attach from file descriptor FD
--content-filename=NAME
                           set the Content-Disposition filename parameter for
                           the next --attach option
--content-name=NAME        set the Content-Type name parameter for the next
--attach option
--content-type=TYPE        set content type for subsequent --attach options
-E, --exec=COMMAND         execute COMMAND
-e, --exist                return true if mail exists
...........................................

 

Step 8: Uninstall mail command

Although I won't recommend but if you choose to uninstall mail utility from your system then you need to use sudo apt remove mailutils command as shown below. But be careful in running below command on production or on any other critical systems as removing this package might affect other active running applications which has dependency on this package to run.

cyberithub@ubuntu:~$ sudo apt remove mailutils
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libgsasl7 libkyotocabinet16v5 libmailutils6 libntlm0 mailutils-common
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
mailutils
0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded.
After this operation, 630 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 250144 files and directories currently installed.)
Removing mailutils (1:3.7-2.1) ...
update-alternatives: using /usr/bin/bsd-from to provide /usr/bin/from (from) in auto mode
Processing triggers for man-db (2.9.1-1) ...

Leave a Comment