Linux Mail

Send and receive mail from the Linux command line by  

The Linux mail command is useful for sending messages locally on your machine or over your network. It is fully capable of sending email over the Internet, but most mail providers and ISPs will block it. So, it works much better for local network messaging.

Sending Mail With the Mail Command

First, you’re probably wondering how you send mail using the Linux mail command. Even though there are a ton of options, you can fire off a simple email without much effort. Use the mail command followed by the ‘-s’ flag to specify a subject in quotes. Then, follow it with an email address or a username at(@) a computer hostname or IP. It should look something like this:

mail -s "Email Subject" admin@webserver

After you press Enter, the mail command will drop you into a different prompt, letting you add a CC: to your message. Press Enter again to leave it blank.

Linux mail compose message

Then, the prompt will shift again. This time, it’ll appear fairly blank. This is for the body of your email message. Type the message as normally as possible. You can press Enter at any time to move down to a new line. When you’re done, press Enter one last time to move to a new line. Then, press Ctrl+D on your keyboard to complete the message and send it.

Linux mail send message

The mail command does offer more options and variations when sending email For instance, you can send an email to a user on your own system with only a username, no IP or hostname is required.

mail -s "The Subject" username

You can also send an email to multiple people at once.

mail -s "Group Email" user1@192.168.1.110,user2@192.168.1.110,user3@192.168.1.110

If you want to try sending an email using a regular domain name based email address, you can, but it’s probably going to get blocked along the way unless the recipient’s account is configured to accept mail from you.

mail -s "To Company Mail Address" user@example.com

You don’t have to wait for the prompt to add a CC or BCC. You can do that directly in your initial command using the ‘-c’ and ‘-b’ flags respectively.

mail -s "Your Account" user@webserver -c admin@webserver -b dev@webserver

The mail command also lets you include the message in the same line you call it. This probably isn’t ideal in most situations, but it does work well for short and simple messages.

mail -s "Call Me Back" admin@webserver <<< 'Please call me back when you get a chance.'

If you like the convenience of adding the message in a single line or you just don’t care for the mail prompt, you can actually specify an external file as the message body with the mail command too. Refer the command to the location of your message, and it’ll do the rest.

mail -s "The Subject" admin@webserver < /home/user/mailmessage.txt

However you choose to send your message, the mail command makes it relatively quick and easy.

 

Reading Mail With the Mail Command

Reading your mail with the mail command is a little different. Try running just the mail command, with no extra flags or information.

mail

Depending on your situation, it may display a series of messages in your inbox or a message letting you know that you don’t have any new mail. If you want to explore the mailbox, send yourself a message, and run the command again.

Linux mail inbox

This time, you’ll see at least your message listed out, with a number next to it. Below that, you’ll notice a new prompt. This is the mail terminal, and it has its own set of commands. To view your message, press p followed by the number associated with it.

p 1

The message will print out in the terminal.

Linux mail print message

If you have multiple messages, you can cycle through them with the n command.

n

You can reply to a message with the r key followed by the number associated with it.

r 1

That will drop you the composition terminal, much like you did before when writing the body of your message with the mail command. Like that, you can finish and send your message by pressing Ctrl+D on a blank line.

When you want to view the list of your mail again, use the h key. It’ll print out the same type of list you received when you first opened the mail console.

h

When you exit the mail console, your read messages will be removed. To save one for later, you can use the S key followed by the message number.

S 1

Linux mail save and list

Finally, when you want to exit the mail console, use q.

q

This is just the absolute surface of the mail command and the console associated with it. For more, enter help in the mail console or refer to the technical manual below.

Mail Technical Manual

For a complete listing and understanding of Linux Mail please see the man pages.