Linux has a lot of tools, services, and applications related to email. An email has a different architecture than standard client-server. We will look at a command-line based mailing application named mailx. mailx
more advanced version of the mail
tool. mailx command supports the MIME, IMAP, POP3, SMTP, and S/MIME protocols and based Berkeley Mail 8.1 mail command.
General Concept
As stated in previous paragraph email systems are bit more complex than standard client server architecture. There are some terms we should learn before continue
Mail Client
is an application used by user directly to send and get emails. Mail clients connects to the mail transfer agent to transmit emails.Mail Transfer Agent (MTA)
is an intermedia component used to receive, send, store mail messages. there are some protocol used to accomplish these tasks like smtp,pop3,imap etc.
Below are some architectural view of a simple email transmission.
Mail Client Sender --> MTA of sender --> MTA receiver --> Mail Client Receiver
Install mailx Command
Linux distributions provides two mailx command one from the mailutils package which is installed by default and the other one is from the bsd-mailx package. In order to work properly we should install the bsd-mailx package like below.
$ sudo apt install bsd-mailx

Help
Brief help information about the mailx command can be printed with the --help
option like below.
$ mail --help

Send Email
We will simply mail some text without provide extra information. This is the fastest way to mail also. In this tutorial we will simply use local mail provider but in the SMTP configuration section we will deal with a SMTP server. In this example we will provide subject part of the mail with -s
option and we will also provide email address which is the local user named ismail
$ mail -s "Hello World" ismail@localhost

There is a warning which says Null message body; hope that's ok
. We think it is ok.
Read Email
In previous example we have send mail to local system user ismail
. When user ismail
logins to the system he will get a message saying he have an email message. This email can be read from /var/mail/ismail
where ismail
is the users name with simple cat
command.
$ cat /var/mail/ismail

Read Email Body From File
While sending email there will be body part of the mail. This body part can be written by hand. But if it is long and repetitive task we do not want to do always there is an alternative. Body part can be read from a file by simply redirecting the content to the mail
command like below. In this example body.txt
file contains body part of the mail. This file may also an html file too.
body.txt
Hi poftut How are you? I hope your pageviews are good. Have a high sessioned day
And we send
$ mail -s "Hello World" ismail@localhost < body.txt
There is other way to send mail by redirecting body content with pipe like below.
$ cat body.txt | mail -s "Hello World" ismail@localhost
Set Multiple Recipient
Another useful feature of mail
command is providing multiple recipients by simply delimiting recipients emails. In this example we will send email both to the ismail@localhost
and root@localhost
. Keep in mind that there may be more than two recipients.
$ mail -s "Hello World" ismail@localhost,root@localhost < body.txt

As we can see mail notification is appeared very fast.
Add CC and BCC To The Mail
Carbon Copy - CC
and Blind Carbon Copy - BCC
are used to send copy of the mail to the other recipients in a visible or hidden way. To provide CC use -c
option with email addresses. To provide BCC use -b
option with email addresses. In the example we will send mail to the ismail@localhost
with root@localhost
in CC and test@localhost
in BCC. From this example we will use same options but more featured mailx
command.
$ mailx -s "Hello World" ismail@localhost -c root@localhost -b test@localhost < body.txt

If we look our mailbox we can see mail and cc but can not see bcc
Specify From Name And Address
As we know we can set sender name and email address explicitly. We will use -r
option to set sender name and email address. Specify email address like <email@address.com>
. In this example we write sender name as İsmail Baydan
and sender address as ismail@localhost
$ mailx -s "Hello World" root@localhost -r "İsmail Baydan<ismail@localhost>"

The sender address and name can be seen explicitly.
Specify “Reply To” Address
Reply to address is used to set return address for the mail if the receiver wants to write back. This can be especially useful for automated systems where return will be do a specific email address.Reply to information can be set with -S
option. In the example we will set reply to address as ismail@localhost
$ mailx -s "Hello World" -S replyto="İsmail Baydan <ismail@localhost>" root@localhost
Add Attachment
Attachments are crusial part of the email. Emai users generally attaches some documents, image, zip file to the emails to send to the receiver. In GUI email clients it can be easy as copy paste or selecting file. But how can we send email attachments in command line interface. Attachments can be added with-a
option with the attachements path. In the example we will add file name a.txt
to our mail as attachment.
$ mailx -s "Some File" ismail@localhost -a a.txt

Use External SMTP Server
Up to now we have used the the local mail system. Local mail system is provided as a simple mechanism by Linux operating system. In the real world examples email system generally uses SMTP, POP3, IMAP services. But in order to send emails we need to setup SMTP server for the mail and mailx command. SMTP configuraiont is put into command line and have some text to type.
$ mailx -v -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login \ -S smtp-auth-user="user1@gmail.com" -S smtp-auth-password="1q2w3e" -S ssl-verify=ignore ismail@gmail.com
We will look what this command options and arguments mean.
-S smtp="mail.example.com:587"
specifies the SMTP server host name or IP address and port number which is 587-S smtp-use-starttls
specifies to use tls encrytion to make communication encrypted and secure-S smtp-auth=login
specifies what type of SMTP authentication will occur.-S smtp-auth-user="user1@gmail.com"
specified the username that will be used to authenticate-S smtp-auth-password="1q2w3e"
specifies the password. Be sure that the system you are using is secure because the clear text password is provided.-S ssl-verify=ignore
this is used to ignore ssl verification of SMTP server. This option is used to prevent self signed ssl problems. But this makes the operation less secure.
Debug, Troubleshoot and Verbose Output
The general assumption is that there will be problems where sending emails. So wee need to debug to get details of the problem. For both mail
and mailx
commands. The -v
option can be used to debug the mailing operation.
$ mailx -v -s "Some File" ismail@localhost < body.txt
List Emails with mailx Command From Command Line
We list and read delivered emails with the mailx command like below. The list provides information like sender, date, time, size and the subject part.
$ mailx

Check whether CC is carbon copy or courtesy copy, and BCC is blind courtesy copy
Sent as a courtesy, not some much a carbon.
Well done Ismail.
@Deane, CC and BCC are throw backs to mail that was typed on paper, well
before electronic mail applications, where a sheet of carbon paper was placed
between the pages to impress the typewriter characters on more than one paper.
Protocol at that time was to designate that the letter was being sent others
by placing CC: followed by the name of a recipient at the bottom of the cover
page. Blind copies were used when the sender did not want the recipient to
know that the message was being sent to someone and were only added to the
copies sent to the blind recipient. This protocol was mimicked in electronic
mail applications. Many have attempted to redefine the acronym, but as in
mailx it does mean ‘carbon copy’ and ‘blind carbon copy’. (ref: man mailx)
Best regards to All!