How to Use the Command 'mailx' (with examples)
The mailx
command is a powerful Unix utility tool used primarily for sending and receiving messages from a command-line interface. It is highly versatile and can be used for sending emails both interactively and through automated scripts. You might encounter mailx
in environments with Unix-based systems where automated emailing capabilities are needed without the overhead of a web-based email service.
Use Case 1: Send mail
Code:
mailx -s "subject" to_addr
Motivation:
This use case is useful when you need to quickly compose and send an email directly from the command line. It’s straightforward and prompts the user to type the content of the email, which is terminated by entering Ctrl+D
. This method is ideal when you want to send a short message without needing a separate text editor for writing the content.
Explanation:
mailx
: The command itself, known for sending and receiving emails.-s "subject"
: This argument specifies the subject line of the email, which is important for recipients to understand the context of your message at a glance.to_addr
: This is the recipient’s email address. You replaceto_addr
with the actual email address of the recipient.
Example Output:
Once you execute the command, mailx
opens an interface where you can type your email content. On pressing Ctrl+D
, the email is sent to the specified recipient.
Use Case 2: Send mail with content passed from another command
Code:
echo "content" | mailx -s "subject" to_addr
Motivation:
This method is useful when you want to automate the sending of standard messages. By piping the output of a command such as echo
into mailx
, you can seamlessly integrate email notifications into scripts. This is particularly beneficial in scenarios where the content of the email needs to be dynamically generated or compiled from other command outputs.
Explanation:
echo "content"
: Outputs the specified string or message. Here, it contains the content of the email you want to send.|
: This is a pipe operator that takes the output of the preceding command and uses it as input for the following command.-s "subject"
: Sets the subject line for the email.to_addr
: The recipient’s email address.
Example Output:
After executing the command, the specified content is sent as the body of the email to the recipient.
Use Case 3: Send mail with content read from a file
Code:
mailx -s "subject" to_addr < content.txt
Motivation:
Reading the email content from a file is a practical choice when the message is lengthy or frequently reused. This helps maintain a clean workflow and allows for easy editing of the message content using text editors before sending it out. It’s particularly helpful for sending formal templates or reports stored in files.
Explanation:
mailx
: The command for email operations.-s "subject"
: Designates the subject line of your email.to_addr
: The recipient’s email address.< content.txt
: Redirects the content of the filecontent.txt
as the input to the email body.
Example Output:
The email is sent with the content of content.txt
as the body of the email. The recipient receives the message with the subject specified and the complete contents of the file.
Use Case 4: Send mail to a recipient and CC to another address
Code:
mailx -s "subject" -c cc_addr to_addr
Motivation:
Use this method when you need to send an email not only to a primary recipient but also to another recipient as a carbon copy (CC). This is commonly used in professional communication to keep relevant parties in the loop about the information being shared without addressing them directly.
Explanation:
mailx
: The command used to execute the mail operation.-s "subject"
: Sets the email’s subject line.-c cc_addr
: The-c
flag is used to add a carbon copy recipient. Replacecc_addr
with the actual secondary recipient’s email address.to_addr
: The primary recipient’s email address.
Example Output:
The email is sent to to_addr
with a copy of the email also sent to cc_addr
.
Use Case 5: Send mail specifying the sender address
Code:
mailx -s "subject" -r from_addr to_addr
Motivation:
In scenarios where you are managing different stakeholder communications from the same system, specifying the sender’s address helps represent different departments or teams. This can be particularly vital in organizations where emails need to come from specific email aliases for branding or identification purposes.
Explanation:
mailx
: Invokes the mail utility.-s "subject"
: Establishes the subject of the email.-r from_addr
: Specifies the email address to be used as the sender’s address, helpful for customizing and managing sender information.to_addr
: The recipient’s email address.
Example Output:
The email is dispatched to the recipient but shows from_addr
as the sender’s address in their inbox.
Use Case 6: Send mail with an attachment
Code:
mailx -a path/to/file -s "subject" to_addr
Motivation:
Attachments are vital for sending documents, images, or any file-based information through emails. This use case is crucial when you need to share important files directly from the command line without switching to a different email client.
Explanation:
mailx
: The command used to facilitate the process.-a path/to/file
: This attaches the specified file to the email.path/to/file
should be replaced with the actual path of the file you want to attach.-s "subject"
: Provides the subject line for the email message.to_addr
: The intended recipient’s email address.
Example Output:
An email is sent with the designated subject and the specified file is included as an attachment, ready for the recipient to download.
Conclusion:
Understanding how to use the mailx
command effectively allows you to manage email communications directly from your Unix command line environment. Each use of mailx
, from sending simple messages to attaching files or setting specific sender addresses, serves distinct practical purposes that can significantly enhance productivity in automation, scripting, and day-to-day operations.