1. Overview
The mail command is a utility that enables us to send and receive emails via the command line. However, setting the sender’s name while using the mail command in Linux is important for several reasons.
Basically, it helps us to identify the source and purpose of the email. It also enhances the credibility and professionalism of the email. In addition, the recipient’s email service prevents the email from being marked as spam or phishing.
In this tutorial, we explain how to set the sender name while using the mail command in Linux. We’ll cover the following topics:
- installing the mail command
- using the mail command
- setting the sender’s name while using the mail command.
We tested the code in this tutorial on Bash shell version 5.1.16.
2. Installing the mail Command
Before using the mail command in Linux, we need to install a package that provides the utility. Notably, the mail command is part of the mailutils package in Debian systems.
Let’s look at how we can install the mail command:
$ sudo apt install mailutils -y
In the code snippet above, we use the sudo command to install the mailutils package:
- sudo command allows us to run another command as a root or superuser with administrative privilege
- apt will enable us to interact with the Advanced Packaging Tool (APT) systems and handle dependencies and sources of the package
- install option will allow us to install any other dependencies the package needs
- mailutils provides us with the mail command and other utilities for sending and receiving emails via the command line
- -y option automatically answers yes to all prompts and questions during the installation process
After we run the code, a prompt will show up for us to configure postfix packages.
We should:
- press the Tab key to select OK and confirm with the Enter key
- choose Internet Site
- use the Tab key to select OK and confirm with the Enter key again
Finally, we complete the configuration by entering our system mail name and wait for the installation to complete.
3. Using the mail Command
Now, we can use the mail command to send out emails. The mail command works with the email we’re signed in on in the Thunderbird Mail app. Let’s look at the basic syntax for sending an email with the mail command:
mail [options] -s [subject] [recipient_address]
In the code snippet above, [options] represents the mail command options, the -s flag specifies the email subject, and [recipient_address] is the email address/username of the person we are emailing.
However, after entering the command, we’d see a prompt for any carbon copies (Cc) recipients. If we don’t have any, we should leave the field blank and press Enter to write the email body. Then, we send the email by pressing Ctrl+D.
For example, let’s send an email with the subject “Test Email” to [email protected]:
$ mail -s "Test Email" [email protected]
When we run the code above, we can fill in for any Cc or skip if none and send the mail with Ctrl+D.
However, we might want to attach a file to the mail:
$ mail -s "subject" -A message.txt [email protected]
Here, we use the -s option followed by the file path to attach the file message.txt.
Alternatively, we can pipe a message to the mail command:
$ echo "Hello world" | mail -s "subject" [email protected]
In this snippet, we use the echo command to write the message body Hello world, pipe the output to the mail command with |, and send it to the recipient.
Notably, when we send email with the above examples, the mail command uses our system username as the sender name. We’ll look at changing the sender name in the next section.
4. Setting the Sender’s Name
By default, the mail command uses the username on our current system as the sender name. However, this may not be desirable or appropriate for some situations. Therefore, we need to set the sender name manually while using the mail command in Linux.
The sender name is the name that appears before the sender address in the email header. For example, we might have an email header like “From Abdmuizz [email protected]”, the sender name is Abdmuizz, and the address is [email protected].
Basically, there are different methods we can use to set the sender name while using the mail command in Linux:
- using the -r option followed by the sender name and address in quotes
- specifying the FROM name and address
We will look at them in the following sub-sections.
4.1. Using the -r Option
The -r option allows us to return the address of the sender. We can also add the sender’s name by putting the address in <>. For example:
$ mail -r "Abdmuizz <[email protected]>" -s "Test Email" [email protected]
Here, we use the -r option to set the sender name as Abdmuizz, [email protected] as the sender’s email address, and Test Email as the subject of the mail we want to send to the recipient [email protected].
Notably, we must enclose the sender’s name and address in quotes and separate them with a space. The sender’s address must also be a valid email address that matches the mail name.
4.2. Specifying the From Name and Address
The -a option allows us to add additional header information to attach to the message. We can use it to provide the FROM name and address. Let’s look at an example:
$ echo "This is the message body" | mail -s "Subject" -aFrom:[email protected] [email protected]
We use the -a option to add a header by specifying the sender’s email address. Let’s specify the sender name:
$ echo "This is the message body" | mail -s "Subject" -aFrom:Abdmuizz\<[email protected]\> [email protected]
Now, we set the sender’s name and address and then use the echo command to pipe the message body to the mail command. However, we must escape the less-than and greater-than arrows since they represent another thing in Shell.
5. Conclusion
In this tutorial, we’ve learned how to set the sender name while using the mail command in Linux. Firstly, we looked at installing and using the mail command in Linux with different options and examples, like using the echo command with the mail command simultaneously.
Finally, we looked at how to set the sender name and address differently while using the mail command. Setting the sender name is essential for identifying, enhancing, and preventing the email from being marked as spam or phishing.