Open In App
Related Articles

mailx in Linux

Like Article
Save Article
Report issue

Linux has an inbuilt Mail User Agent program called mailx. As the name suggests, it is a console application that is used for sending and receiving emails. The mailx utility is an enhanced version of the mail command. Along with the functionality provided by the original mail command, it provides extra features like the ability to send attachments by using the -a flag.  The mailx command is available from a variety of different packages: 

  • bsd-mailx 
  • heirloom-mailx 
  • mailutils  

Installing mailx

For Ubuntu/Debian: 

sudo apt-get install bsd-mailx

For fedore/centos: 

sudo yum install mailx

Note: Even though the mailx command is a newer version of the original mail utility, it can still be referenced with the ‘mail’ keyword. 

Sending an Email 

1. Writing the message directly in the command line: To send a simple email, use the “-s” flag to set the subject in quotes which is followed by the email of the receiver. After this, mailx waits for the content of the email. To enter new lines, keep hitting enter. After the content is written, press Ctrl+D & EOT will be displayed by mailx. 

$ mail -s "A mail sent using mailx"
Hey person,
Hope you're fine these days

2.  Taking the message from a file 

$ mail -s "A mail sent using mailx" < /path/to/file

3.  Using pipes 

$ echo "Example Message" | mail - s "A mail sent using mailx"

4. Sending same Mail to Multiple Recipients: We can send the same email to multiple receivers (not by cc or bcc) as follows: 

$ mail - s "A mail sent using mailx", < /path/to/file

5. Adding CC & BCC 

We can send a carbon copy (CC) or a blind carbon copy (BCC) to send the same email to multiple recipients (visibly or in a hidden manner). For CC, we use the “-c” option & for BCC we use the “-b” option which is followed by the email addresses. 

$ mail - s "A mail sent using mailx" -c -b 

6. Adding Attachments
Attachments are a vital part of email communication. We can attach documents, images, text files, etc. by using the “-a” option followed by the path of the file that we want to attach. 

$ mail - s "A mail sent using mailx" -a Attachment.txt

Last Updated : 10 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads