
formail Command in Linux
The formail command in Linux processes the email messages. It is a part of the procmail suite; a versatile email processor.
The formail command formats the email messages into mailbox format. The mailbox format is the standard format used to store a collection of email messages into a file. In the file, mail messages are stored one after another. Each email message in the file starts with the From keyword (a standard sign to denote the beginning of a new message). After the From line, there will be an email message with header information.
The formail command is also used to extract and format the header information.
Table of Contents
Here is a comprehensive guide to the options available with the formail command −
- Prerequisites to Use formail Command
- Syntax of formail Command
- Options of formail Command
- Examples of formail Command in Linux
Prerequisites to Use formail Command
The formail command may not be available by default in Linux. To check whether the formail command is installed or not, execute the following command −
formail -v

If the above command does not produce output specifying the version of the command, then install it using the instructions given below −
The formail command is a part of the procmail suite. Procmail is a versatile email processor. To install the formail command, the procmail package is needed to be installed.
To install procmail on Ubuntu, Kali Linux, Debian, and other Debian-based distributions, use the following command −
sudo apt install procmail
To install it on Arch Linux, use the command given below −
sudo pacman -S procmail
To get in on CentOS, use −
sudo yum install procmail
On Fedora, use the below command −
sudo dnf install procmail
Syntax of formail Command
The syntax of the Linux formail command is as follows −
formail [options] [folder | headerfield]
The [options] field is used to specify the options to modify the commandâs behavior. The [folder | headerfield ] is used to specify the folder to append the processed message and the headerfield is used to specify the extraction of specific header information.
Options of formail Command
The options of formail command are listed below −
Options | Description |
---|---|
-v | To display the command version |
-b | To avoid bogus mailbox header |
-p prefix | To change the default quotation prefix (>) |
-Y | To assume Berkeley format mailbox (ignores the content length) |
-c | To concatenate the continued header-fields |
-z | To zap whitespaces and empty header fields |
-f | To force formail to pass along any non-mailbox format |
-r | To generate an autoreply (preserves fields with -i option) |
-k | To keep the body on auto-reply (prevents escaping with -b) |
-t | To trust the sender's return address for reply (instead of enveloping sender) |
-l folder | To generate the procmail-compatible log summary |
-D maxlen idcache | To detect duplicates with an idcache of length maxlen |
-s | To split the input into separate mail messages and pipes each to the specified command (concatenates the messages if no command is provided) |
-d | To accept the digest format for splitting |
-e | To set the empty lines optional |
-B | To accept the BABYL rmail format |
-q | To silence the errors |
-m minfields | To specify the minimum number of consecutive header fields (default 2) |
-x headerfield | To extract the content of the header field |
-X headerfield | To extract the content of the header field and its name |
-a headerfield | To add a field if not present |
-A headerfield | To add headerfield in any case |
-i headerfield | To rename the existing field and insert |
-I headerfield | To delete the existing field |
-u headerfield | To make the first occurrence headerfield unique by removing the subsequent fields |
-U headerfield | To make the last occurrence headerfield unique by removing the preceding fields |
-R oldfield newfield | To rename all the instances of the specified field |
+skip | To skip the specified number of messages |
-total | To specify the total number of messages to process |
Examples of formail Command in Linux
This section demonstrates the usage of the formail command in Linux with examples −
Note − The results of the command mentioned in the following examples will only be displayed in the standard output. To save the modification use the redirection operator (> or >> for apending) and move the stdout to a new file.
Splitting the Mail Messages
To split the multiple mail messages in the mailbox file and pipe the output to the grep command, use the -s option with formail. For example, to filter mail messages based on the subject line and display those containing Subject:, use formail command in the following way −
formail -s grep -i "Subject:" < mail

Extracting a Specific Header Field
To extract a specific header field, use the -x option. For example, to extract the content of the subject field, use the -x option −
formail -x "Subject:" < mail
To extract the date, use −
formail -x "Date:" < mail

To include the field name, use the -X option −
formail -X "Subject:" < mail formail -X "Date:" < mail

To keep the body of the message, use the -k option −
formail -k -X "Subject:" < mail

Skipping Messages
To skip a specific number of messages, use the +skip option. For example, to skip the first message, use +1 −
formail +1 -s < mail

The above command will display all messages except the first one. Similarly, to skip five messages, specify +5.
Generating Auto Reply Header
To generate the auto-reply header, use the -r option.
formail -r < mail

To preserve any other field, use the -i option. For example, to preserve the Date field, use the formail command in the following way −
formail -r -i "Date" < mail

Appending a Header Field
To append a header field to the existing message, use the -a option. For example, to append the Reply-To field to the existing mail messages, use the following command −
formail -a "Reply-To: abc@example.com" < mail

Modifying the Header Field
To insert or update an existing header field, use the -i option. For example, to change the Subject field content, use the command given below −
formail -i "Subject: New Subject" < mail

The old subject will be changed to the Old-Subject field and a new Subject field will be added.
Renaming the Header Field
To rename a specific header field, use the -R option. For example, to rename the Subject field with the New-Subject, use the formail command in the following way −
formail -R Subject New-Subject < mail

Conclusion
The formail command in Linux is used to process the mail messages. It is used to modify the message in a mailbox file and extract or manipulate the header fields.
The formail command is a part of the procmail suite which is a widely used mail processor tool.