Mail Command in Linux - Explained with examples

Mail is the easiest way to send messages to your friends, partners. There are many ways to allow users to send emails, and you may not know it, Linux can also help you send emails using the mail command on the terminal. You can write the subject, content with the email address and send it by mail command.

This command is built-in on Linux distributions. 

Here at LinuxAPT, we shall look into how to use the mail command in Linux.

How to install the mail package ?

You can run the apt command to install the mailutils package:

$ sudo apt install mailutils

Next, install Postfix with the below command:

$ sudo apt install postfix

How to use the mail command ?

The syntax of the mail command:

$ mail -s "subject" [recipient's email]
[Type the message]
[Press Ctrl + D]
CC [You can skip by pressing Enter]

To understand clearly, we will try to send a mail to address "ubuntu@linuxapt":

$ mail -s "test" ubuntu@linuxapt

Alternatively, you can send mail by a single command:

$ mail -s "test" ubuntu@linuxapt <<< 'Test'

If you want to attach the text file:

$ mail -s "test" ubuntu@linuxapt <<< 'Hello, We want to share a file for you' /home/ubuntu/test/test1.txt

You can check the mail by command:

$ mail

To read the email, enter the email's sequence number and press Enter.

To delete the email:

& d [email's sequence number]

[Need help in fixing Linux system issues ? We can help you. ]

This article covers the Complete guide for setting up a mail server using Postfix, Dovecot & SquirrelMail. 

Linux mail command Options:

  • -a, --append=HEADER: VALUE: Append given header to the message being sent.
  • -e, --exist: Return true if mail exists.
  • -E, --exec=COMMAND: Execute COMMAND.
  • -f, --file[=URL]: Operate on given mailbox URL. The default mailbox is ~/mbox.
  • -F, --byname: Save messages according to sender.
  • -H, --headers: Write a header summary and exit.
  • -i, --ignore: Ignore interrupts.
  • -n, --norc: Do not read the system mailrc file.
  • -N, --nosum: Do not display initial header summary.
  • -p, --print: Print all mail to standard output.
  • -q, --quit: Cause interrupts to terminate program.
  • -r, --read: Same as -p
  • -s, --subject=SUBJ: Send a message with a subject of SUBJ.
  • -t, --to: Precede message by a list of addresses.
  • -u, --user=USER: Operate on USER's mailbox.
  • --license: Print license and exit.
  • --external-locker=PATH: Set full path of the external locker program
  • --lock-expire-timeout=SECONDS: Number of seconds after which the lock expires.
  • --lock-flags=FLAGS: Default locker flags (E=external, R=retry, T=time, P=pid)
  • --lock-retry-count=NUMBER: Set the maximum number of times to retry acquiring the lockfile.
  • --lock-retry-timeout=SECONDS: Set timeout for acquiring the lockfile.
  • -m, --mail-spool=URL: Use specified URL as a mailspool directory.
  • --mailbox-type=PROTO: Default mailbox type to use.
  • --tls[=BOOL]: Enable TLS support.
  • -?, --help: Display a help message and exit.
  • --usage: Display a short usage message and exit.
  • -V, --version: Display program version and exit.

Related Posts