mailx in Linux
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:
sudo apt-get install bsd-mailx
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" firstname.lastname@example.org Hey person, Hope you're fine these days Thanks EOT
2. Taking the message from a file
$ mail -s "A mail sent using mailx" email@example.com < /path/to/file
3. Using pipes
$ echo "Example Message" | mail - s "A mail sent using mailx" firstname.lastname@example.org
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" personONE@example.com, personTWO@example.com < /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" personONE@example.com -c personTWO@example.com -b personTHREE@example.com
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" personONE@example.com -a Attachment.txt