How to Send Mails With Attachments Using mailx Command?

The “mailx” command in Linux is a command-line utility used to send and receive email messages. It is a component of the “mailutils” package and is compatible with both Unix and Linux systems. This command allows users to compose, send, and receive emails with attachments directly from the command line.

This article will illustrate various ways to send emails with attachments using the “mailx” command.

  • Using mailx Command to Send Mails with Attachments 
  • Prerequisite: Install “mailx” Command 
  • Syntax With Explanation
  • Send a Mail with Attachments
  • Send a Mail Specifying the Sender Address
  • Send an Email Specifying the CC and BCC Recipient

How to Use the mailx Command to Send Mails With Attachments? 

The “mailx” command sends emails from the terminal. It can be used to send plain text emails and emails with attachments. 

Prerequisite: Install “mailx” Command 

Install the “mailx” command line support for Linux using commands: 

$ sudo apt install mailutils  # Debian/Ubuntu/Linux-Mint
$ sudo yum install mailx      # CentOS/Redhat
$ sudo dnf install mailx      # Fedora/RHEL

Syntax With Explanation

The basic syntax of “mailx” command to send a mail with attachment is provided below:

$ echo "This is the body of the email" | mailx -a /path/to/attachment.pdf -s "Subject of the email" [email protected]
  • echo” command is used to specify the body of the email.
  • -a” option is used to attach the file to the email.
  • -s” option is used to specify the email’s subject.
  • [email protected]” is the email address of the recipient.

Example 1: Send an Email With Attachments

To send an email with an attachment, use the “-a” option followed by the path to the file you want to attach. The sender mail is the default mail that the user can define at the installation stage of the “mailx” command. For instance, the “file.txt” and “file2.txt” attachments will be sent to “[email protected]” as below:

$ echo "Hello Sir, Always Stay Happy" | mailx -A file.txt file2.txt -s "Acknowledgment" [email protected]

The output shows that both “file.txt” and “file2.txt” attachments have been sent to a particular address.

Example 2: Send an Email Specifying the Sender Address

Users can specify the sender address with the “-r” option to send an attachment. In our case, the “file.txt” will be sent as an attachment from the address “[email protected]” to “[email protected]”: 

$ echo "Have a Nice Day" | mailx -A file.txt -s "Wish" -r [email protected] [email protected]

The outcome shows that both “file.txt” attachments have been sent to “[email protected]” address.

Example 3: Send an Email Specifying the CC and BCC Recipient

You can specify a CC and BCC recipient using the “c” and “b” options, respectively. In our case, the “file.txt” is sent as an attachment from “[email protected]” while keeping “[email protected]”, and “[email protected]” in CC and BCC respectively

$ echo "Hello Sir, Great Wishes" | mailx -s "Announcement" -A file.txt [email protected] c [email protected] b [email protected]

The “file.txt” attachment has been sent to the addresses mentioned above.

Conclusion

Linux offers the “mailx” command to send and receive email messages along with attachments directly from the command line. The command’s syntax is “echo “body” | mailx -a attachment.pdf -s “Subject” [email protected]” to send an email with attachments. This post has briefly explained the usage of the “mailx” command to send mails with attachments.